Opencv-image Components

Source: Internet
Author: User
# Include "opencv2/highgui. HPP "# include" opencv2/imgproc. HPP "# include <iostream> # include <stdio. h> # include <stdlib. h> using namespace CV; using namespace STD; mat src; MAT src_gray; int thresh = 100; int max_thresh = 255; RNG (12345 ); // function declaration void thresh_callback (INT, void *); int main (INT argc, char ** argv) {// load the color image, convert to a grayscale image and blur src = imread (argv [1], 1); cvtcolor (SRC, src_gray, cv_bgr2gray); blur (src_gray, src_gray, size (3, 3 )); // create a window char * source_window = "Source"; namedwindow (source_window, cv_window_autosize); imshow (source_window, Src); createtrackbar ("kan thresh:", "Source ", & Thresh, max_thresh, thresh_callback); thresh_callback (0, 0); waitkey (0); Return (0);} void thresh_callback (INT, void *) {mat canny_output; vector <vector <point> contours; vector <vec4i> hierarchy; // The CTR (src_gray, canny_output, thresh, thresh * 2, 3 ); // search for the image contour findcontours (canny_output, contours, hierarchy, cv_retr_tree, cv_chain_approx_simple, point (0, 0); // obtain the component vector <moments> Mu (contours. size (); For (INT I = 0; I <contours. size (); I ++) {Mu [I] = moments (ments s [I], false);} // obtain the mass center vector <point2f> MC (contours. size (); For (INT I = 0; I <contours. size (); I ++) {Mc [I] = point2f (MU [I]. m10/mu [I]. m00, Mu [I]. m01/mu [I]. m00);} // draw the contour mat drawing = mat: zeros (canny_output.size (), cv_8uc3); 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 (); Circle (drawing, MC [I], 4, color,-1, 8, 0);} // display the result namedwindow ("s", cv_window_autosize); imshow ("contours", drawing ); // calculate the area with the moments 00 and compare with the result of the opencv function printf ("\ t info: area and contour length \ n "); for (INT I = 0; I <contours. size (); I ++) {printf ("* contour [% d]-area (m_00) = %. 2f-area opencv: %. 2f-length: %. 2f \ n ", I, Mu [I]. m00, metrics area (S [I]), arclength (contours [I], true); 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 (); Circle (drawing, MC [I], 4, color,-1, 8, 0 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.