我想从图像中获得轮廓,并在黑色图像上仅显示填充轮廓
我的代码:
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('sample.jpeg')
black_img = np.zeros(img.shape)
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,tresh = cv2.threshold(imgray,127,255,0)
contours,hierarchy = cv2.findContours(tresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cv2.drawContours(black_img,contours,-1,(0,255,0),3)
plt.imshow(black_img)
plt.show()
这是sample.jpeg
没有给我预期的输出,但是黑色的img
我该怎么做
我认为您希望在Python/OpenCV中执行的操作可能是:
输入:
结果:
关于
首先
img2
来自哪里?我预计会爆炸,但这不是你当时展示的图像。您可以在img
的顶部绘制,然后显示它。或者您可以尝试在black_img
上绘制等高线相关问题 更多 >
编程相关推荐