Python | cv2 findcontours()方法

时间:2020-02-23 14:42:14  来源:igfitidea点击:

在本教程中,我们将看到如何找到所有 boundary points(x,y)使用Python Open-CV中的图像中的对象,它作为CV2(计算机视觉)库。
我们可以使用 findContours()的方法 cv2库找到图像中的对象的所有边界点(x,y)。
要使用CV2库,我们需要使用CV2库使用 import statement

轮廓可以简单地解释,作为加入所有连续点(沿边界)的曲线,具有相同的颜色或者强度。
轮廓对于形状分析和对象识别和对象检测非常有用。

Note:
For better accuracy, we must use binary images. So before finding contours, apply threshold or canny edge detection on the input image.

现在让我们看看语法和返回值 cv2 findContours()方法首先,我们将继续前进到示例。

语法

cv2.findContours(src, contour_retrieval, contours_approximation)

参数

我们需要将三个参数传递给findContours()方法。

  • src:n维数组的输入图像(n = 2,3),但优选的2-dim二进制图像,以便更好的结果。
  • contour_retrieval:这是轮廓检索模式。可能的值是:a)cv2.reetr_tree b)cv2.retr_external c)cv2.reetr_list d)cv2.reet_ccomp等。
  • contours_approximation:这是轮廓近似方法。可能的值是:a)cv2.chain_appox_none b)cv2.chain_approx_simple

返回值

它返回三个值:a)输入图像阵列b)轮廓c)层次结构

Note:
1) Contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.
2) Hierarchy is the parent-child relationship in contours. It is represented as an array of four values : [Next contour, previous contour, First child contour, Parent contour]

如果我们有兴趣阅读更多关于层次结构,那么轮廓检索模式,请单击此给定的链接层次结构和轮廓检索模式

轮廓近似方法

在上面,我们看到轮廓是具有相同强度的形状的边界。
它存储形状边界的(x,y)坐标。
但它是否存储了所有坐标?
这是由此轮廓近似方法指定的。
如果我们通过 cv2.CHAIN_APPROX_NONE,捕获所有边界点。
但实际上,我们需要所有的积分吗?
例如,如果我们需要找到直线的轮廓。
我们只需要这条线的两个端点。
在这种情况下,我们可以通过 cv2.CHAIN_APPROX_SIMPLE
它排除了所有过多的点并压缩轮廓,从而节省了内存。

cv2 findcontours()方法示例

现在让我们看看Python代码:

示例1:使用CV2.RETR_TREE作为检索模式和CV2.CHAIN_ABPOX_NONE作为轮廓近似方法。

# import computer vision library(cv2) in this code
import cv2
 
# main code
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "C:\Users\user\Desktop\rectangle.jpg"
 
    # read/load an image in grayscale mode
    image = cv2.imread(img_path,0)
 
    # show the Input image on the newly created image window
    cv2.imshow('Input',image)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    ret, bin_img = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
 
    # show the binary image on the newly created image window
    cv2.imshow('Intermediate',bin_img)
 
    # extracting the contours from the given binary image
    img,contours, hierarchy = cv2.findContours(bin_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
 
    print("Total Number of Contours found =", len(contours))
    print("contours are: \n",contours)
    print("hierarchy is: \n",hierarchy)

输出 :

Total Number of Contours found = 5
contours are:
[array([[[0, 0]],[[0, 1]],
[[0, 2]],
...,
[[3, 0]],
[[2, 0]],
[[1, 0]]], dtype=int32), array([[[202, 35]],
[[203, 34]],
[[204, 35]],
[[203, 36]]], dtype=int32), array([[[183, 35]],
[[184, 34]],
[[185, 34]],
[[186, 34]],
[[187, 34]],
[[188, 34]],
[[189, 34]],
[[190, 34]],
[[191, 34]],
[[192, 35]],
[[191, 36]],
[[190, 36]],
[[189, 36]],
[[188, 36]],
[[187, 36]],
[[186, 36]],
[[185, 36]],

示例2:使用cv2.reetr_external作为轮廓检索模式和cv2.chain_appox_simple作为轮廓近似方法。

# import computer vision library(cv2) in this code
import cv2
 
# main code
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "C:\Users\user\Desktop\rectangle.jpg"
 
    # read/load an image in grayscale mode
    image = cv2.imread(img_path,0)
 
    # show the Input image on the newly created image window
    cv2.imshow('Input',image)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    ret, bin_img = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
 
    # show the binary image on the newly created image window
    cv2.imshow('Intermediate',bin_img)
 
    # extracting the contours from the given binary image
    img,contours, hierarchy = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
    print("Total Number of Contours found =", len(contours))
 
    print("contours are: \n",contours)
    print("hierarchy is: \n",hierarchy)

输出 :

Total Number of Contours found = 1
contours are:
[array([[[ 0, 0]],[[ 0, 191]],
[[261, 191]],
[[261, 0]]], dtype=int32)]
hierarchy is:
[[[-1 -1 -1 -1]]]