#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespace std; using namespace cv; Mat src, dst; const char* output_win = "findcontours-demo"; int threshold_value = 100; int threshold_max = 255; RNG rng; void Demo_Contours(int, void*); int main(int argc, char** argv) { src = imread("L:/12.jpg"); if (src.empty()) { printf("could not load image...\n"); return -1; } namedWindow("input-image", CV_WINDOW_AUTOSIZE); namedWindow("output_win", CV_WINDOW_AUTOSIZE); imshow("input-image", src); cvtColor(src, src, CV_BGR2GRAY); const char* trackbar_title = "Threshold Value:"; createTrackbar (trackbar_title, output_win, & threshold_value, threshold_max, Demo_Contours); Demo_Contours ( 0 , 0 ); waitKey ( 0 ); return 0 ; } void Demo_Contours ( int , void * ) { Mat canny_output; Vector <Vector <>> Contours Point ; // defined below using a two-dimensional array array of FIG Vector <Vec4i> hierachy; // array dimensional integer the Canny (the src, canny_output, threshold_value, threshold_value * 2 , . 3 ,to false ); // the Canny edge detector, the threshold value findContours (canny_output, Contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point ( 0 , 0 )); // findContours Parameters: // 1. 2. The input image is detected by all the contour of the object ( array) 3. 4. contour topology discovery methods 6. The default mode P (0,0) is not displaced dst = Mat :: zeros (src.size (), CV_8UC3); // initialize src dst is a size zero matrix of the RNG RNG ( 12345 ); for (size_t I = 0 ; I <contours.size (); I ++ ) { the Scalar Color = the Scalar (rng.uniform ( 0 , 255 ), rng.uniform ( 0 , 255), Rng.uniform ( 0 , 255 )); // for each segment of the outline contours take a random color drawContours (DST, contours, I, Color, . 1 , . 8 , hierachy, 0 , Point ( 0 , 0 )); / / drawContours parameters: // 1. 2. contour output image of contours 3. 4. several contour line type color 5. 6. 7. width topology // 8.0 1 represents a drawing showing the current drawing and the current 9. the displacement profile contour embedded } imshow (output_win, DST); }
result:
Code involves the use of an array of methods:
vector <vector <Point >> contours; // define a two-dimensional array using the array shown in the graph
vector <Vec4i> hierachy; four-dimensional array of integers //
The two arrays of findContours the second and third parameters;
vector <vector <Point >>: // Find and general profile contouring using parameter contours
vector vessel which put a vector container, put some sub-container
vector <V ec 4 i>: put a 4-dimensional vector // int drawn generally used to find the contour and profile parameters hierarchy
vector <R e ct>: width * height from the pixel position (x * y)
vector <R otated R ec t>: FIG three members