首页 > 代码库 > OpenCV Tutorials —— Finding contours in your image

OpenCV Tutorials —— Finding contours in your image

void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, intmethod, Point offset=Point())

Parameters:

  • image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary . You can use & matches, bool addQuery , const Mat& mask)">compare() , inRange() , threshold(), adaptiveThreshold() , Canny() , and others to create a binary image out of a grayscale or color one. The function modifies the image while extracting the contours.  必须是二值图像

 

  • contours – Detected contours. Each contour is stored as a vector of points.最终的轮廓被作为点的集合

 

  • hierarchy – Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elementshierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.  可选输出,关于图像的拓扑包含关系

 

  • mode

    Contour retrieval mode (if you use Python see also a note below).

    • CV_RETR_EXTERNAL retrieves only the extreme outer contours. It sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours.  只恢复最外面的轮廓
    • CV_RETR_LIST retrieves all of the contours without establishing any hierarchical relationships.  恢复所有轮廓,不建立层级结构
    • CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.  恢复所有轮廓,建立二层结构
    • CV_RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours. This full hierarchy is built and shown in the OpenCV contours.c demo. 恢复所有轮廓,全部层次结构
  • method

    Contour approximation method (if you use Python see also a note below).

    • CV_CHAIN_APPROX_NONE stores absolutely all the contour points. That is, any 2 subsequent points(x1,y1) and (x2,y2) of the contour will be either horizontal, vertical or diagonal neighbors, that is, max(abs(x1-x2),abs(y2-y1))==1.
    • CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points.
    • CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS applies one of the flavors of the Teh-Chin chain approximation algorithm. See [TehChin89] for details.
  • offset – Optional offset by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.

 

void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar& color, int thickness=1, int lineType=8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point() )

Parameters:

  • image – Destination image.
  • contours – All the input contours. Each contour is stored as a point vector.
  • contourIdx – Parameter indicating a contour to draw. If it is negative, all the contours are drawn.
  • color – Color of the contours.
  • thickness – Thickness of lines the contours are drawn with. If it is negative (for example,thickness=CV_FILLED ), the contour interiors are drawn.
  • lineType – Line connectivity. See line() for details.
  • hierarchy – Optional information about hierarchy. It is only needed if you want to draw only some of the contours (see maxLevel ).
  • maxLevel – Maximal level for drawn contours. If it is 0, only the specified contour is drawn. If it is 1, the function draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, all the nested contours, all the nested-to-nested contours, and so on. This parameter is only taken into account when there is hierarchy available.
  • offset – Optional contour shift parameter. Shift all the drawn contours by the specified\texttt{offset}=(dx,dy) .
  • contour – Pointer to the first contour.
  • externalColor – Color of external contours.
  • holeColor – Color of internal contours (holes).

 

Code

#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>#include <stdio.h>#include <stdlib.h>using namespace cv;using namespace std;Mat src; Mat src_gray;int thresh = 100;int max_thresh = 255;RNG rng(12345);/// Function headervoid thresh_callback(int, void* );/** @function main */int main( int argc, char** argv ){  /// Load source image and convert it to gray  src = http://www.mamicode.com/imread( argv[1], 1 );"Source";  namedWindow( source_window, CV_WINDOW_AUTOSIZE );  imshow( source_window, src );  createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );  thresh_callback( 0, 0 );  waitKey(0);  return(0);}/** @function thresh_callback */void thresh_callback(int, void* ){  Mat canny_output;  vector<vector<Point> > contours;  vector<Vec4i> hierarchy;  /// Detect edges using canny  Canny( src_gray, canny_output, thresh, thresh*2, 3 );  /// Find contours  findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );  /// Draw contours  Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );  for( int i = 0; i< contours.size(); i++ )     {       Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );       drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );     }  /// Show in a window  namedWindow( "Contours", CV_WINDOW_AUTOSIZE );  imshow( "Contours", drawing );}

OpenCV Tutorials —— Finding contours in your image