磁条(黑色矩形)检测

时间:2019-06-22 18:06:41

标签: python opencv computer-vision

我正在尝试检测该人脸前信用卡的磁条。首先,我尝试使用Canny边缘检测器检测边界。尽管可见边缘清晰,但边缘检测无法检测到不连续的边界。下面是我运行以获得结果的代码:

img = cv2.imread(input_dir + str(f))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 5, 10, 10)
edges = cv2.Canny(gray, 20, 60)

plt.subplot(121), plt.imshow(gray, cmap='gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(edges, cmap='gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

enter image description here

源图像:

enter image description here

所需的结果(标有红色的区域)enter image description here

我将不胜感激。

谢谢

Niko

2 个答案:

答案 0 :(得分:1)

更新:

在将色彩空间转换为HSV之后执行双重阈值处理。查看以下结果:

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

img = cv2.imread("img.jpg")

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_grey = np.array([0, 5, 50]) #Lower threshold for grey.
upper_grey = np.array([360, 50, 255]) #Higher threshold for grey.
mask = cv2.inRange(hsv, lower_grey, upper_grey)
img_res = cv2.bitwise_and(img, img, mask = mask)
img_res = cv2.GaussianBlur(img_res,(7,7),0)

edges = cv2.Canny(img_res, 100, 200)

plt.subplot(121), plt.imshow(img_res, cmap='gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(edges, cmap='gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

**enter image description here**

原始:

首先,您可以将色彩空间转换为HSV,然后在其顶部使用高斯模糊。enter image description here。这是我使用的代码:

import cv2
import matplotlib.pyplot as plt

img = cv2.imread("img.jpg")

gray = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
gray = cv2.GaussianBlur(gray[:,:,1],(7,7),0)
edges = cv2.Canny(gray, 20, 60)

plt.subplot(121), plt.imshow(gray, cmap='gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(edges, cmap='gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

答案 1 :(得分:0)

我的解决方案仅限于这张图片,在这张图片中,卡片水平放置并紧握着。

!wget https://i.stack.imgur.com/46VsT.jpg

读入图像。

import matplotlib.pyplot as plt
import numpy as np
import imageio

# rgb to gray https://stackoverflow.com/a/51571053/868736
im = imageio.imread('46VsT.jpg')
gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114]) 
gray = gray(im)  
image = np.array(gray)
plt.imshow(image,cmap='gray')

enter image description here

import numpy as np
import skimage
from skimage import feature
from skimage.transform import probabilistic_hough_line
import matplotlib.pyplot as plt
from matplotlib import cm

在水平边缘上找到一些约束。

edges = np.abs(skimage.filters.sobel_h(image))
edges = feature.canny(edges,1,100,200)
plt.imshow(edges,cmap='gray')

enter image description here

找到更多约束的水平线。

# https://scikit-image.org/docs/dev/auto_examples/edges/plot_line_hough_transform.html
lines = probabilistic_hough_line(edges, threshold=1, line_length=200,line_gap=100)
plt.imshow(edges * 0,cmap='gray')
for line in lines:
    p0, p1 = line
    plt.plot((p0[0], p1[0]), (p0[1], p1[1]),color='red')

enter image description here

使用检测到的线获得感兴趣的区域。

# https://scikit-image.org/docs/dev/auto_examples/edges/plot_convex_hull.html
from skimage.morphology import convex_hull_image
canvas = edges*0
for line in lines:
    p0, p1 = line
    canvas[p0[1],p0[0]]=1
    canvas[p1[1],p1[0]]=1
chull = convex_hull_image(canvas)
plt.imshow(chull,cmap='gray')

enter image description here

...但是为什么呢? ;)

我怀疑上述解决方案是否真的可以在“生产中”工作……如果您有足够的资源,我会去使用经过修改的YOLO模型,然后将资源用于构建良好的训练数据集(强调“良好”数据集,但您必须先定义什么是优点...),请观看此视频,以获取启发,https://www.youtube.com/watch?v=pnntrewH0xg