<filename>unittest/test_Contours.py
import numpy as np
import cv2

import math

img = cv2.imread('/Users/xh/Downloads/ks/vision-ui/unittest/green.png')

# Prepocess
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
flag, thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)

# Select long perimeters only
perimeters = [cv2.arcLength(contours[i],True) for i in range(len(contours))]
listindex=[i for i in range(15) if perimeters[i]>perimeters[0]/2]
numcards=len(listindex)

card_number = -1 #just so happened that this is the worst case
stencil = np.zeros(img.shape).astype(img.dtype)
cv2.drawContours(stencil, [contours[listindex[card_number]]], 0, (255, 255, 255), cv2.FILLED)
res = cv2.bitwise_and(img, stencil)
cv2.imwrite("/Users/xh/Downloads/ks/vision-ui/unittest/out.png", res)
# canny = cv2.Canny(res, 100, 200)
# cv2.imwrite("canny.bmp", canny)
<filename>unittest/test_Contours.py
import numpy as np
import cv2

import math

img = cv2.imread('/Users/xh/Downloads/ks/vision-ui/unittest/green.png')

# Prepocess
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
flag, thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)

# Select long perimeters only
perimeters = [cv2.arcLength(contours[i],True) for i in range(len(contours))]
listindex=[i for i in range(15) if perimeters[i]>perimeters[0]/2]
numcards=len(listindex)

card_number = -1 #just so happened that this is the worst case
stencil = np.zeros(img.shape).astype(img.dtype)
cv2.drawContours(stencil, [contours[listindex[card_number]]], 0, (255, 255, 255), cv2.FILLED)
res = cv2.bitwise_and(img, stencil)
cv2.imwrite("/Users/xh/Downloads/ks/vision-ui/unittest/out.png", res)
# canny = cv2.Canny(res, 100, 200)
# cv2.imwrite("canny.bmp", canny)
# Prepocess # Find contours # Select long perimeters only #just so happened that this is the worst case # canny = cv2.Canny(res, 100, 200) # cv2.imwrite("canny.bmp", canny)