While solving an OMR problem I am not completely able to detect all the marked answers correctly.
Here is my input sheet.
import cv2
import numpy as np
# read image
img = cv2.imread('omr_sheet.jpg')
h, w = img.shape[:2]
# trim 15 from bottom to remove partial answer
img = img[0:h-15, 0:w]
# threshold on color
lower=(120,60,80)
upper=(160,100,120)
thresh = cv2.inRange(img, lower, upper)
# apply morphology close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
# get contours
result = img.copy()
centers = []
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = contours[0] if len(contours) == 2 else contours[1]
print("count:", len(contours))
print('')
i = 1
for cntr in contours:
M = cv2.moments(cntr)
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
centers.append((cx,cy))
cv2.circle(result, (cx, cy), 20, (0, 255, 0), -1)
pt = (cx,cy)
print("circle #:",i, "center:",pt)
i = i 1
# print list of centers
#print(centers)
# save results
cv2.imwrite('omr_sheet_thresh.png',thresh)
cv2.imwrite('omr_sheet_morph.png',morph)
cv2.imwrite('omr_sheet_result.png',result)
# show results
cv2.imshow("thresh", thresh)
cv2.imshow("morph", morph)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Threshold Image:
Morphology Image:
Results:
Answers:
count: 48
circle #: 1 center: (77, 3151)
circle #: 2 center: (78, 3087)
circle #: 3 center: (77, 3021)
circle #: 4 center: (76, 2959)
circle #: 5 center: (79, 2892)
circle #: 6 center: (77, 2830)
circle #: 7 center: (77, 2762)
circle #: 8 center: (78, 2695)
circle #: 9 center: (77, 2631)
circle #: 10 center: (76, 2566)
circle #: 11 center: (78, 2500)
circle #: 12 center: (77, 2435)
circle #: 13 center: (77, 2373)
circle #: 14 center: (77, 2301)
circle #: 15 center: (79, 2238)
circle #: 16 center: (79, 2178)
circle #: 17 center: (77, 2108)
circle #: 18 center: (78, 2045)
circle #: 19 center: (80, 1980)
circle #: 20 center: (78, 1913)
circle #: 21 center: (78, 1848)
circle #: 22 center: (80, 1786)
circle #: 23 center: (77, 1722)
circle #: 24 center: (77, 1657)
circle #: 25 center: (79, 1593)
circle #: 26 center: (79, 1524)
circle #: 27 center: (80, 1461)
circle #: 28 center: (77, 1395)
circle #: 29 center: (79, 1332)
circle #: 30 center: (76, 1265)
circle #: 31 center: (80, 1203)
circle #: 32 center: (73, 1136)
circle #: 33 center: (77, 1072)
circle #: 34 center: (80, 1007)
circle #: 35 center: (77, 944)
circle #: 36 center: (78, 878)
circle #: 37 center: (75, 815)
circle #: 38 center: (77, 747)
circle #: 39 center: (77, 684)
circle #: 40 center: (79, 618)
circle #: 41 center: (77, 554)
circle #: 42 center: (78, 488)
circle #: 43 center: (80, 423)
circle #: 44 center: (78, 359)
circle #: 45 center: (77, 293)
circle #: 46 center: (78, 232)
circle #: 47 center: (77, 165)
circle #: 48 center: (78, 102)
CodePudding user response:
Analysis:
The marked bubbles are in blue ink, which is a dominant color in the B-channel of the corresponding LAB color space.
- The B-channel captures blue and yellow colors in an image
- While A-channel captured red and green colors
- The L-channel highlights the brightness content in the image
# Otsu threshold on B-channel
th = cv2.threshold(b_channel,0,255,cv2.THRESH_BINARY_INV cv2.THRESH_OTSU)[1]
# morphological operations to close up the gaps within
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
closing = cv2.morphologyEx(th, cv2.MORPH_CLOSE, kernel, iterations = 2)
# finding contours and drawing those above certain area onto black screen
contours, h = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
black = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
for c in contours:
area = cv2.contourArea(c)
if area > 100:
black = cv2.drawContours(black, [c], -1, (0, 255, 0), -1)