Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- //compiled with g++ -Wall -c "%f" -I/usr/local/include/opencv4
- //built with: g++ -Wall -o "%e" "%f" `pkg-config --cflags --libs opencv4`
- using namespace cv;
- using namespace std;
- /// Function header
- void thresh_callback(int, void* );
- void vectorOutput( vector<vector<Point>>, vector<Vec4i> hierarchy);
- void contour2svg(vector<vector<Point>> contours);
- void svgHeader();
- void polyline();
- void svgFooter();
- Mat src; Mat src_gray;
- int thresh = 50;
- int max_thresh = 215;
- RNG rng(12345); //random number generator
- /** @function main */
- int main( int argc, char** argv ){
- VideoCapture cap;
- // open the default camera, use something different from 0 otherwise;
- if(!cap.open(0))
- return 0;
- // Create mat with alpha channel
- Mat mat(800, 800, CV_8UC4);
- for(;;){
- Mat frame;
- cap >> frame;
- src = frame;
- /// Convert image to gray and blur it
- cv::cvtColor(src, src_gray, COLOR_BGRA2GRAY, 0);
- blur( src_gray, src_gray, Size(3,3) );
- thresh_callback( 0 , 0 );
- if( waitKey(1) == 113 ) break; // stop capturing by pressing q
- }
- return(0);
- }
- /*******************/
- /* */
- /* FUNCTIONS */
- /* */
- /*******************/
- void contour2svg(){
- svgHeader();
- polyline();
- svgFooter();
- }
- void svgHeader(){
- cout<< "<svg width=\"800\" height=\"800\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">" << endl;
- };
- void polyline(){
- //actual function might be: cout points(Cordinates)
- cout << "<polyline\n" << endl;
- cout << /*points*/ "\n" << endl;
- cout << " stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>" << endl;
- }
- void svgFooter(){
- cout << "</svg>" << endl;
- };
- void vectorOutput( vector<vector<Point> > contours, int i){
- for(auto vec: contours){
- cout << vec << "\n";
- }
- };
- /** @function thresh_callback */
- void thresh_callback(int, void* ){
- Mat canny_output;
- vector<vector<Point> > contours;
- vector<Vec4i> hierarchy;
- /// Detect edges using canny
- Canny( src_gray, canny_output, thresh, thresh*2, 3 );
- /// Find contours
- findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point() );
- /// Draw contours
- /*void cv::drawContours (
- InputOutputArray image,
- InputArrayOfArrays contours,
- int contourIdx,
- const Scalar & color,
- int thickness = 1, //can be a FLOAT
- int lineType = LINE_8,
- InputArray hierarchy = noArray(),
- int maxLevel = INT_MAX,
- Point offset = Point() //can be Point() or Point(x,y) cordinates
- )*/
- Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
- for(unsigned int i = 0; i< contours.size(); i++ ){
- //Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
- Scalar color = Scalar( 0, 0,255 ); //BGR, set to red
- drawContours( drawing, contours, i, color, 0.1, 8, hierarchy, 1, Point(0,0) );
- //vectorOutput( contours, i);
- }
- sleep(0.25); //slow down there flashy flashy
- /// Show in a window
- //createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
- namedWindow("Name", 1); //what does 1 mean?
- cv::imshow("Name", drawing);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement