I have the following binary image of a disk, and extracted the border of it:
How can I calculate the center and the radius of the circle? I already tried some methods with cv2.HoughCircles() and cv2.findContours() + cv2.fitEllipse(), however these don't work with images where the circle center is far outside of the image.
You can find center of circle from 3 points, but for robust solution it is better to use ransac method. It uses a set of different solutions, for all your bounding points set and will give you more accurate solution. For instance check : here
Related
I want to find the exact center of these attached images. The things I've tried:
1- HoughCircles, but it didn't work because it's not a perfect circle...
2- Thresholded the picture, so it's all black and white -> contour -> center of contour. This doesn't work on both of these images. It gives a center which isn't correct.
Does one of you know another approach I can try?
EDIT: In the first image, you can see why just taking the center with the contour doesn't work. It's not perfectly in the center of the 'circle'
EDIT2: The definition of the center can be seen in the second image where the circle touches all the 'sides' at the same moment
Thanks,
Try the following:
Threshold the image using the OTSU-Method (which will leave you with a binary image, where only the middle section should be kept. Maybe you have to modify the actual threshold value a little bit)
On the thresholded image find the center using image moments (see e.g. https://docs.opencv.org/3.4/d0/d49/tutorial_moments.html)
This assumes, that the middle section will always be brighter in comparison to the area outside.
One way you could try:
Find corners
Clean up outliers
Average their locations
I expect that to be quite in the center.
If not, please define the question more precisely.
I have an image containing a bunch of circular features. I'd like to know exactly the exact location of the centre of a particular circle and its radius (preferably with sub-pixel accuracy).
Rather than using a circle detector, which will try to find all of the circles, is there a method in OpenCV for fitting a circle to the image? Like this:
Update:
I have tried using the Hough circle detection method, and it seems to get confused about whether the circle should be on the inside or outside edge of the black line. The circle jumps around between the inside and outside edges, or sometimes tries to do both.
All I can think of now is, if you know the approximate centre and radius, search for all the circles and use least fitting squares with the circle equation to find the one you are looking for.
I have a panoramic one shot lens from here: http://www.0-360.com/ and I wrote a script using the python image library to "unwrap" the image into a panorama. I want to automate this process though, as currently I have to specify the center of the image. Also, getting the radius of the circle would be good too. The input image looks like this:
And the "unwrapped" image looks like this:
So far I have been trying the Hough Circle detection. The issues I have is selecting the correct values to use. Also, sometimes, dark objects near the center circle seem to throw it off.
Other Ideas I had:
Hough Line detection of the unwrapped image. Basically, choose center pixel as center, then unwrap and see if the lines on the top and bottom are straight or "curvy". If not straight, then keep trying with different centers.
Moments/blob detection. Maybe I can find the center blob and find the center of that. The problem is sometimes I get a bright ring in the center of the dark disk as seen in the image above. Also, the issue with dark objects near the center.
Paint the top bevel of the mirror a distinct color like green to make circle detection easier? If I use green and only use the green channel, would the detection be easier?
Whats the best method I should try and use to get the center of this image and possibly the radius of the outer and inner rings.
As your image have multiple circle with common centre you can move that way, like
Detect circle with Hough circle and consider circle with common centre.
Now check the ratio for co-centred circle, as your image keep that ratio constant.
I guess don't make it too fancy. The black center is at the center of the image, right? Cut a square ROI close to the image center and look for 'black' region there. Store all the 'black' pixel locations and find their center. You may consider using CMYK color space for detecting the black region.
I am trying to detect arcs inside an image. The information that I have for certain with me is the radius of the arc. I can try and maybe get the centre of the circle whose arc I want to identify.
Is there any algorithm in Open CV which can tell us that the detected contour ( or edge from canny edge is an arc or an approximation of an arc)
Any help on how this would be possible in OpenCV with Python or even a general approach would be very helpful
Thanks
If you think that there will not be any change in the shape (i mean arc won't become line or something like this) then you can have a look a Generalized Hough Transform (GHT) which can detect any shape you want.
Cons:
There is no directly function in openCV library for GHT but you can get several source code at internet.
It is sometimes slow but can become fast if you set the parameters properly.
It won't be able to detect if the shape changes. for exmaple, i tried to detect squares using GHT and i got good results but when square were not perfect squares (i.e. rectangle or something like that), it didn't detect.
You can do it this way:
Convert the image to edges using canny filter.
Make the image binary using threshold function there is an option for regular threshold, otsu or adaptive.
Find contours with sufficient length (findContours function)
Iterate all the contours and try to fit ellipse (fitEllipse function)
Validate fitted ellipses by radius.
Check if detected ellipse is good fit - checking how much of the contour pixels are on the detected ellipse.
Select the best one.
You can try to increase the speed using RANSAC each time selecting 6 points from binarized image and trying to fit.
My math is rusty, but...
What about evaluating a contour by looping over its composite edge-nodes and finding those where the angle between the edges doesn't change too rapidly AND doesn't change sign?
A chain of angles (θ) where:
0 < θi < θmax
with number of edges (c) where:
c > dconst
would indicate an arc of:
radius ∝ 1/(θi + θi+1 + ...+ θn)/n)
or:
r ∝ 1/θave
and:
arclenth ∝ c
A way of finding these angles is discussed at Get angle from OpenCV Canny edge detector
I want to implement hough transform algorithm using python, numpy and scipy.
I do not want to use opencv.
I am trying to detect Center of circle or circle in image without known radius.
How do I proceed?
The process of implementing the Hough Transform is pretty straightfoward. I suggest you look on youtube for some videos about it, there are even videos with code/pseudocode for it.
That being said, I've been in the same situation, looking to implement the HT to detect circles. However, the approach I decided to use was a bit different than the traditional HT. Instead of looping over all pixels to generate the circles that passes on at leat one of the circle points, I used the circle points as centers, incrementing the radius from min_radius to max_radius and accummulating it in the same form as the classic HT.
This way, you will end up having a 3D array with (x, y and radius used). The center and radius will be the (x, y, radius) with the maximum accummulated value.
Simplified Hough Transform
I have googled a bit and I found the following:
http://nabinsharma.wordpress.com/2012/12/26/linear-hough-transform-using-python/
Maybe this is what you are searching.
Sorry I think for circles you should try the following:
http://nullege.com/codes/search/houghcircles