Split a multiple choice answer sheet

I have this picture below. I want to separate it into two parts, so I can work independently with every part. I added those black dots to make my algo detect the dots and separate the part but it’s detect all the elements of paper. it doesn’t work as i expected, i do not know the reason why.

I’am new in opencv and image processing

this is my code

import cv2
import numpy as np

image = cv2.imread('18936-model-1.jpg')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (23, 23), 0)
cv2.imshow("blurred", blurred)
canny = cv2.Canny(blurred, 30, 30)
cnts, _ = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print(f"Contours in the image, %d" % (len(cnts)))
shape = image.copy()
cv2.drawContours(shape, cnts, -1, (0, 255, 0), 2)
cv2.imwrite('{}.png'.format('sunflower'), shape)
cv2.imshow("Edges", shape)
cv2.waitKey(0)

the left image is the original one. the middle image is what i want to process. the right picture is what i get

however, it’s doing exactly, what you told it to do :wink:

  • make your life easy. assuming, this comes from a flatbed scanner (no significant scaling) – assume, that the lower part is 3/4 of the whole – and just crop it blindly
  • use the markers, luke, they’re there for a purpose …
  • there’s a nice horizontal dividing line. use morphology or reduce() or check your existing contours for that
  • switch Canny (ooouuch) for a plain threshold(), and remember, that opencv expects white contours on black bg, so – invert intensity before findContours() (that’s a simple ~img or 255-img in python)
  • visualize ALL intermediate steps, using imshow(), to quickl find out, where things did not went “as expected”
1 Like

related:

2 Likes

@crackwitz Thanks! Reading your suggested article has proven very helpful for me.