Long time no update, forgive yourself put a fake recently in a hurry, so ... The updated content is very close to the eighth chapter, because the most recent work to jump first, the following will update the note number ... Come on, come on!
Finding outlines in a binary image
void cv::findcontours ( inputoutputarray image, outputarrayofarrays contours, outputarray hierarchy, int mode, int method , point = Point () )
- Image: Input images, 8-bit single-channel image, image not 0 pixels as 1. can be created with functions such as compare (), Imrange (), Threshold (), adaptivethreshold (), canny (), note: This function modifies the image content while extracting the image outline.
- If mode equals to Retr_ccomp or Retr_floodfill, the input can also is a 32-bit integer image of Labels (CV_32SC1).
- Contours: The detected contour, each of which is stored as a point vector, that is, a vector of type points, such as type vector<vector<point> >.
- Hierarchy: An optional output vector that contains the topology information for the image. Each contour contours[i],
- Hierarchy[i][0], after a contour,
- HIERARCHY[I][1], the previous contour,
- HIERARCHY[I][2], parent outline,
- HIERARCHY[I][3], the index number of the inline contour.
- If there is no corresponding entry, the corresponding entry in Hierarchy[i] is set to a negative number.
- Mode: Retrieval modes, optional modes include
- Retr_external: Only the outermost wheel expansion is monitored. HIERARCHY[I][2] = hierarchy[i][3] = 1
- Retr_list: Extracts all the outlines and places them in the list. The detected contour does not establish a hierarchy relationship.
- Retr_ccomp: Extracts all the contours and organizes them into two-layer structure, the top layer is the peripheral boundary of the Unicom domain, and the secondary layer is the empty inner boundary.
- Retr_tree: Extracts all contours and re-establishes the mesh contour structure.
- Method: Approximate method of contour, including
- Chain_approx_none: Gets each pixel of each contour, adjacent two pixel position difference not more than 1,max (ABS (X1-X2), ABS (y1-y2)) = = 1
- Chain_approx_simple: Compress horizontal, vertical, diagonal elements, leaving only the end coordinates of that direction
- Chain_approx_tc89_li/chain_approx_tc89_kcos: Using a TEH-CHINL chain approximation algorithm
- [135] C-h Teh and Roland T. Chin. On the detection of dominant points on digital curves. Pattern analysis and Machine Intelligence, IEEE transactions on, 11 (8): 859–872, 1989.
- Offset: An optional offset for each contour point, the default points () that can be used when the contour found in the ROI image needs to be analyzed throughout the graph.
Drawing outlines
voidCV::d rawcontours (inputoutputarray image, Inputarrayofarrays contours, intContouridx,ConstScalar &Color,intThickness =1, intLinetype =line_8, Inputarray hierarchy=Noarray (),intMaxlevel =Int_max, point offset=Point ())
- Image: Target Images
- Contours: input contour, each contour stored as a point vector
- CONTOURIDX: The number of outlines that need to be drawn, if negative, to draw all outlines
- Color: Outline Color
- Thickness: Contour line thickness, if negative (such as thickness==cv_filled), drawn inside the contour
- Linetype: Line type
- 8:8 Connected Linetype
- 4:4 Connected Linetype
- Line_aa (OPENCV2:CV_AA): Anti-aliasing style
- Hierarchy: Optional Hierarchy
- Maxlevel: Maximum level for drawing outlines
- Offset: Optional Profile offset parameter
Case Procedure 1
#include <opencv2/opencv.hpp>#include<opencv2/highgui/highgui.hpp>#include<vector>//MainintMainintargcChar**argv) { //Loading ImageCv::mat srcimage = Cv::imread ("1.jpg",0); Imshow ("Original Image", Srcimage); //Initialize result imageCv::mat Dstimage =Cv::mat::zeros (Srcimage.rows, Srcimage.cols, CV_8UC3); //thresholding ImageSrcimage = srcimage >119; Imshow ("thresholding Image", Srcimage); //Finding contoursSTD::VECTOR<STD::VECTOR<CV::P oint> >contours; Std::vector<cv::Vec4i>hierarchy; //For OpenCV 2//cv::findcontours (srcimage, contours, hierarchy, Cv_retr_ccomp, cv_chain_approx_simple); //For OpenCV 3cv::findcontours (srcimage, contours, hierarchy, Cv::retr_ccomp, cv::chain_approx_simple); //iterate through all levels, and draw contours in random color intindex =0; for(; index>=0; index = hierarchy[index][0]) {cv::scalar color (rand ()&255, Rand () &255, Rand () &255); //For OpenCV 2//CV::d rawcontours (dstimage, contours, index, color, cv_filled, 8, hierarchy); //For OpenCV 3CV::d rawcontours (dstimage, contours, index, color, cv::filled,8, hierarchy); Imshow ("Contours", Dstimage); Cv::waitkey ( Max); } cv::imwrite ("result.jpg", Dstimage); return 0;}
1.jpg
Result.jpg
Case Procedure 2
#include <opencv2/opencv.hpp>#include<opencv2/highgui/highgui.hpp>#include<iostream>#include<vector>#defineWindow_name1 "Original image"#defineWindow_name2 "Contours"//Global VariablesCv::mat g_srcimage;cv::mat G_grayimage;cv::mat g_cannymat_output;intG_nthresh = the;intG_nthresh_max =255; cv::rng g_rng (12345); Std::vector<STD::VECTOR<CV::P oint> >G_vcontours;std::vector<cv::Vec4i>G_vhierarchy;//functionsvoidOn_threshchange (int,void*);//MainintMainintargcChar**argv) { //Change the text color of the consoleSystem"Color 1F"); //Loading ImageG_srcimage = Cv::imread ("1.jpg",1); if(!g_srcimage.data) {Std::cerr<<"ERROR while loading image."<<Std::endl; return false; } //convert to Gray-scale and BlurCv::cvtcolor (G_srcimage, G_grayimage, Cv::color_bgr2gray); Cv::blur (G_grayimage, G_grayimage, Cv::size (3,3)); //Create WindowCv::namedwindow (window_name1, cv::window_autosize); Imshow (window_name1, g_srcimage); //Create tracker BarCv::createtrackbar ("Canny Threshold", Window_name1, &G_nthresh, G_nthresh_max, On_threshchange); On_threshchange (0,0); Cv::waitkey (0); return 0;}voidOn_threshchange (int,void*) {Cv::canny (G_grayimage, G_cannymat_output, G_nthresh, G_nthresh*2,3); Cv::findcontours (G_cannymat_output, G_vcontours, G_vhierarchy, Cv::retr_tree, cv::chain_approx_simple); Cv::mat Drawing=Cv::mat::zeros (G_cannymat_output.size (), CV_8UC3); for(inti =0; I<g_vcontours.size (); i++) {Cv::scalar color (g_rng.uniform (0,255), G_rng.uniform (0,255), G_rng.uniform (0,255)); CV::d rawcontours (drawing, g_vcontours, I, Color,2,8, G_vhierarchy); } imshow (Window_name2, Drawing);}
Result diagram:
Find and draw outlines [OpenCV notes XX]