我正在尝试在图像上绘制多个轮廓,到目前为止,我已经设法通过应用不同的阈值来绘制轮廓。唯一的问题是,大多数轮廓区域是重叠的,因此我在这里讨论如何处理它。我理想地希望的是,每当有重叠时,都应将轮廓分成各个区域。例如,与Conceptual image中一样,有4个区域(轮廓)为橙色,绿色,蓝色和黑色。每当有重叠时,都应分为紫色区域。似乎很棘手,我什至不确定是否可行。如果没有,我希望所有重叠部分合并。谁能帮助解决这个问题? Sample image
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
im = cv.imread('images/sample.jpg')
imgray = cv.cvtColor(im,cv.COLOR_BGR2GRAY)
ret1,thresh1 = cv.threshold(imgray,30,80,0)
ret2,thresh2 = cv.threshold(imgray,110,0)
ret3,thresh3 = cv.threshold(imgray,150,0)
ret4,thresh4 = cv.threshold(imgray,200,0)
ret5,thresh5 = cv.threshold(imgray,255,0)
_,contours1,hierarchy1 = cv.findContours(thresh1,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
_,contours2,hierarchy2 = cv2.findContours(thresh2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
_,contours3,hierarchy3 = cv2.findContours(thresh3,contours4,hierarchy4 = cv2.findContours(thresh4,contours5,hierarchy5 = cv2.findContours(thresh5,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,-1,(0,255),1)
cv2.drawContours(im,0),(10,200),(255,1)
cv2.imshow("im",im)
cv2.waitKey(0)