Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect circle like shapes opencv

everyone i'm fairly new to OpenCV and computer vision and i'm stuck at this problem , which might seem like a fairly trivial but forgive my noobness :)

I'm trying to detect Rebars from a cross-sectional image.

Original Color Image

i'm using this code :

import cv2
import cv2.cv as cv
import numpy as np

img = cv2.imread('test/t2.jpg',0)
img = cv2.equalizeHist(img)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is the result i'm getting currently, which is not good : Result

I'm looking for pointers on how to proceed with this problem and how to learn more about CV as i'm really interested!

Thanks a ton!

like image 301
Harpreet Boparai Avatar asked Feb 20 '16 04:02

Harpreet Boparai


People also ask

How do I find circles in OpenCV?

HoughCircles() function. Finds circles in a grayscale image using the Hough transform. In the below example we will take an image as input. Then make a copy of it and apply this transform function to identify the circle in the output.

What algorithm is used to detect circles in OpenCV?

Use the OpenCV function HoughCircles() to detect circles in an image.

Can OpenCV detect shapes?

We can find shapes present in an image using the findContours() and approxPolyDP() function of OpenCV. We can detect shapes depending on the number of corners it has. For example, a triangle has 3 corners, a square has 4 corners, and a pentagon has 5 corners.


2 Answers

HoughCircles is not a strong enough way to detect circle in such complex image like your case.

SO has already had some discussion about this. You could refer these post with quality accepted answers

Standard way:

Filled circle detection using CV2 in Python?

What are the possible fast ways to detect circle in an image?

Noise image:

https://dsp.stackexchange.com/questions/5930/find-circle-in-noisy-data

Another method:

Gradient Pair Vectors

Learning Automata

like image 97
Trevor Avatar answered Oct 11 '22 16:10

Trevor


Those results can be slightly improved with setting the parameters better on this line:

circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)

For example, you can reduce the maxRadius slightly and increase the sensitivity.

In my experience, however, you won't get a good result on an image like this. It is very complex, the circles are irregular and at different angles. If your goal is to practice, then sure, play with the parameters and try different methods to improve it. I don't see much practical use though.

like image 39
user3654766 Avatar answered Oct 11 '22 18:10

user3654766