我似乎找不到一种方法来画出不止一个物体的轮廓。在
输入图像:
代码:
import cv2
import numpy as np
#import image
img = cv2.imread('img.png', 0)
#Thresh
ret, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
#Finding the contours in the image
_, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#Convert img to RGB and draw contour
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
cv2.drawContours(img, contours, 0, (0,0,255), 2)
#save output img
cv2.imwrite('output_img.png', img)
输出:
只绘制较大的对象轮廓。我怎么画两个轮廓?在
将drawContours中的第三个参数更改为-1(第三个参数是等高线的索引),这将绘制图像中的所有轮廓:
如果只想绘制两个轮廓,并且前两个轮廓是白色对象,请使用:
^{pr2}$相关问题 更多 >
编程相关推荐