Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 41027

Possible if only draw a Rectangle at the front object?

$
0
0
May i know is that possible if we draw a rectangle at the front object show as below picture? **This is the Real Situation** ![image description](/upfiles/14532883886478712.jpg) **This is what the robot will get from webcam when tracking the color** ![image description](/upfiles/1453288426893377.jpg) **This what was i get now (draw a rectangles at biggest object)** ![image description](/upfiles/14532884812885547.jpg) **This is what i want to do** ![image description](/upfiles/14532885384234651.jpg) int largestIndex = 0; int largestContour = 0; int secondLargestIndex = 0; int secondLargestContour = 0; vector> contours; vector hierarchy; //find contours findContours(thresh, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); /// Find the convex hull object for each contour vector>hull(contours.size()); vector>inthull(contours.size()); vector>defects(contours.size()); for (int i = 0; i < contours.size(); i++) { convexHull(Mat(contours[i]), hull[i], false); convexHull(Mat(contours[i]),inthull[i], false); if (inthull[i].size()>3) convexityDefects(contours[i], inthull[i], defects[i]); } //find 2 largest contour for( int i = 0; i< contours.size(); i++ ) { if(contours[i].size() > largestContour) { secondLargestContour = largestContour; secondLargestIndex = largestIndex; largestContour = contours[i].size(); largestIndex = i; } else if(contours[i].size() > secondLargestContour) { secondLargestContour = contours[i].size(); secondLargestIndex = i; } } //show contours of 2 biggest and hull as well if(contours.size()>0) { //check for contouraea function if error occur //draw the 2 largest contour using previously stored index. //drawContours(frame, contours, largestIndex, CV_RGB(0,255,0), 2, 8, hierarchy); //drawContours(frame, contours, secondLargestIndex, CV_RGB(0,255,0), 2, 8, hierarchy); //draw rectangle at specified obstacle Rect minRect = boundingRect( Mat(contours[ largestIndex ]) ); rectangle( frame, minRect, Scalar( 0, 0, 255 ),2 ); minRect = boundingRect( Mat(contours[ secondLargestIndex ]) ); rectangle( frame, minRect, Scalar( 0, 0, 255 ),2 );} This code link : http://paste.ofcode.org/33beiUcpvTuPd5pfVGhnPUK

Viewing all articles
Browse latest Browse all 41027

Trending Articles