我尝试的是使用以下线条绘制外部轮廓
cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(orig, cnts, -1, (0, 255, 0), 3) # this draws the external contour
引用以下内容 [![在此处输入图像描述][1]][1]
我怎样才能得到下面的答案?
[![在此处输入图像描述][2]][2]
最佳答案
我不知道它是如何在链接中解决的,但您可以使用空白蒙版,在其上绘制轮廓,然后使用cv2.dilate
使用内核大小来扩展它它们之间需要的像素数。完成后,找到蒙版上的轮廓并将第二个轮廓绘制到原始图像上。
import cv2
import numpy as np
img = cv2.imread('contour.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cnts, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, cnts, -1, (0, 255, 0), 3)
mask = np.zeros(img.shape[:2], dtype=np.uint8)
cv2.drawContours(mask, cnts, -1, 255, 1)
kernel = np.ones((100, 100), np.uint8)
mask = cv2.dilate(mask, kernel, iterations = 1)
cnts, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, cnts, 1, (255, 0, 0), 3)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
关于python - 如何使用 python 和 opencv 在图像上绘制两个轮廓,给定它们之间的轮廓距离为 100 像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60224733/