Advertisement
Guest User

Untitled

a guest
Sep 18th, 2015
151
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <opencv2/highgui.hpp>
  2. #include <opencv2/imgproc.hpp>
  3.  
  4. using namespace cv;
  5. using namespace std;
  6.  
  7. // calculates distance between two points
  8. static double distanceBtwPoints(const cv::Point a, const cv::Point b)
  9. {
  10.     double xDiff = a.x - b.x;
  11.     double yDiff = a.y - b.y;
  12.  
  13.     return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
  14. }
  15.  
  16. static int findNearestPointIndex(const cv::Point pt, const vector<Point> points)
  17. {
  18.     int nearestpointindex = 0;
  19.     double distance;
  20.     double mindistance = 1e+9;
  21.  
  22.     for ( size_t i = 0; i < points.size(); i++)
  23.     {
  24.         distance = distanceBtwPoints(pt,points[i]);
  25.  
  26.         if( distance < mindistance )
  27.         {
  28.             mindistance =  distance;
  29.             nearestpointindex = i;
  30.         }
  31.     }
  32.     return nearestpointindex;
  33. }
  34.  
  35. int main( int argc, char** argv )
  36. {
  37.     Point pt0;
  38.     int shift=0; // optional value for drawing scaled
  39.     Scalar color = Scalar(0,0,0);
  40.  
  41.     char* filename = argc >= 2 ? argv[1] : (char*)"test.png";
  42.     Mat img = imread(filename);
  43.     if (img.empty())
  44.         return -1;
  45.  
  46.     Mat src = img;
  47.     Mat bw;
  48.  
  49.     cvtColor( src, bw, COLOR_BGR2GRAY );
  50.     bw = bw < 127;
  51.  
  52.     // Find contours
  53.     vector<vector<Point> > contours;
  54.     vector<Point> contour;
  55.     findContours( bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
  56.  
  57.     // assuming the closed contour have biggest size
  58.     contour = contours[0];
  59.     for ( size_t i = 0; i < contours.size(); i++)
  60.     {
  61.         if( contour.size() < contours[i].size() )
  62.             contour = contours[i];
  63.     }
  64.  
  65.     for ( size_t i = 0; i < contours.size(); i++)
  66.     {
  67.         if( contour != contours[i] && contours[i].size() > 10 )
  68.         {
  69.             for ( size_t j = 0; j <  contours[i].size(); j++)
  70.             {
  71.                 pt0 = contours[i][j];
  72.                 line(src,pt0,contour[findNearestPointIndex(pt0,contour)],color,1,LINE_8,shift);
  73.             }
  74.         }
  75.     }
  76.  
  77.     cvtColor( src, src, COLOR_BGR2GRAY );
  78.     src = src < 127;
  79.     vector<vector<Point> > contours1;
  80.     findContours( src, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
  81.  
  82.     src = 255; // clears source image to redraw
  83.     for ( size_t i = 0; i < contours1.size(); i++)
  84.     {
  85.         drawContours(src,contours1,i,color);
  86.     }
  87.  
  88.     Mat m_xor(src.size(),CV_8UC1,255);
  89.     polylines(m_xor,contour,false,color,1,LINE_8);
  90.  
  91.     m_xor = 255 - (src ^ m_xor) ;
  92.  
  93.     imwrite("result.png",m_xor);
  94.     imshow("result", m_xor);
  95.     waitKey(0);
  96.     return 0;
  97. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement