Advertisement
j0h

contours2svg inprogress

j0h
Jul 20th, 2021
1,950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. //compiled with g++ -Wall -c "%f" -I/usr/local/include/opencv4
  8. //built with: g++ -Wall -o "%e" "%f" `pkg-config --cflags --libs opencv4`
  9.  
  10. using namespace cv;
  11. using namespace std;
  12.  
  13. /// Function header
  14. void thresh_callback(int, void* );
  15. void vectorOutput(  vector<vector<Point>>, vector<Vec4i> hierarchy);
  16. void contour2svg(vector<vector<Point>> contours);
  17. void svgHeader();
  18. void polyline();   
  19. void svgFooter();
  20.  
  21. Mat src; Mat src_gray;
  22. int thresh = 50;
  23. int max_thresh = 215;
  24. RNG rng(12345);  //random number generator
  25.  
  26.  
  27.  
  28.  
  29. /** @function main */
  30. int main( int argc, char** argv ){
  31.    
  32.     VideoCapture cap;
  33.     // open the default camera, use something different from 0 otherwise;
  34.     if(!cap.open(0))
  35.         return 0;
  36.      // Create mat with alpha channel
  37.     Mat mat(800, 800, CV_8UC4);    
  38.  
  39.  
  40. for(;;){   
  41.     Mat frame;
  42.     cap >> frame;
  43.     src = frame;
  44.   /// Convert image to gray and blur it
  45.   cv::cvtColor(src, src_gray, COLOR_BGRA2GRAY, 0);
  46.   blur( src_gray, src_gray, Size(3,3) );
  47.  
  48.   thresh_callback( 0 , 0 );
  49.  
  50.   if( waitKey(1) == 113 ) break; // stop capturing by pressing q
  51. }
  52.   return(0);
  53. }
  54. /*******************/
  55. /*                 */
  56. /*    FUNCTIONS    */
  57. /*                 */  
  58. /*******************/
  59. void contour2svg(){
  60.     svgHeader();
  61.     polyline();
  62.     svgFooter();
  63.     }
  64. void svgHeader(){
  65. cout<< "<svg width=\"800\" height=\"800\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">" << endl;
  66.     };
  67.  
  68. void polyline(){
  69. //actual function might be: cout points(Cordinates)
  70.     cout << "<polyline\n" << endl;
  71.     cout << /*points*/ "\n"   << endl;
  72.     cout << " stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>" << endl;
  73.     }  
  74.    
  75. void svgFooter(){
  76.     cout << "</svg>" << endl;
  77.     };
  78.  
  79.  
  80. void vectorOutput(   vector<vector<Point> > contours, int i){
  81. for(auto vec: contours){
  82.    cout << vec << "\n";
  83. }
  84. };
  85.  
  86. /** @function thresh_callback */
  87. void thresh_callback(int, void* ){
  88.   Mat canny_output;
  89.   vector<vector<Point> > contours;
  90.   vector<Vec4i> hierarchy;
  91.  
  92.   /// Detect edges using canny
  93.   Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  94.   /// Find contours
  95.   findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point() );
  96.  
  97.   /// Draw contours
  98. /*void cv::drawContours     (  
  99.         InputOutputArray    image,
  100.         InputArrayOfArrays      contours,
  101.         int     contourIdx,
  102.         const Scalar &      color,
  103.         int     thickness = 1, //can be a FLOAT
  104.         int     lineType = LINE_8,
  105.         InputArray      hierarchy = noArray(),
  106.         int     maxLevel = INT_MAX,
  107.         Point   offset = Point()  //can be Point() or Point(x,y) cordinates
  108.     )*/
  109.  
  110.   Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
  111.   for(unsigned int i = 0; i< contours.size(); i++ ){
  112. //Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
  113.       Scalar color = Scalar( 0, 0,255 ); //BGR, set to red
  114.        drawContours( drawing, contours, i, color, 0.1, 8, hierarchy, 1, Point(0,0) );
  115.    
  116.      //vectorOutput( contours, i);
  117.  
  118.       }
  119. sleep(0.25); //slow down there flashy flashy
  120.  
  121.   /// Show in a window
  122.     //createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
  123.     namedWindow("Name", 1); //what does 1 mean?
  124.     cv::imshow("Name", drawing);
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement