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

#include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" using namespace cv; using namespace std;
source share