重叠轮廓

我正在尝试在图像上绘制多个轮廓,到目前为止,我已经设法通过应用不同的阈值来绘制轮廓。唯一的问题是,大多数轮廓区域是重叠的,因此我在这里讨论如何处理它。我理想地希望的是,每当有重叠时,都应将轮廓分成各个区域。例如,与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)
rockywell 回答:重叠轮廓

如前所述,从轮廓生成遮罩,然后从原始遮罩计算成对的相交以及“排他”部分,肯定会为您提供所需的区域,但是这种方法往往会很昂贵也一样从您的示例图像和代码中,我无法弄清您实际上想要做什么,因此我坚持使用一些非常基本的示例来说明这种方法。

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Generate some dummy images,whose (main) contours overlap
img1 = cv2.circle(np.zeros((400,400,3),np.uint8),(150,150),100,(0,255,0),cv2.FILLED)
img2 = cv2.rectangle(np.zeros((400,(175,175),(325,325),255),cv2.FILLED)

# Find contours (OpenCV 4.x)
contours1,_ = cv2.findContours(cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contours2,_ = cv2.findContours(cv2.cvtColor(img2,cv2.CHAIN_APPROX_NONE)

# Find contours (OpenCV 3.x)
#_,contours1,cv2.CHAIN_APPROX_NONE)
#_,contours2,cv2.CHAIN_APPROX_NONE)

# Generate masks of (main) contours; Attention: Hard-coded selection of first contour here!
mask1 = cv2.drawContours(np.zeros((400,400),[contours1[0]],-1,cv2.FILLED)
mask2 = cv2.drawContours(np.zeros((400,[contours2[0]],cv2.FILLED)

# Find intersection of both masks
mask_combined = cv2.bitwise_and(mask1,mask2)

# Generate "exclusive" masks,i.e. masks without the intersection parts
mask1_excl = cv2.bitwise_xor(mask1,mask_combined)
mask2_excl = cv2.bitwise_xor(mask2,mask_combined)

# Visualization
plt.figure()
plt.subplot(3,3,1),plt.imshow(img1),plt.ylabel('img1')
plt.subplot(3,2),plt.imshow(img2),plt.ylabel('img2')
plt.subplot(3,plt.imshow(img1 + img2),plt.ylabel('img1 + img2')
plt.subplot(3,4),plt.imshow(mask1,cmap='gray'),plt.ylabel('mask1')
plt.subplot(3,5),plt.imshow(mask2,plt.ylabel('mask2')
plt.subplot(3,6),plt.imshow(mask_combined,plt.ylabel('mask_combined')
plt.subplot(3,7),plt.imshow(mask1_excl,plt.ylabel('mask1_excl')
plt.subplot(3,8),plt.imshow(mask2_excl,plt.ylabel('mask2_excl')
plt.subplot(3,9),plt.ylabel('mask_combined')
plt.show()

可视化:

Visualization

现在,必须对每个轮廓元组都进行此操作-不仅要成对,因为可以有三个或更多轮廓的交点。跟踪所有这些生成的掩码等将很可能占用大量内存,但计算量并不大。最后,所有方法都需要以某种方式将结果区域存储为某种掩码。

希望有帮助!

本文链接:https://www.f2er.com/3119959.html

大家都在问