OpenCV之轮廓(Contours)相关函数的使用(1)

第一部分:Contours: Getting Started (轮廓基础)

1、What are contours?

轮廓就是一条连接所有连续点的曲线,在图像中就是连续像素的曲线。OpenCV中提供了一系列的方法,其中包括怎样找轮廓、画轮廓等函数。

函数cv2.findContours()

功能:通过二进制图像发现轮廓

参考说明

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

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.

mode        Contour retrieval mode

method    Contour approximation method

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.

# -*- coding: utf-8 -*-
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import io,data,color
img = cv2.imread('../../../image/messi.jpg')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #将图像转为灰度,发现函数适用于黑白之间的发现
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE # 调用发现轮廓的函数
print(contours) # 轮廓点集合
print(hierarchy) # 层级图集合
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(im2),plt.title('Output')
# plt.colorbar(orientation='horizontal')
# plt.show()
cv2.imwrite('d:/messi_contours.jpg', im2) #将图片保存到本地

2、How to draw the contours?

函数cv2.drawContours()

功能:绘制轮廓或者填充轮廓

参数说明

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.

lineType   Line connectivity. See cv::LineTypes.

hierarchy Optional information about hierarchy.

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 offset=(dx,dy) .

注意点:

  • To draw all the contours in an image:

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

  • To draw an individual contour, say 4th contour:

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

  • But most of the time, below method will be useful:

cnt = contours[4]

cv2.drawContours(img, [cnt], 0, (0,255,0), 3)

 

3、Contour Approximation Method

在函数cv2.findContours中,在表示画轮廓的方法中,怎样表示这些像素点,其中OpenCV提供了很多方法,其中有存储所有像素点的集合,有只存储特征点的集合。

cv2.CHAIN_APPROX_NONE:存储所有轮廓点的集合

cv2.CHAIN_APPROX_SIMPLE:存储特征点,去除冗余的部分,比如直线,就只存储两个点就可以了(两点成直线)。

 

4、Reference Material

资料1(findContours):

http://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a

资料2(drawContours):

http://docs.opencv.org/3.3.0/d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc

资料3:

http://docs.opencv.org/3.3.0/d4/d73/tutorial_py_contours_begin.html

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: