水印轮廓的OpenCV处理

3
我希望在我的图像中画一个围绕水印的框。我已经提取了水印并找到了它的轮廓,但是轮廓没有围绕着水印画出来,而是跨越了整个图像。请帮我提供正确的代码。
轮廓坐标的输出结果如下:
[array([[[  0,   0]],

       [[  0, 634]],

       [[450, 634]],

       [[450,   0]]], dtype=int32)]

输出的图片为:

enter image description here

我的代码片段如下:

img = cv2.imread('Watermark/w3.png')

gr = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bg = gr.copy()

closing = cv2.morphologyEx(bg, cv2.MORPH_CLOSE, kernel)  #dilation followed by erosion
#plt.imshow(cv2.subtract(img,opening))
plt.imshow(closing)

_,contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print(contours)
print(len(contours))

if len(contours)>0 :
    cnt=contours[len(contours)-1]
    cv2.drawContours(closing, [cnt], 0, (0,255,0), 3)
    
plt.imshow(closing)
1个回答

3
函数findContours难以找到您的框轮廓,因为它期望在二进制图像上运行。根据文档,为了更好的准确度,请使用二进制图像。因此,在查找轮廓之前,请应用阈值或Canny边缘检测。
在OpenCV中,查找轮廓就像从黑色背景中查找白色对象一样。因此请记住,要查找的对象应为白色,背景应为黑色。因此,在cvtColor函数之后应用threshold,确保您有一个黑色背景。
...
img = cv2.imread('sample.png')
gr = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, bg = cv2.threshold(gr, 127, 255, cv2.THRESH_BINARY_INV)
...

如果在此二值图像上运行findContours函数,您将会找到多个框。
要获得整个文本周围的单个框,请搜索 morphologyEx 函数中创建一个单一的 Blob 的 iterations 参数的数量。
...
kernel = np.ones((3,3))
closing = cv2.morphologyEx(bg, cv2.MORPH_CLOSE, kernel, iterations=5)
...

创建blob后,应用findContours并使用minAreaRect来查找包含传递点集的最小面积旋转矩形。
...
contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print(len(contours))

for i in range(len(contours)):
    rect = cv2.minAreaRect(contours[i])
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(img,[box],0,(127,60,255),2)

cv2.imwrite("output_box.png", img)

enter image description here enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接