【问题标题】:Detecting edges of a card with blurry edges and different backgrounds检测具有模糊边缘和不同背景的卡片边缘
【发布时间】:2020-06-15 16:51:22
【问题描述】:

这是我的测试照片

我正在寻找卡片的边缘。但是,如您所见,边缘有些模糊。

为了找到边缘,我首先增强了图像的对比度,希望模糊的边缘不会那么模糊,并且更容易找到: 然后我用高斯模糊稍微平滑了一点(我尝试去除高斯模糊,但边缘检测器在背景中发现了很多细节+在卡片中)。

然后我使用带有“动态阈值”的 canny 并得到以下结果: 正如你所看到的,我发现了卡片的任何边缘(除了左边,因为背景很暗,这很容易)。是否有稳健的(我不想在此图像上“过度拟合”)方法来找到直线模糊边缘?

在这里找到了一些建议: Blurry edge detection How to find accurate corner positions of a distorted rectangle from blurry image in python?,但没有一个结果令人满意。

完整代码:

def auto_canny(image, sigma=0.5):
    v = np.median(image)
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    return cv2.Canny(image, lower, upper)

def add_contrast(img, contrast_level=8):
    lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)

    l, a, b = cv2.split(lab)

    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(contrast_level, contrast_level))
    cl = clahe.apply(l)

    limg = cv2.merge((cl, a, b))

    final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)

    return final

# ------------------------------------------ #
# FIND EDGES
# ------------------------------------------ #
img = add_contrast(img=img, contrast_level=8)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)

edges = auto_canny(image=blur_gray) 

# Show images for testing
cv2.imshow('edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

【问题讨论】:

  • ID的底部将很难一步找到。您可以将阈值放宽到 canny 并对返回的边缘进行后处理,或者编写自己的边缘检测方式。在某种程度上,顶部和左侧边缘的检测应该更容易。您真正需要的是左上角和左下角来查找倾斜和比例。
  • 测试图像中的信息是否用红色进行了编辑?还是在发布图片之前手动添加?
  • @eldesgraciado 我在发布图片之前手动添加了红色(尽管我在这张图片上运行了算法)。

标签: python opencv computer-vision


【解决方案1】:

这也不是一个完整的解决方案,但如果红色部分有问题,您可以先使用cv2.inpaint() 函数修复这些部分。然后,您可以应用其余方法来查找卡片边缘。

# create an inpainting mask with "red-enough" pixels
mask = cv2.inRange(img_src_rgb, np.array([200,0,0]), np.array([255,50,50]))
# enlarge the mask to cover the borders
kernel = np.ones((3,3),np.uint8)
mask = cv2.dilate(mask,kernel,iterations = 1)
# inpaint the red parts using Navier-Stokes based approach
img_dst = cv2.inpaint(img_src, mask,50,cv2.INPAINT_NS)
cv2.imshow("no_red", img_dst)

生成的图像如下。

编辑:既然我们知道您在问什么,下面是一个完整的解决方案。

修复后,您可以应用霍夫变换来找到图像中的强直线。

gray = cv2.cvtColor(img_dst, cv2.COLOR_RGB2GRAY)
edges = auto_canny(gray) # your auto_canny function, WITHOUT blur
lines = cv2.HoughLines(edges, 1, np.pi/90, 50)
for line in lines:
    rho,theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 10000*(-b))
    y1 = int(y0 + 10000*(a))
    x2 = int(x0 - 10000*(-b))
    y2 = int(y0 - 10000*(a))
    cv2.line(img_dst,(x1,y1),(x2,y2),(0,255,0),1)

cv2.imwrite('linesDetected.jpg', img_dst)

同样,结果行如下。

【讨论】:

  • 这是个好主意!然而,这不是我的问题。我希望找到一种检测模糊边缘的方法
  • 在您的示例中,模糊边缘由红色区域边界主导,因此我建议将此解决方案作为预处理步骤。您应该通过提及红色部分不在原始图像中来纠正您的问题,并根据原始图像生成示例输出,以便我们提供帮助。
  • @AlexGoft,请参阅上面的完整答案。
  • 感谢您的出色回答,对于给您带来的不便,我们深表歉意。您能否详细说明为什么使用 canny 而不模糊?
  • 你实际上需要边缘和角落,高斯模糊会平滑它们,所以你也会丢失卡片边缘。如果您想要一些边缘感知平滑,您可以尝试双边过滤器。此外,增加对比度会引入新的边缘,这些边缘对噪声的贡献比对您寻找的边缘的贡献更大。对于这种情况,您应该利用数据中的假设:卡片边缘是直线。
【解决方案2】:

您可以通过使用cv2.floodFill 填充部分背景来改进解决方案。

在寻找边缘之前增强对比度是个好主意,但它看起来会产生一些伪影,使寻找边缘变得更加困难。

这是一个代码示例:

import numpy as np
import cv2


def auto_canny(image, sigma=0.5):
    v = np.median(image)
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    return cv2.Canny(image, lower, upper)


img = cv2.imread('card.png')

h, w = img.shape[0], img.shape[1]

# Seed points for floodFill (use few points at each corner for improving robustness)
seedPoints = ((5, 5), (w-5, 5), (5, h-5), (w-5, h-5), 
              (w//2, 5), (w//2, h-5), (5, h//2), (w-5, h//2), 
              (w//4, 5), (w//4, h-5), (5, h//4),  (w-5, h//4),
              (w*3//4, 5), (w*3//4, h-5), (5, h*3//4),  (w-5, h*3//4))

# Fill parts of the background with black color
for seed in seedPoints:
    cv2.floodFill(img, None, seedPoint=seed, newVal=(0, 0, 0), loDiff=(2, 2, 2), upDiff=(2, 2, 2))

# ------------------------------------------ #
# FIND EDGES
# ------------------------------------------ #
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)

# Second, process edge detection use Canny.
edges = auto_canny(image=blur_gray)

# Show images for testing
cv2.imshow('edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

我知道这不是一个完整的解决方案,但我希望它有所帮助......

【讨论】:

  • 点沿着图像的周边均匀分布(我应该使用循环)。
猜你喜欢
  • 2016-08-26
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
相关资源
最近更新 更多