Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 19555

Extract largest contour in very colorful image in OpenCV

$
0
0
I have an image of object with lots of different color and other small contours. I would like to find the largest contour in the image (airplane door). The image (door) can contain lots of painting and logos . So I just wont to find the exact door contour . I have some code in C++ and OpenCv . It works for normal door but when its very colorful it is not so good. I include the code and some image results. Here the part of the code. I played with the threshold but can not get better results. Also is it possible to extract that door contour automatically with image processing without playing with the threshold? Here my sample code #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include #include #include using namespace cv; using namespace std; Mat src; Mat src_gray; int thresh = 5; int max_thresh = 600; RNG rng(12345); /// Function header void thresh_callback(int, void* ); int main() { src = imread("door3.jpg"); resize(src, src, Size(640,480), 0, 0, INTER_CUBIC); // convert to grayscale (you could load as grayscale instead) cvtColor( src, src_gray, CV_BGR2GRAY ); blur( src_gray, src_gray, Size(1,3) ); /// Create Window char* source_window = "Source"; namedWindow( source_window, CV_WINDOW_AUTOSIZE ); imshow( source_window, src ); createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback ); thresh_callback( 0, 0 ); waitKey(0); return(0); } /** @function thresh_callback */ void thresh_callback(int, void* ) { Mat canny_output; vector> contours; vector hierarchy; vector> largestContour; int largestArea = -1; int size=5; /// Detect edges using canny Canny( src_gray, canny_output, thresh, thresh*5.5, 3 ); /// Find contours findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); /// Draw contours Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 ); vector> polyContours( contours.size() ); for( int i = 0; i< contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() ); approxPolyDP( Mat(contours[i]), polyContours[i], 3, true ); } for( int i = 0; i < contours.size(); i++ ) { int area = fabs(contourArea(polyContours[i],false)); if(area > largestArea && area > size) { largestArea = area; largestContour.push_back(polyContours[i]); } } //Scalar color = Scalar( 255 ); if(largestContour.size()>0) //drawContours( drawing, largestContour, 0, color, 1, 8, vector(), 0, Point() ); /// Show in a window namedWindow( "Contours", CV_WINDOW_AUTOSIZE ); imshow( "Contours", drawing ); } Here the source and result images ![source image](/upfiles/14690758101143779.jpg) ![results](/upfiles/14690758342602113.jpg)

Viewing all articles
Browse latest Browse all 19555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>