Opencv Python 简明教程

OpenCV Python - Image Contours

轮廓是一条连接所有沿边界具有相同颜色或强度的连续点的曲线。轮廓非常适用于形状分析和对象检测。

Contour is a curve joining all the continuous points along the boundary having the same color or intensity. The contours are very useful for shape analysis and object detection.

Find Contour

在找到轮廓之前,我们应该应用阈值或 Canny 边缘检测。然后,通过使用 findContours() 方法,我们可以在二进制图像中找到轮廓。

Before finding contours, we should apply threshold or canny edge detection. Then, by using findContours() method, we can find the contours in the binary image.

使用 findContours() 函数的命令如下 −

The command for the usage of *findContours()*function is as follows −

cv.findContours(image, mode, method, contours)

Parameters

findContours() 函数的参数如下 −

The parameters of the findContours() function are as follows −

  1. image − Source, an 8-bit single-channel image.

  2. mode − Contour retrieval mode.

  3. method − Contour approximation method.

mode 参数的值枚举如下 −

The mode parameter’s values are enumerated as follows −

  1. cv.RETR_EXTERNAL − Retrieves only the extreme outer contours.

  2. cv.RETR_LIST − Retrieves all of the contours without establishing any hierarchical relationships.

  3. cv.RETR_CCOMP − Retrieves all of the contours and organizes them into a twolevel hierarchy.

  4. cv.RETR_TREE − Retrieves all of the contours and reconstructs a full hierarchy of nested contours.

另一方面,近似方法可以是从以下方法中选取一个 −

On the other hand, approximation method can be one from the following −

  1. cv.CHAIN_APPROX_NONE − Stores absolutely all the contour points.

  2. cv.CHAIN_APPROX_SIMPLE − Compresses horizontal, vertical, and diagonal segments and leaves only their end points.

Draw Contour

在检测到轮廓矢量后,使用 cv.drawContours() 函数在原始图像上绘制轮廓。

After detecting the contour vectors, contours are drawn over the original image by using the cv.drawContours() function.

cv.drawContours()函数的命令如下 −

The command for the cv.drawContours() function is as follows −

cv.drawContours(image, contours, contourIdx, color)

Parameters

drawContours() 函数的参数如下 −

The parameters of the drawContours() function are as follows −

  1. image − Destination image.

  2. contours − All the input contours. Each contour is stored as a point vector.

  3. contourIdx − Parameter indicating a contour to draw. If it is negative, all the contours are drawn.

  4. color − Color of the contours.

Example

以下代码示例中绘制轮廓的输入图像有三个填充有黑色颜色的图形。

Following code is an example of drawing contours on an input image having three shapes filled with black colours.

第一步,我们获取一个灰度图像,然后执行 Canny 边缘检测。

In the first step, we obtain a gray image and then perform the canny edge detection.

然后,我们在生成图像上调用 findContours() 函数。其结果是点矢量。然后,我们调用 drawContours() 函数。

On the resultant image, we then call findContours() function. Its result is a point vector. We then call the drawContours() function.

完整代码如下 −

The complete code is as below −

import cv2
import numpy as np

img = cv2.imread('shapes.png')
cv2.imshow('Original', img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

canny = cv2.Canny(gray, 30, 200)

contours, hierarchy = cv2.findContours(canny,
   cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print("Number of Contours = " ,len(contours))
cv2.imshow('Canny Edges', canny)

cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

cv2.imshow('Contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

原始图像在 Canny 边缘检测之后,以及绘制轮廓图像将显示在单独的窗口中,如下所示 −

The original image, after canny edge detection and one with contours drawn will be displayed in separate windows as shown here −

separate windows

执行 canny edge detection 操作后,图像如下 −

After the canny edge detection, the image will be as follows −

canny edge detection

contours are drawn 后,图像如下所示 −

After the contours are drawn, the image will be as follows −

contours drawn