Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

opencv - Best Way to Find Circles in Image using Python

I wish to approximately count the total number of storage tanks (i.e. the white circles) in this satellite image. Satellite Image

I have tried using OpenCV to tackle this task, but I have been experiencing some problems. Here is my current code:

import numpy as np
import cv2
import matplotlib.pyplot as plt

image_color= cv2.imread("satellite.jpg")

lower_bound = np.array([0,0,10])
upper_bound = np.array([255,255,195])
image = image_color
mask = cv2.inRange(image_color, lower_bound, upper_bound)
mask = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
plt.imshow(mask)

Mask

circles = cv2.HoughCircles(image=mask, method=cv2.HOUGH_GRADIENT, dp=1.2, 
                           minDist=1, param1=50, param2=50, minRadius=1,
                           maxRadius=10)

if circles is not None:
    circlesRound = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circlesRound:
        cv2.circle(image_color, (x, y), r, (0, 255, 0), 4)
    plt.imshow(image_color)
else:
    print ('No circles found')

But I'm getting no circles.

Sorry, I'm still really new to OpenCV. Any sort of insight would be greatly appreciated.

question from:https://stackoverflow.com/questions/65894829/best-way-to-find-circles-in-image-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

it`s all about parameters to the function. i took a liberty to modify your code a little an this solved an issue for me (at least partially)

mask = cv2.erode(mask, kernel)
mask = cv2.dilate(mask, kernel)
# mask = cv2.GaussianBlur(mask, (7,7), 1.5)
cv2.imshow('mask', mask)

circles = cv2.HoughCircles(image=mask, method=cv2.HOUGH_GRADIENT, dp=1,
                           minDist=5, param1=300, param2=10, minRadius=1,
                           maxRadius=15)

not all circles are detected, but this, at least, is a proof of the concept. You may want to play with the parameter values and apply morphological transforms before running the detector. I took my values from official manual, reading it may give you an insight too https://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga47849c3be0d0406ad3ca45db65a25d2d

Also note that dection method seems to affect the way output data is stored.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...