pythonopencvcomputer-visionhough-transform

OpenCV : Vehicle axle detection


I tried to detect vehicle axles in an image. Here are the steps that I followed.

For canny the threshold1, threshold2 parameters are varied using track bar and for Hough circles param1, param2, minDist are varied. By param1 and param2 has no impact on detection.

Original: Original Image

Median: Median Canny: Canny

Detected cirlce:enter image description here

Problem : As you can see in the last image, it detects the circles and it is little away from the original axle. Is there any other parameters that I need to change or consider ?

Code

import cv2
import numpy as np

def readAndResize(image,a):
    imag = cv2.imread(image,a)
    org = cv2.imread(image)
    #resixe the image
    small = cv2.resize(imag, (0,0), fx=0.3, fy=0.3)
    small2 = cv2.resize(org, (0,0), fx=0.3, fy=0.3)
    height, width = small.shape
    img = small[height - height/3:height,0:width]
    org2 = small2[height - height/3:height,0:width]
    return img, org2


img,org2 = readAndResize('5.jpg',0)
cv2.imshow('Original',org2)
gray = cv2.cvtColor(org2, cv2.COLOR_BGR2GRAY)
med = cv2.medianBlur(gray,5)
canny = cv2.Canny(med,100,50)
circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,92,20,10,minRadius=4,maxRadius=0)
circles = np.uint16(np.around(circles))

for i in circles[0,:]:
    cv2.circle(org2,(i[0],i[1]),i[2],(0,255,0),2)
    cv2.circle(org2,(i[0],i[1]),2,(0,50,255),3)
    cv2.imshow('Median',med)
    cv2.imshow('canny',canny)
    cv2.imshow('Detected',org2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Solution

  • Sorry but i didn't used your code. Here is some other which can help you :

    import cv2
    import cv2.cv as cv
    import numpy as np
    import sys
    
    img = cv2.imread('5.jpg',0)
    img = cv2.medianBlur(img,5)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,240,
                                   param1=250,
                                   param2=50,
                                   minRadius=5,
                                   maxRadius=200)
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) 
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),1)
    print circles
    cv2.imshow('circles',cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Play with parameters 240 param1 param2 minRadius maxRadius Additionally what i got with this code Sample