Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. void detachRods(Mat& image)
  2. {
  3.     Mat invImage;
  4.     bitwise_not(image, invImage);
  5.     // Find contours
  6.     std::list<Point> defectPoints;
  7.     std::list<Point>::iterator it;
  8.     it = defectPoints.begin();
  9.     vector<Point> defectPointsVect;
  10.     vector<vector<Point> > contours;
  11.     vector<int> contoursHull;
  12.     vector<Vec4i> defects;
  13.     findContours(invImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  14.  
  15.     for ( size_t i = 0; i < contours.size(); i++)
  16.     {
  17.         double contArea = contourArea(contours[i]);
  18.  
  19.         if(contourArea(contours[i]) > 6000) // This permits to not alter single rods, since 2 or 3 connected rods have a total area much bigger than a single rod
  20.         {
  21.             approxPolyDP(contours[i],contours[i],9,true);
  22.             convexHull(contours[i], contoursHull,true);
  23.             convexityDefects(contours[i], contoursHull,defects);
  24.  
  25.             for ( size_t j = 0; j <  defects.size(); j++)
  26.             {
  27.                 Vec4i defpoint = defects[j];
  28.                 defectPoints.insert(it, contours[i][defpoint[2]]);
  29.             }
  30.         }
  31.     }
  32.  
  33.     defectPointsVect = std::vector<Point>(std::begin(defectPoints), std::end(defectPoints));
  34.     float dist = 0;
  35.     int linesCount = 0;
  36.     for (int i=0; i<defectPointsVect.size(); i++)
  37.     {
  38.         if (linesCount >= defectPointsVect.size()) break; // Needed to not redraw the same line 2 times
  39.         for (int k=0; k<defectPointsVect.size(); k++)
  40.         {
  41.             if (i!=k)
  42.             {
  43.                 dist = sqrt(std::pow((defectPointsVect[i].x-defectPointsVect[k].x), 2) + std::pow((defectPointsVect[i].y-defectPointsVect[k].y), 2));
  44.                 if (dist <= 25)
  45.                 {
  46.                     line(image, defectPointsVect[i], defectPointsVect[k], Scalar(B), 2);
  47.  
  48.                     // Algorithm to fix the image after the rods have been detached (needed cause the detach process may detach also parts of the same rod too):
  49.                     // _________________________________________________________________________________________________________________________________________________________________
  50.                     // For each point of the line that detaches 2 rods (except 2 points at each extreme of the line), all his surrounding pixels are selected (8 connectovity).         |  
  51.                     // For each of this surrounding point, if it's value of grayscale in the image depict a foreground pixel, all his surrounding pixels are selected (4 connectovity)  |
  52.                     // For each of this surrounding pixel, if it doesn't belong to the line that detaches 2 rods, it's grayscale level is set to foreground value.                      |
  53.                     // _________________________________________________________________________________________________________________________________________________________________|
  54.                     //
  55.                     // Grabs pixels along the line (pt1, pt2)
  56.                     LineIterator it(image, defectPointsVect[i], defectPointsVect[k], 8);
  57.  
  58.                     std::vector<Point> linePoints(it.count);
  59.                     for(int i = 0; i < it.count; i++, ++it) linePoints[i] = it.pos();
  60.  
  61.                     for(int i = 2; i < it.count-2; i++)
  62.                     {
  63.                         for (int x=-1; x<=1; x++)
  64.                         {
  65.                             for (int y=-1; y<=1; y++)
  66.                             {
  67.                                 Point pxlPoint(linePoints[i].x+x, linePoints[i].y+y);
  68.                                 unsigned char pxlVal = image.at<uchar>(pxlPoint.y, pxlPoint.x);
  69.                                 if (pxlVal == F)
  70.                                 {
  71.                                     Point topPxl(pxlPoint.x, pxlPoint.y-1);
  72.                                     if (std::find(linePoints.begin(), linePoints.end(), topPxl) == linePoints.end())
  73.                                         image.at<uchar>(topPxl.y, topPxl.x) = F;
  74.  
  75.                                     Point botPxl(pxlPoint.x, pxlPoint.y+1);
  76.                                     if (std::find(linePoints.begin(), linePoints.end(), botPxl) == linePoints.end())
  77.                                         image.at<uchar>(botPxl.y, botPxl.x) = F;
  78.  
  79.                                     Point leftPxl(pxlPoint.x-1, pxlPoint.y);
  80.                                     if (std::find(linePoints.begin(), linePoints.end(), leftPxl) == linePoints.end())
  81.                                         image.at<uchar>(leftPxl.y, leftPxl.x) = F;
  82.  
  83.                                     Point rightPxl(pxlPoint.x+1, pxlPoint.y);
  84.                                     if (std::find(linePoints.begin(), linePoints.end(), rightPxl) == linePoints.end())
  85.                                         image.at<uchar>(rightPxl.y, rightPxl.x) = F;
  86.  
  87.                                 }
  88.                             }
  89.                         }
  90.                     }
  91.  
  92.                     linesCount++;
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement