android - OpenCV - Detect hand-drawing shapes -
could opencv detect geometric shapes drawn hand below? shape can rectangle, triangle, circle, curve, arc,polygon,... going develop android application detect these shapes. 
well, tried in harry. need skeletonize input. anyway. can reason shapes based on points. square has 4, triangle 3, etc. effort results:
canny results: 
polygonal approximation: 
console output:
contour points:11 contour points:6 contour points:4 contour points:5 here code:
mat src=imread("wyokm.png"); mat src_gray(src.size(),cv_8uc1); if (src.empty()) exit(-10); imshow("img",src); /// convert image gray , blur cvtcolor( src, src_gray, cv_bgr2gray ); threshold(src_gray,src_gray,100,255,src_gray.type()); imshow("img2",src_gray); mat canny_output; vector<vector<point> > contours; vector<vec4i> hierarchy; /// detect edges using canny int thresh=100; canny( src_gray, canny_output, thresh, thresh*2, 3 ); imshow("canny",canny_output); imwrite("canny.jpg",canny_output); /// find contours findcontours( canny_output, contours, hierarchy, cv_retr_tree, cv_chain_approx_simple, point(0, 0) ); // testing approximate polygon cv::mat result(src_gray.size(),cv_8u,cv::scalar(255)); for(int i=0;i<contours.size();i=i+4) //for testing reasons. skeletonize input. { std::vector<cv::point> poly; poly.clear(); cv::approxpolydp(cv::mat(contours[i]),poly, 5, // accuracy of approximation true); // yes closed shape // iterate on each segment , draw std::vector<cv::point>::const_iterator itp= poly.begin(); cout<<"\ncontour points:"<<poly.size(); while (itp!=(poly.end()-1)) { cv::line(result,*itp,*(itp+1),cv::scalar(0),2); ++itp; } // last point linked first point cv::line(result, *(poly.begin()), *(poly.end()-1),cv::scalar(20),2); } imshow("result",result); imwrite("results.jpg",result); cvwaitkey();
Comments
Post a Comment