Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
Line 30... Line 30...
30
30
31
        cv::namedWindow("Binary Image");
31
        cv::namedWindow("Binary Image");
32
        cv::imshow("Binary Image",image);
32
        cv::imshow("Binary Image",image);
33
33
34
        // Get the contours of the connected components
34
        // Get the contours of the connected components
35
        std::vector<std::vector<cv::Point>> contours;
35
        std::vector<std::vector<cv::Point> > contours;
36
        cv::findContours(image,
36
        cv::findContours(image,
37
                contours, // a vector of contours 
37
                contours, // a vector of contours 
38
                CV_RETR_EXTERNAL, // retrieve the external contours
38
                CV_RETR_EXTERNAL, // retrieve the external contours
39
                CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
39
                CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
40
40
41
        // Print contours' length
41
        // Print contours' length
42
        std::cout << "Contours: " << contours.size() << std::endl;
42
        std::cout << "Contours: " << contours.size() << std::endl;
43
        std::vector<std::vector<cv::Point>>::const_iterator itContours= contours.begin();
43
        std::vector<std::vector<cv::Point> >::const_iterator itContours= contours.begin();
44
        for ( ; itContours!=contours.end(); ++itContours) {
44
        for ( ; itContours!=contours.end(); ++itContours) {
45
45
46
                std::cout << "Size: " << itContours->size() << std::endl;
46
                std::cout << "Size: " << itContours->size() << std::endl;
47
        }
47
        }
48
48
Line 57... Line 57...
57
        cv::imshow("Contours",result);
57
        cv::imshow("Contours",result);
58
58
59
        // Eliminate too short or too long contours
59
        // Eliminate too short or too long contours
60
        int cmin= 100;  // minimum contour length
60
        int cmin= 100;  // minimum contour length
61
        int cmax= 1000; // maximum contour length
61
        int cmax= 1000; // maximum contour length
62
        std::vector<std::vector<cv::Point>>::const_iterator itc= contours.begin();
62
        std::vector<std::vector<cv::Point> >::const_iterator itc= contours.begin();
63
        while (itc!=contours.end()) {
63
        while (itc!=contours.end()) {
64
64
65
                if (itc->size() < cmin || itc->size() > cmax)
65
                if (itc->size() < cmin || itc->size() > cmax)
66
                        itc= contours.erase(itc);
66
                        itc= contours.erase(itc);
67
                else
67
                else
Line 164... Line 164...
164
        cv::namedWindow("All Contours");
164
        cv::namedWindow("All Contours");
165
        cv::imshow("All Contours",result);
165
        cv::imshow("All Contours",result);
166
166
167
        cv::waitKey();
167
        cv::waitKey();
168
        return 0;
168
        return 0;
169
}
-
 
170
169
}
-
 
170