Reputation: 2269
I am experimenting on a project in which I have to extract a square, specifically an ID card.
The problem happen just at the begining, when I try to detect (and after to extract) the card. I am using Canny Edge with this code:
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)
For this image (with manually applied blur for privacy) this is the result of Canny edge..
and this is the result with a normal card (after blur was applied)
My questions are 2: how to get rid of background stripes ? How to eventually close the gap in the last image (it's closed in the first processed one because of blur, but take the last as use case) ?
BONUS
I add this as alternative source (and my result - closed kernel)
Thanks
Upvotes: 0
Views: 1153
Reputation: 1978
Instead of converting the image to gray_scale, Try converting it to hsv colorspace. And use the saturation channel to do the edge detection. The code and result:
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)
Upvotes: 3