查找轮廓r = cv2.drawContours(o, contours, contoursIdx, color[, thickness])
参数 :
o : 原始图像
contours : 需要绘制的边缘数组
contoursIdx : 需要绘制的边缘索引,如果全部绘制则为 -1.
color : 绘制的颜色,为BGR格式的Scalar
thickness : 可选,绘制的密度,即绘制轮廓时所用的画笔粗细
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('Penguins.jpg',2)
ori = img.copy()
#imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rows,cols = img.shape
convas = np.zeros((rows,cols))
convas[:] = 255
ret,res = cv2.threshold(img,127,255, cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(res,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
counterimg = cv2.drawContours(convas,contours,-1,(0,0,0),2)
plt.subplot(121)
plt.imshow(ori,'gray'),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(122)
plt.imshow(counterimg,'gray'),plt.title('contour',),plt.xticks([]),plt.yticks([])
plt.show()