OPENCV2 Series Learning Note 10 (extract connected area contour) Another

Source: Internet
Author: User

http://blog.csdn.net/lu597203933/article/details/17362457

A connected region is a shape that consists of connected pixels in a two-value image. The concept of inner and outer contour and how to extract the contour of binary image in opencv1 see my blog: http://blog.csdn.net/lu597203933/article/details/14489225

The simple extraction algorithm of contour is as follows:

Systematically scans the image until it encounters a point in the connected area, takes it as the starting point, tracks its outline, and marks the pixels on the boundary. When the contour is completely closed, the scan goes back to the previous position until the new ingredient is discovered again.

Code:

[CPP]View PlainCopyprint?
  1. #include <iostream>
  2. #include <opencv2\core\core.hpp>
  3. #include <opencv2\highgui\highgui.hpp>
  4. #include <opencv2\imgproc\imgproc.hpp>
  5. Using namespace std;
  6. Using namespace CV;
  7. Remove too small or too large outlines
  8. void Getsizecontours (vector<vector<point>> &contours)
  9. {
  10. int cmin = 100; //min. Profile length
  11. int cmax = 1000; //Maximum profile length
  12. Vector<vector<point>>::const_iterator ITC = Contours.begin ();
  13. While (itc! = Contours.end ())
  14. {
  15. if ((Itc->size ()) < Cmin | | (Itc->size ()) > Cmax)
  16. {
  17. ITC = Contours.erase (ITC);
  18. }
  19. else + + itc;
  20. }
  21. }
  22. Calculates the contour of the connected area, which is the shape of the connected pixels in the two-value image
  23. int main ()
  24. {
  25. Mat image = Imread ("E:\\opencv2cv\\lesson7\\debug\\55.png", 0);
  26. if (!image.data)
  27. {
  28. cout << "Fail to load image" << Endl;
  29. return 0;
  30. }
  31. Mat Imageshold;
  32. Threshold (image, Imageshold, 255, thresh_binary); //Must be binary
  33. Vector<vector<point>> contours;
  34. //cv_chain_approx_none get each pixel point per contour
  35. Findcontours (Imageshold, contours, Cv_retr_ccomp, Cv_chain_approx_none, Cvpoint (0,0));
  36. Getsizecontours (contours);
  37. cout << contours.size () << Endl;
  38. Mat result (Image.size (), cv_8u, Scalar (255));
  39. Drawcontours (result, contours,-1, Scalar (0), 2); //-1 means all profiles
  40. Namedwindow ("result");
  41. Imshow ("result", result);
  42. Namedwindow ("image");
  43. Imshow ("image", image);
  44. Waitkey (0);
  45. return 0;
  46. }

Results:

Before most small outlines have been removed:

After removal:

OPENCV2 Series Learning Note 10 (extract connected area contour) Another

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.