当前位置:网站首页>Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv

Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv

2022-07-23 18:40:00 biter0088

Catalog

1. findContours function

1.1. outline contours

1.2 cv2.findContours、cv::findContours Function introduction

1.3 Parameter description :

2. opencv Various versions cv2.findContours explain

2.1  opencv3.x

2.2  opencv2.x and 4.x

3  cv2.findContours Examples of use :

3.1 opencv3.x edition

4. Reference link


1. findContours function

1.1. outline contours

         A contour can be simply interpreted as connecting all continuous points ( Along the border ) The curve of , Having the same color or intensity . Contour is a useful tool for shape analysis and object detection and recognition .

· For better accuracy , Please use Binary image . So before looking for the outline , Apply threshold or precise edge detection .

·findContours Function to modify the source image . therefore , If you still want the source image after finding the contour , Please store it in some other variables .

· stay OpenCV in , Looking for outlines is like looking for white objects against a black background . So remember , The object to be found should be white , The background should be black . If there are other pretreatment methods , Just contrast the area where the contour is located with other areas as far as possible

1.2 cv2.findContours、cv::findContours Function introduction

function :

         Looking for contours in binary images .
         This function retrieves the contour from the binary image . Contour is a useful tool for shape analysis and object detection and recognition .

matters needing attention :

         The source image is modified by this function .

         Besides , This function does not consider the 1 Pixel boundary ( It USES 0 Fill in and use it for neighborhood analysis in the algorithm ), Therefore, the outline touching the image boundary will be clipped .

c++ Construction form 1:

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

c++ Construction form 2:

void cv::findContours 	( 	InputOutputArray  	image,
		OutputArrayOfArrays  	contours,
		int  	mode,
		int  	method,
		Point  	offset = Point() 
	) 	

1.3 Parameter description :

image:

         The source image ,8 Bit single channel image . Non zero pixels are considered 1. Zero pixels remain 0, Therefore, the image is regarded as binary . You can use compare 、 inRange 、 threshold 、adaptiveThreshold 、 Canny etc. Create a binary image from a grayscale or color image . This function modifies the image while extracting the contour . If mode be equal to RETR_CCOMP or RETR_FLOODFILL, Then the input can also be a label 32 Bit integer image (CV_32SC1).

contours:

         Detected contours . Each contour is stored as a point vector .

hierarchy:

         Optional output vector , Contains information about the image topology . It has as many elements as the number of contours . For every second i A profile contours[i] , Elements hierarchy[i][0] 、 hiearchy[i][1] 、 hiearchy[i][2] and hiearchy[i][3] Set to 0- Index in the contour based on the next and previous contour of the same level , They are the first sub contour and the parent contour . If the contour i There's no next one 、 the previous 、 Parent or nested profiles , be hierarchy[i] The corresponding element of will be negative .

mode:

         Contour retrieval mode , see cv::RetrievalModes

method:

         Contour approximation method , see cv::ContourApproximationModes

offset:

         Optional offset for each contour point movement . If from the image ROI Extract contour from , Then it should be analyzed in the whole image context , It will be useful .

2. opencv Various versions cv2.findContours explain

2.1  opencv3.x

image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

         Three parameters , Note the order of the parameters

2.2  opencv2.x and 4.x

contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

         Two parameters , Note the order of the parameters

3  cv2.findContours Examples of use :

3.1 opencv3.x edition

The test image :test.png, Two pictures , The outline in the left figure will not be found, resulting in an error in the program , The right picture can be found

python Code :

# -*- coding:utf-8 -*-
import numpy as np
import cv2
im = cv2.imread('test2.png') ## Read in color picture , Used for the following display rectangle 
cv2.imshow('im', im) ##hxz
cv2.waitKey(0)##hxz
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# Find the largest outline 
area = []
for k in range(len(contours)):
    area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))

rect = cv2.minAreaRect(contours[max_idx])
points = cv2.boxPoints(rect) # Get the coordinates of the four points of the smallest circumscribed rectangle 
points = np.int0(points) # Coordinate values are rounded 

image_show = cv2.drawContours(im, [points], 0, (0 ,0 , 255), 4) ##hxz  Red is more obvious     

cv2.imshow('image_show', image_show) ##hxz
cv2.waitKey(0)##hxz

Program execution effect

4. Reference link

opencv3.2 Contours Official documents : OpenCV: Contours : Getting Started

Change the arrow in the following figure to view other opencv The content of the version

staclflow About cv2.findContours Return the answer with an error :

python - compatibility issue with contourArea in openCV 3 - Stack Overflow

cv2.findContours Function description   OpenCV: Structural Analysis and Shape Descriptors

原网站

版权声明
本文为[biter0088]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231618507797.html