0% found this document useful (0 votes)
14 views2 pages

OCR

Uploaded by

Duy Phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

OCR

Uploaded by

Duy Phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

def detect_card_v1(im):

'''
Function to detect the card square
input: image
output: image with detected card
'''
# Copy image
im_copy = im.copy()
im_copy = cv2.cvtColor(im_copy, cv2.COLOR_BGR2GRAY)
# Downscale image
# scale_percent = 50 # percent of original size
# width = int(im_copy.shape[1] * scale_percent / 100)
# height = int(im_copy.shape[0] * scale_percent / 100)
# dim = (width, height)
# # resize image
# im_copy = cv2.resize(im_copy, dim, interpolation=cv2.INTER_AREA)

########### Preprocessing ############

# Enhance contrast with CLAHE


clahe = cv2.createCLAHE(clipLimit=6.0, tileGridSize=(5, 5))
im_copy = clahe.apply(im_copy)

orgs = im_copy.copy()

# these constants are carefully picked


MORPH = 23
CANNY = 100
HOUGH = 25

# Blur image with Gaussian Blur


im_copy = cv2.GaussianBlur(im_copy, (3, 3), 0, im_copy)

# this is to recognize white on white


kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (MORPH, MORPH))
dilated = cv2.dilate(im_copy, kernel)

edges = cv2.Canny(dilated, 0, CANNY, apertureSize=3)

lines = cv2.HoughLinesP(edges, 1, np.pi/180, HOUGH)


for line in lines[0]:
cv2.line(edges, (line[0], line[1]), (line[2], line[3]),
(255, 0, 0), 2, 8)

# Find contours
contours, _ = cv2.findContours(
edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS)
contours = filter(lambda cont: cv2.arcLength(cont, False) > 10000, contours)
contours = filter(lambda cont: cv2.contourArea(cont) > 20000, contours)

# simplify contours down to polygons


rects = []
for cont in contours:
rect = cv2.approxPolyDP(cont, 40, True).copy().reshape(-1, 2)
rects.append(rect)

# that's basically it
cv2.drawContours(orgs, rects, -1, (0, 255, 0), 1)
# show only contours
new = get_new(im_copy)
cv2.drawContours(new, rects, -1, (0, 255, 0), 1)
cv2.GaussianBlur(new, (9, 9), 0, new)
new = cv2.Canny(new, 0, CANNY, apertureSize=3)

# cv2.imshow('result', orgs)
# cv2.waitKey(0)
# cv2.imshow('result', dilated)
# cv2.waitKey(0)
# cv2.imshow('result', edges)
# cv2.waitKey(0)
# cv2.imshow('result', new)
# cv2.waitKey(0)
# Save image
cv2.imwrite('card.jpg', new)
return im_copy

You might also like