title: Multiple Choice Scanner with Opencv and Python
body: <p>I'm working on a project to scan a multiple choice file of this kind:</p>
  
  <p><a href="http://i.stack.imgur.com/I4sjw.jpg" rel="nofollow">Multiple Choices Image</a></p>
  
  <p>The code works but sometimes I have some troubles to recognize the right position of the check box.</p>
  
  <p>I proceed as follow:</p>
  
  <ul>
  <li><p>First I detect the 20 black box at the right side of the image and I add the x and width to a list:</p>
  
  <pre>
  <code>
  image = cv2.imread(args["image"])
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  (_, thresh) = cv2.threshold(gray, 220, 255,cv2.THRESH_BINARY) 
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
  closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
  dilation = cv2.dilate(closed,None,iterations = 5)
  (_,contours,_)=cv2.findContours(dilation.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
  sorted_rows = sorted(contours, key = cv2.contourArea, reverse = True)[1:21]
  for idx,row in enumerate(sorted_rows):
      (_x,y,w,h) = cv2.boundingRect(row)
      rows.append([(y),(y+h)])
  
  </code> 
  </pre></li>
  <li><p>then I detect the five columns:</p>
  
  <pre>
  <code>
  (_, thresh) = cv2.threshold(gray, 127, 255,cv2.THRESH_BINARY) 
  closed = cv2.erode(thresh, None, iterations = 4)
  (_,contours,_)=cv2.findContours(closed.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
  sorted_cols = sorted(contours, key = cv2.contourArea, reverse = True)[1:6]
  for col in sorted_cols:
      # add to list
      (x,y,w,h) = cv2.boundingRect(col)
      cols.append([(x),(x+w)])
  </code>
  </pre></li>
  <li><p>next, I sort the two lists by the position of x and y respectively:</p>
  
  <pre>
  <code>
  cols = sorted(cols, key = lambda x: x[0])
  rows = sorted(rows, key = lambda x: x[0])
  </code>
  </pre></li>
  <li><p>then I loop through the columns list and the rows list to build the coordinates of my intersection where I can find the five check boxes</p>
  
  <pre>
  <code>
  count_iterations = 0
  for col in cols:
      for row in rows:  
          count_iterations +=1 
          crop = image[row[0]:row[1], col[0]: col[1]]
          denoised = cv2.fastNlMeansDenoisingColored(crop,None,10,10,7,21)
          edges = cv2.Canny(denoised,220,250)
          closed = cv2.dilate(edges, None, iterations = 1)
          (_,contours,_)= cv2.findContours(closed.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
          checkboxes = sorted(contours, key = cv2.contourArea, reverse = True)[:5]
          crdn_checkboxes = []
          for check_box in checkboxes:
              crdn_checkboxes.append(cv2.boundingRect(check_box))
  </code>     
  </pre></li>
  <li><p>finally, I sort my check boxes by their x position and I check the pixel s density to find the right one:</p>
  
  <pre>
  <code>
  crdn_checkboxes = sorted(crdn_checkboxes, key = lambda x: x[0])
  for idx, crdn in enumerate(crdn_checkboxes):
  
  <pre><code>x,y,w,h = crdn
  check_box = crop[y:y+h,x:x+w]
  check_box_img_gray = cv2.cvtColor(check_box, cv2.COLOR_BGR2GRAY)
  (_, thresh) = cv2.threshold(check_box_img_gray,200,255,cv2.THRESH_BINARY)
  height,width = thresh.shape[:2]
  tot_px = height * width
  checked_box =  cv2.countNonZero(thresh) / float(tot_px)
  if checked_box &lt; 0.6: 
      print "Found"
  </code></pre>
  
  </code>
  </pre></li>
  </ul>
  
  <p>The problem seems to be due to the presence of the line number at the left side of the five check boxes that is recognized as a check box.</p>
  
  <p><a href="http://i.stack.imgur.com/AlCMr.png" rel="nofollow">Line processed</a></p>
  
  <p>I'm wondering if someone could help to fix this and eventually reviews my code.</p>