I am having problems detecting circle areas. I tried it with the HoughCircles function from opencv. However even though the images are pretty similar the parameters for the funtion have to be different in order to detect the cirles.
Another approach I tried was to iterate over every pixel and check if the current pixel is white. If this is the case then check if there is a blob object in the area (distance to blob center smaller than a threshold). If there is, append the pixel to the blob, if not then create a new blob. This also didn't work properly.
Has anyone a idea how I can make this work (90% detection) ? I attached a example image and another image where I marked the cirles. Thanks!
UPDATE: Thank you for the help so far! This is the code where I acquire the contours and filter them by area:
im = cv2.imread('extract_blue.jpg')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gauss = cv2.GaussianBlur(imgray, (5, 5), 0)
ret, thresh = cv2.threshold(im_gauss, 127, 255, 0)
# get contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours_area = []
# calculate area and filter into new array
for con in contours:
area = cv2.contourArea(con)
if 1000 < area < 10000:
contours_area.append(con)
This works pretty neat. I drew them on the image:
This is the part where I filtered by circularity, it goes straight below the code where I filter by area:
contours_cirles = []
# check if contour is of circular shape
for con in contours_area:
perimeter = cv2.arcLength(con, True)
area = cv2.contourArea(con)
if perimeter == 0:
break
circularity = 4*math.pi*(area/perimeter*perimeter)
print circularity
if 0.8 < circularity < 1.2:
contours_cirles.append(con)
However, the new list 'contours_cirles' is empty. I printed 'circularity' in the loop and the values are all between 10 000 and 100 000.
UPDATE #2: After correcting the missing brackets it is working now!
contours_cirles = []
# check if contour is of circular shape
for con in contours_area:
perimeter = cv2.arcLength(con, True)
area = cv2.contourArea(con)
if perimeter == 0:
break
circularity = 4*math.pi*(area/(perimeter*perimeter))
print circularity
if 0.7 < circularity < 1.2:
contours_cirles.append(con)
Thanks a lot guys! :)
(area/perimeter*perimeter)
this looks odd to me. Are you sure, you did not forget any brackets ? – Ottillia