Only look at 20% margins for hole punches. Works on all existing examples
This commit is contained in:
parent
73b61c0cab
commit
e1c6777bf8
|
@ -46,6 +46,23 @@ print(f'Analyzing {scanImage} and creating {templateFile}')
|
||||||
img = cv2.imread(scanImage)
|
img = cv2.imread(scanImage)
|
||||||
|
|
||||||
height, width = img.shape[:2]
|
height, width = img.shape[:2]
|
||||||
|
orientation = height > width
|
||||||
|
pageDim = (11, 8.5)
|
||||||
|
if not orientation :
|
||||||
|
pageDim = (8.5, 11)
|
||||||
|
pageRatio = pageDim[1] / pageDim[0]
|
||||||
|
|
||||||
|
left=-1
|
||||||
|
right=-1
|
||||||
|
top=-1
|
||||||
|
bottom=-1
|
||||||
|
|
||||||
|
if orientation :
|
||||||
|
left = width * 0.2
|
||||||
|
right = width * 0.8
|
||||||
|
else :
|
||||||
|
top = height * 0.2
|
||||||
|
bottom = height * 0.8
|
||||||
|
|
||||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
|
@ -57,52 +74,57 @@ canny = cv2.Canny(thresh, 75, 200)
|
||||||
|
|
||||||
contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
|
contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
|
||||||
|
|
||||||
contour_list = []
|
contourList = []
|
||||||
area_list = []
|
areaList = []
|
||||||
for contour in contours:
|
for contour in contours:
|
||||||
approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
|
approx = cv2.approxPolyDP(contour, 0.03 * cv2.arcLength(contour, True), True)
|
||||||
|
if cv2.isContourConvex(approx) :
|
||||||
|
M = cv2.moments(contour)
|
||||||
|
cX = int(M["m10"] / M["m00"])
|
||||||
|
cY = int(M["m01"] / M["m00"])
|
||||||
|
if (orientation and ( cX < left or cX > right) ) or ( not orientation and ( cY < top or cY > bottom)) :
|
||||||
area = cv2.contourArea(contour)
|
area = cv2.contourArea(contour)
|
||||||
area_list.append(area)
|
areaList.append(area)
|
||||||
contour_list.append(contour)
|
contourList.append(contour)
|
||||||
|
|
||||||
maxArea=0
|
maxArea=0
|
||||||
maxIndex=0
|
maxIndex=0
|
||||||
|
|
||||||
for i in range(len(area_list)) :
|
for i in range(len(areaList)) :
|
||||||
area = area_list[i]
|
area = areaList[i]
|
||||||
if area > maxArea:
|
if area > maxArea:
|
||||||
maxArea = area
|
maxArea = area
|
||||||
maxIndex = i
|
maxIndex = i
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
hole_punches = []
|
holePunches = []
|
||||||
centers = []
|
centers = []
|
||||||
centersStr = []
|
centersStr = []
|
||||||
area_range = 0
|
areaRange = 0
|
||||||
|
|
||||||
# pretty good
|
# pretty good
|
||||||
# add position constraint
|
# add position constraint
|
||||||
|
|
||||||
while count < 6 :
|
while count < 6 :
|
||||||
area_range+=1
|
areaRange+=1
|
||||||
for i in range(len(area_list)) :
|
for i in range(len(areaList)) :
|
||||||
area = area_list[i]
|
area = areaList[i]
|
||||||
if area == maxArea or area * ((100 + area_range) / 100) > maxArea :
|
if area == maxArea or area * ((100 + areaRange) / 100) > maxArea :
|
||||||
M = cv2.moments(contour_list[i])
|
M = cv2.moments(contourList[i])
|
||||||
cX = int(M["m10"] / M["m00"])
|
cX = int(M["m10"] / M["m00"])
|
||||||
cY = int(M["m01"] / M["m00"])
|
cY = int(M["m01"] / M["m00"])
|
||||||
strC = f'{cX},{cY}'
|
strC = f'{cX},{cY}'
|
||||||
if strC in centersStr :
|
if strC in centersStr :
|
||||||
continue
|
continue
|
||||||
centersStr.append(strC)
|
centersStr.append(strC)
|
||||||
corners.append((cX, cY))
|
centers.append((cX, cY))
|
||||||
print(f'{cX},{cY}')
|
print(f'{cX},{cY}')
|
||||||
cv2.circle(img, (cX, cY), 40, (255, 0, 0), -1)
|
cv2.circle(img, (cX, cY), 40, (255, 0, 0), -1)
|
||||||
hole_punches.append(contour_list[i])
|
holePunches.append(contourList[i])
|
||||||
count+=1
|
count+=1
|
||||||
print(f'Found hole punches within {area_range}% of largest')
|
print(f'Found hole punches within {areaRange}% of largest')
|
||||||
|
|
||||||
|
|
||||||
cv2.drawContours(img, hole_punches, -1, (0, 255, 0), 20)
|
cv2.drawContours(img, holePunches, -1, (0, 255, 0), 20)
|
||||||
display(img)
|
display(img)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue