【发布时间】:2018-01-08 19:40:26
【问题描述】:
我有一个函数可以或多或少地侵蚀某些轮廓,具体取决于它们的面积大小。但是,一旦它们被裁剪,它们就会丢失与原始图像相对应的正确坐标数据。
如何在保持原始位置的同时将eroded_contours 重绘为原始图像?或者有没有更好的方法来使用基于轮廓区域大小的自定义侵蚀?
edged = cv2.Canny(original_image.copy(), 50, 200)
contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
def show_each_contour(original_image):
for i,c in enumerate(contours):
area = cv2.contourArea(c)
if area > 0:
rect = cv2.boundingRect(c)
x,y,w,h = rect
start_row, start_col = int(x), int(y)
end_row, end_col = int(x+x+w), int(y+y+h)
cv2.rectangle(original_image, (x,y), (x+w,y+h), (0,0,255), 2)
cropped = original_image[y:y+h, x:x+w]
if area < 2000:
kernel = np.ones((5,5), np.uint8)
numberOfIterations = area / 200
else:
kernel = np.ones((5,5), np.uint8)
numberOfIterations = 7
eroded_contours = cv2.erode(cropped.copy(), kernel, iterations = int(numberOfIterations))
#This won't work correctly because the coordinates are different now
#cv2.drawContours(original_image, eroded_contours , -1, (0,255,255), 3)
【问题讨论】:
标签: python opencv numpy contour area