我正在做一个必须提取正方形的项目,特别是身份证。

python - 摆脱背景条纹(Python,OpenCV)-LMLPHP

当我尝试检测(然后提取)卡时,问题就在一开始就发生了。
我使用此代码的Canny Edge:

import cv2
import numpy as np

card = cv2.imread('card.png')
card = cv2.resize(card, (800, 800))

gray = cv2.cvtColor(card, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
cv2.imshow("Gray", gray)
cv2.waitKey(0)

# detect edges in the image
edged = cv2.Canny(gray, 10, 250)
cv2.imshow("Edged", edged)
cv2.waitKey(0)

# construct and apply a closing kernel to 'close' gaps between 'white'
# pixels
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 10))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
cv2.imshow("Closed", closed)
cv2.waitKey(0)

对于此图像(手动添加模糊效果以保护隐私),这是Canny edge ..的结果。

python - 摆脱背景条纹(Python,OpenCV)-LMLPHP

这是普通卡的结果(应用模糊后)

python - 摆脱背景条纹(Python,OpenCV)-LMLPHP

我的问题是2:如何消除背景条纹?如何最终缩小最后一张图片中的缝隙(由于模糊,它在第一个处理过的图片中已关闭,但以最后一个为用例)?

奖励

我将此添加为替代源(以及我的结果-封闭的内核)

python - 摆脱背景条纹(Python,OpenCV)-LMLPHP
python - 摆脱背景条纹(Python,OpenCV)-LMLPHP

谢谢

最佳答案

而不是将图像转换为gray_scale,请尝试将其转换为hsv色彩空间。并使用饱和通道进行边缘检测。代码和结果:

import cv2
import numpy as np

card = cv2.imread('inputs/Idcard.jpg')
hsv = cv2.cvtColor(card, cv2.COLOR_BGR2HSV)
blur = cv2.GaussianBlur(hsv[:,:,1],(7,7),0)
edged = cv2.Canny(blur, 10, 250)
cv2.imwrite("Edged.jpg", edged)

python - 摆脱背景条纹(Python,OpenCV)-LMLPHP

关于python - 摆脱背景条纹(Python,OpenCV),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48756873/

10-10 21:34