current position:Home>Python opencv contour discovery function based on image edge extraction

Python opencv contour discovery function based on image edge extraction

2022-01-30 00:48:44 Dream eraser

Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities

This article has participated in  「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .

Python OpenCV 365 Day study plan , Go into the field of image with the eraser . This blog is the third in this series 36 piece .

Basic knowledge

In the image , The outline can be simply understood as connecting all continuous points with the same color ( The border ) The curve of , Contours can be used for shape analysis and object detection 、 Identification and other fields .

The principle of contour discovery : Firstly, the target object is extracted by threshold segmentation , Then the contour of the target object is extracted by edge detection . An outline is a series of points ( Pixels ), These points form an ordered set of points .

Use cv2.findContours Function can be used to detect the edge of an image .

Function prototype description

contours, hierarchy = cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
 Copy code 

I use Python OpenCV yes 4.0 Above version , If you're using 3.0 above , There may be a return value difference problem . The parameters are described as follows :

  • image: The input image ;
  • mode: Contour retrieval mode , Please refer to the following text for details ;
  • method: Contour approximation method , For details, please refer to the following text ;
  • contours: The outline of the return ;
  • hierachy: Attributes corresponding to each contour ;
  • offset: Optional offset for each contour point movement .

remarks :image The parameter needs to be a binary graph , Not grayscale , The result is contour and hierarchy .

Contour retrieval mode , There are four kinds.

  • cv2.RETR_EXTERNAL: Indicates that only the outer contour is detected ;
  • cv2.RETR_LIST: Detected contour , Do not establish hierarchical relationships ;
  • cv2.RETR_CCOMP: Build two levels of outline , The upper layer is the outer boundary , The inner layer is the boundary information of the inner hole . If there is a connected object in the inner hole , The boundary of this object is also at the top ;
  • cv2.RETR_TREE: Create a hierarchical tree structure outline .
  • Above , Can be found on this website : Official website address

Contour approximation method

  • cv2.CHAIN_APPROX_NONE: Store all contour points , The pixel position difference between two adjacent points shall not exceed 1, namely max(abs(x1-x2),abs(y2-y1))==1, It's not used in general ;
  • cv2.CHAIN_APPROX_SIMPLE: Compress the horizontal direction , vertical direction , Diagonal elements , Only the coordinates of the end point in this direction are reserved , For example, a rectangular outline only needs 4 Points to save profile information .
  • cv2.CHAIN_APPROX_TC89_L1,cv2.CV_CHAIN_APPROX_TC89_KCOS: Use teh-Chinl chain The approximate algorithm ( Didn't look for information to learn ).

After understanding the above , You can apply the contour discovery function , The code is as follows :

import cv2 as cv

src = cv.imread("./both.jpeg")

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 150, 255, 0)
cv.imshow("thresh",thresh)
#  Look for the outline 
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
 Copy code 

After the contour is found , And through cv2.drawContours Function to draw the outline , The prototype of this function is as follows :

image = cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]])
 Copy code 

The parameters are described as follows :

  • image: The input image ;
  • contours: outline , stay Python There is a list, Namely cv2.findContours The point set found by the function , A list of ;
  • contourIdx: Index of contour , Specifies the sketch profile list Which outline in the , To draw all the outlines , Please deliver -1;
  • color: Color ;
  • thickness: The thickness of the , If it is -1, Indicates filling ;
  • lineType: Linetype ;
  • hierarchy: Optional information for the hierarchy ;
  • maxLevel: The maximum level at which the outline is drawn ,0: Draw only the specified profile ,1: Draw the profile and all nested profiles ,2: Draw the outline , All nested profiles , All nested to nested contours ;
  • offset: Contour offset parameters .

The test code and running results are as follows :

import cv2 as cv
# help(cv.drawContours)
src = cv.imread("./both.jpeg")

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 150, 255, 0)
cv.imshow("thresh",thresh)
#  Look for the outline 
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

# print(contours)
# print(hierarchy)
#  Draw the outline 
cv.drawContours(src,contours,-1,(200,0,150),2)

cv.imshow('src',src)
cv.waitKey(0)
 Copy code 

Python OpenCV  Contour detection function based on image edge extraction

copyright notice
author[Dream eraser],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300048418621.html

Random recommended