Akhilesh Chobey
Akhilesh Chobey

Reputation: 317

Python cv2 edge and contour detection

I am trying to detect bubbles on an OMR sheet which looks something like this:

Raw image

My code for edge detection and contour display is referenced from here. However, before finding the actual contours, I am trying to detect the edges but somehow not able to set the correct values of parameters. This is what I get:

edged

Code:

from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np
import argparse
import imutils
import cv2

def auto_canny(image, sigma=0.50):
    # compute the median of the single channel pixel intensities
    v = np.median(image)

    # apply automatic Canny edge detection using the computed median
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    edged = cv2.Canny(image, lower, upper)


# return the edged image
return edged

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to the input image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])

r = 500.0 / image.shape[1]
dim = (500, int(image.shape[0] * r))

# perform the actual resizing of the image and show it
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
equalized_img =  cv2.equalizeHist(gray)
cv2.imshow('Equalized', equalized_img)
# cv2.waitKey(0)
blurred = cv2.GaussianBlur(equalized_img, (7, 7), 0)
# edged =cv2.Canny(equalized_img, 30, 160)
edged = auto_canny(blurred)

cv2.imshow('edged', edged)
cv2.waitKey(0)

How can I get all the 90*4 circles?

Upvotes: 2

Views: 1697

Answers (2)

dpetrini
dpetrini

Reputation: 1239

You need to improve your contour detection. Eventually by not changing it, but by better pre-processing the earlier stage. Contour detection works better with more contrast and color separation in image. If you don´t have yet need to threshold you image with techniques like Simple Threshold, Adaptive or more smart techniques like Otsu's. Check Open CV document here.

Besides that, for your case eventually need more advanced techniques like "Adaptive Thresholding Using the Integral Image", described here.

Upvotes: 0

Header Footer
Header Footer

Reputation: 161

You should be using Hough to search for circles. This method project every single white pixel as a circle, and tries to get as many overlapping pixels possible. You'll have to specify the predicted radiuses of circles to be found within image.

Hough projects every white pixel on circles within specified radi

  • Left - original image
  • Top-right - each white pixel is projected as red circle - they are too small to find intersecting point
  • Bottom-right - green circle is larger, and all the intersecting points meet exactly at the middle of the circle! Both radius and position is returned by cvHoughCircles

This person dealt with blob detection (that's what finding circles is called I think) using cvHoughCircles with cvCanny-ized image (read OPs update).

OpenCV: Error in cvHoughCircles usage

Upvotes: 1

Related Questions