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?
- #include <iostream>
- #include <opencv2\core\core.hpp>
- #include <opencv2\highgui\highgui.hpp>
- #include <opencv2\imgproc\imgproc.hpp>
- Using namespace std;
- Using namespace CV;
- Remove too small or too large outlines
- void Getsizecontours (vector<vector<point>> &contours)
- {
- int cmin = 100; //min. Profile length
- int cmax = 1000; //Maximum profile length
- Vector<vector<point>>::const_iterator ITC = Contours.begin ();
- While (itc! = Contours.end ())
- {
- if ((Itc->size ()) < Cmin | | (Itc->size ()) > Cmax)
- {
- ITC = Contours.erase (ITC);
- }
- else + + itc;
- }
- }
- Calculates the contour of the connected area, which is the shape of the connected pixels in the two-value image
- int main ()
- {
- Mat image = Imread ("E:\\opencv2cv\\lesson7\\debug\\55.png", 0);
- if (!image.data)
- {
- cout << "Fail to load image" << Endl;
- return 0;
- }
- Mat Imageshold;
- Threshold (image, Imageshold, 255, thresh_binary); //Must be binary
- Vector<vector<point>> contours;
- //cv_chain_approx_none get each pixel point per contour
- Findcontours (Imageshold, contours, Cv_retr_ccomp, Cv_chain_approx_none, Cvpoint (0,0));
- Getsizecontours (contours);
- cout << contours.size () << Endl;
- Mat result (Image.size (), cv_8u, Scalar (255));
- Drawcontours (result, contours,-1, Scalar (0), 2); //-1 means all profiles
- Namedwindow ("result");
- Imshow ("result", result);
- Namedwindow ("image");
- Imshow ("image", image);
- Waitkey (0);
- return 0;
- }
Results:
Before most small outlines have been removed:
After removal:
OPENCV2 Series Learning Note 10 (extract connected area contour) Another