OpenCV - Filter Drops Width and Height

I have an image filtered by a canny edge detector. Now I want to detect drops and make a filter in width and height. What features should I look at?

+1
source share
2 answers

alternative approach based on minAreaRect contours and distance between minAreaRect points. in this way, the contours can be filtered by their angles, as shown in the sample result sample.

you can change the ratio of width to height and angel by changing the following lines

if(dist0 > dist1 *4) // dist0 and dist1 means width and height you can change as you wish . . if( fabs(angle) > 35 & fabs(angle) < 150 ) // you can change angle criteria 

enter image description here enter image description here

 #include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" using namespace cv; using namespace std; //! Compute the distance between two points /*! Compute the Euclidean distance between two points * * @param a Point a * @param b Point b */ static double distanceBtwPoints(const cv::Point2f &a, const cv::Point2f &b) { double xDiff = ax - bx; double yDiff = ay - by; return std::sqrt((xDiff * xDiff) + (yDiff * yDiff)); } int main( int argc, char** argv ) { Mat src,gray; src = imread(argv[1]); if(src.empty()) return -1; cvtColor( src, gray, COLOR_BGR2GRAY ); gray = gray < 200; vector<vector<Point> > contours; findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); RotatedRect _minAreaRect; for (size_t i = 0; i < contours.size(); ++i) { _minAreaRect = minAreaRect( Mat(contours[i]) ); Point2f pts[4]; _minAreaRect.points(pts); double dist0 = distanceBtwPoints(pts[0], pts[1]); double dist1 = distanceBtwPoints(pts[1], pts[2]); double angle = 0; if(dist0 > dist1 *4) angle =atan2(pts[0].y - pts[1].y,pts[0].x - pts[1].x) * 180.0 / CV_PI; if(dist1 > dist0 *4) angle =atan2(pts[1].y - pts[2].y,pts[1].x - pts[2].x) * 180.0 / CV_PI; if( fabs(angle) > 35 & fabs(angle) < 150 ) for( int j = 0; j < 4; j++ ) line(src, pts[j], pts[(j+1)%4], Scalar(0, 0, 255), 1, LINE_AA); } imshow("result", src); waitKey(0); return 0; } 
+4
source

In OpenCV 3.0, you can use connectedComponentsWithStats , which returns an array of stats that includes the width and height of each connected component:

statsv -

statistics output for each label, including background label, see below for available statistics. Statistics are accessed through statsv (label, COLUMN), where the available columns are defined below.

  • CC_STAT_LEFT The leftmost (x) coordinate, which is the included start of the bounding box in the horizontal direction.
  • CC_STAT_TOP Upper (y) coordinate, which is the included start of the bounding box in the vertical direction.
  • CC_STAT_WIDTH Horizontal bounding box size
  • CC_STAT_HEIGHT Vertical bounding box size
  • CC_STAT_AREA Total area (in pixels) of the connected component
+3
source

All Articles