Ask Your Question
0

Detect ellipses, ovals in images with OpenCV python[SOLVED]

asked 2019-11-27 23:05:05 -0600

abbeykevin gravatar image

updated 2019-12-03 13:15:47 -0600

supra56 gravatar image

Hello everyone, I am using opencv to detect shapes that look like circles, my script reads a png image (center_cut.png) and perfectly detects the circles with their centroids, however when I pass another image where the circles are no longer perfect does not recognize them (left_cut). I would like to know if there is any way to detect shapes similar to circles, could someone please guide me? My code is as follows:

import cv2
import numpy as np

img = cv2.imread('center_cut.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1, 50,
                             param1=80,param2=20,minRadius=3,maxRadius=25)


circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # Dibuja la circusnferencia del círculo
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # dibuja el centro del círculo
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('círculos detectados',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

image description image description

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
5

answered 2019-11-28 00:05:54 -0600

Tetragramm gravatar image

updated 2019-12-03 13:16:16 -0600

supra56 gravatar image

Well, if these are representative images, you are probably better off doing something besides HoughCircles.

I suggest doing an adaptive threshold to segment the image into white and black, inverting the image, then performing connected components. If you use connectedComponentWithStats, it will return the centroid of every component (which would be the circles)

If you're worried about the edges, you'll have to remove any component that touches the edge of the image, but that shouldn't be hard.

t2 = cv2.imread('Test2.png')
thresh = cv2.cvtColor(t2, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(thresh, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 101, 0)
count, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh)
for i in range(1,count):
    t2 = cv2.circle(t2, (int(centroids[i,0]), int(centroids[i,1])), 5, (0, 255, 0, 0), 5)

cv2.imshow('circles', thresh)
cv2.imshow('centers', t2)
cv2.waitKey()

image description image description

edit flag offensive delete link more
2

answered 2019-12-03 10:03:39 -0600

abbeykevin gravatar image

it works thank, you very much

edit flag offensive delete link more

Comments

1

Make sure to upvote the answer, and to mark it as correct (click on the check-mark).

sjhalayka gravatar imagesjhalayka ( 2019-12-03 14:17:02 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-11-27 23:05:05 -0600

Seen: 12,119 times

Last updated: Dec 03 '19