【问题标题】:Detecting the edge of a colorful picture OpenCV检测彩色图片OpenCV的边缘
【发布时间】:2021-09-01 15:55:10
【问题描述】:

我是 CV 新手,我刚刚学会了如何检测纸张的边缘。我想尝试一些更复杂的东西。所以我从一个电影网站截屏,想从网站上检测海报。如果背景颜色与海报不同,效果很好。但是当它们颜色相似时,我找不到图片的边缘 cv2.findContours() 原图为: Poster

而我所做的是:

img = cv2.imread('pic5.jpg')
orig = img.copy()
image = orig
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
binary = cv2.medianBlur(gray,3)
# blur = cv2.GaussianBlur(binary, (5, 5), 0)
# ret, binary = cv2.threshold(blur,127,255,cv2.THRESH_TRUNC)
edged = cv2.Canny(binary, 3, 30)
show(edged)

# detect edge
contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:5]

#
for c in cnts:
    # approx
    peri = cv2.arcLength(c, True)
    eps = 0.02
    approx = cv2.approxPolyDP(c, eps*peri, True)

    # detect square (4 points)
    if len(approx) == 4:
        screenCnt = approx
        break

res = cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
show(orig)

结果是: after preprocess What I detect

我不知道这种方法是否有效。是否可以根据背景颜色(与海报颜色无关)检测正方形部分?

【问题讨论】:

  • 嗨!您可以在检测到边缘后尝试进行闭合操作,这样您就可以填补海报中的空白并获得更平滑,更接近矩形的东西。 docs.opencv.org/master/d9/d61/…
  • 但是在这里检测边缘听起来不是一个好方法,因为它在很大程度上取决于海报的内容和颜色。您可以尝试定位网站的一些固定元素,然后从已知的 x,y 偏移量中剪切海报。
  • 另一种方法是遍历图像,并找到边界框,其中您有一些与背景不同的颜色和与文本颜色不同的颜色。但是,如果您的海报与网站背景颜色相同,这可能会失败。
  • 如果这是一项真正的任务,那么您可能需要结合几种方法,检查每个算法找到的海报有多大,如果算法失败,则回退到另一个。如果只是为了教育,你可以继续解决一些明确定义的问题,或者按照这里的优秀教程:docs.opencv.org/master/d6/d00/tutorial_py_root.html
  • 感谢您的回复!我想到了海报与网站背景颜色相同的情况。但我想只有极少部分的海报有相同的颜色。在这种情况下,即使没有找到边缘的某些部分,我们也可以找到包含剩余部分的最小矩形。

标签: python opencv image-processing deep-learning computer-vision


【解决方案1】:

您可以继续使用edged 结果,并使用闭合形态学运算来闭合小间隙。

建议您不要使用approxPolyDP 搜索矩形,而是找到最大连通分量(或最大轮廓)的边界矩形。

在我的代码示例中,由于外部边界线,我将 findContours 替换为 connectedComponentsWithStats
您可以使用开放形态操作来摆脱外部线(并使用findContours继续使用)。

您也可以使用approxPolyDP 来优化结果。


这是代码示例:

import numpy as np
import cv2

img = cv2.imread('pic5.png')
orig = img.copy()
image = orig
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
binary = cv2.medianBlur(gray, 3)
edged = cv2.Canny(binary, 3, 30)

edged = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, np.ones((5,5)))  # Close small gaps

#contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#c = max(contours, key=cv2.contourArea) # Get the largest contour
#x, y, w, h = cv2.boundingRect(c)  # Find bounding rectangle.

nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(edged, 8)  # finding components

# https://stackoverflow.com/a/61662694/4926757
# Find the largest non background component.
# Note: range() starts from 1 since 0 is the background label.
max_label, max_size = max([(i, stats[i, cv2.CC_STAT_AREA]) for i in range(1, nb_components)], key=lambda x: x[1])

# Find bounding rectangle of largest connected component.
x = stats[max_label, cv2.CC_STAT_LEFT]
y = stats[max_label, cv2.CC_STAT_TOP]
w = stats[max_label, cv2.CC_STAT_WIDTH]
h = stats[max_label, cv2.CC_STAT_HEIGHT]

res = image.copy()
cv2.rectangle(res, (x, y), (x+w, y+h), (0, 255, 0), 2)  # Draw a rectangle

cv2.imshow('edged', edged)
cv2.imshow('res', res)
cv2.waitKey()
cv2.destroyAllWindows()

结果:

edged:

res:

【讨论】:

  • 找到最大的连通分量是个好主意!我只是认为cv2.erode(), cv2.dilate(), cv2.morphologyEx() 可能会有所帮助,但我不确定如何使用它们。非常感谢您的回复!
猜你喜欢
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多