java - Overlapping shapes recognition (OpenCV) -
i have simple image containing shapes: rectangles , ellipses, in total number of 4 or 5. shapes can rotated, scaled , overlapped. there sample input: task detect of these figures , prepare information them: size, position, rotation, etc. in opinion, core problem fact shapes can overlapped each other. tried search information kind of problem , find opencv library can useful.
opencv has ability detect contours , try fit ellipses or rectangles these contours. problem when shapes overllaped, contours mixed up.
i think following algorithm: detect characteristic points: , put white dot @ them. got these every figure divided separate sections: can try link these parts using information, example complexity value (i fit curve approxpolydp contour , read how many parts has). starts hard. other idea try of permutations of linking contours , trying fit figures them. best compilation output.
any ideas how create simple elegant solution?
blurring image helps find intersections seen in code
#include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" using namespace cv; int main( int argc, char** argv ) { mat src = imread( argv[1] ); mat gray, blurred; cvtcolor( src, gray, color_bgr2gray ); threshold( gray, gray, 127, 255, thresh_binary ); gaussianblur( gray, blurred, size(), 9 ); threshold( blurred, blurred, 200, 255, thresh_binary_inv ); gray.setto( 255, blurred ); imshow("result",gray); waitkey(); return 0; }
step 2
simply, borrowed code generalcontours_demo2.cpp
#include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" using namespace cv; using namespace std; int main( int argc, char** argv ) { mat src = imread( argv[1] ); mat gray, blurred; cvtcolor( src, gray, color_bgr2gray ); threshold( gray, gray, 127, 255, thresh_binary ); gaussianblur( gray, blurred, size(), 5 ); threshold( blurred, blurred, 180, 255, thresh_binary_inv ); gray.setto( 255, blurred ); imshow("result of step 1",gray); vector<vector<point> > contours; /// find contours findcontours( gray.clone(), contours, retr_tree, chain_approx_simple ); /// find rotated rectangles , ellipses each contour vector<rotatedrect> minrect( contours.size() ); vector<rotatedrect> minellipse( contours.size() ); for( size_t = 0; < contours.size(); i++ ) { minrect[i] = minarearect( mat(contours[i]) ); if( contours[i].size() > 5 ) { minellipse[i] = fitellipse( mat(contours[i]) ); } } /// draw contours + rotated rects + ellipses for( size_t = 0; i< contours.size(); i++ ) { mat drawing = src.clone(); // contour //drawcontours( drawing, contours, (int)i, color, 1, 8, vector<vec4i>(), 0, point() ); // ellipse ellipse( drawing, minellipse[i], scalar( 0, 0, 255 ), 2 ); // rotated rectangle point2f rect_points[4]; minrect[i].points( rect_points ); for( int j = 0; j < 4; j++ ) line( drawing, rect_points[j], rect_points[(j+1)%4], scalar( 0, 255, 0 ), 2 ); /// show in window imshow( "results of step 2", drawing ); waitkey(); } return 0; }
you can following result images among others. hope solve final step.
Comments
Post a Comment