图像轮廓是什么?
轮廓是一系列相连的点组成的曲线,代表了物体的基本外形。
谈起轮廓不免想到边缘,它们确实很像。简单的说,轮廓是连续的,边缘并不全都连续。
其实边缘主要是作为图像的特征使用,比如可以用边缘特征可以区别脸和手;而轮廓主要用来分析物体的形态,比如物体的周长和面积等,可以说边缘包括轮廓。
绘制图像轮廓
import cv2
import numpy as np
img = cv2.imread(‘shape.jpg’)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,220,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,
cv2.CHAIN_APPROX_NONE)
n=len(contours)
contoursImg=[]
for i in range(n):
temp=np.zeros(img.shape,np.uint8)
contoursImg.append(temp)
contoursImg[i]=cv2.drawContours(contoursImg[i],contours,i,(255,255,255), 3)
cv2.imshow(“contours[” + str(i)+"]",contoursImg[i])
cv2.waitKey()
cv2.destroyAllWindows()
结果