12343954
12343954

Reputation: 2631

How to detect the inner radius and outer radius of this circle in python?

i want to detect the radius of two circles in mask image, but cv2.HoughCircles can not detect these two circles.

what should i do? thanks in advance.

enter image description here

here is the code:

import cv2
import numpy as np

track_win = 'Tracking'

def nothing(x):
    pass
    
# use track bar to perfectly define (1/2)
# the lower and upper values for HSV color space(2/2)
cv2.namedWindow(track_win)
cv2.resizeWindow(track_win, 1000, 500)
cv2.moveWindow(track_win, 1920, 100)
#1 Lower/Upper HSV 3 startValue 4 endValue
cv2.createTrackbar("LH",track_win,35,255,nothing)
cv2.createTrackbar("LS",track_win,43,255,nothing)
cv2.createTrackbar("LV",track_win,46,255,nothing)
cv2.createTrackbar("UH",track_win,77,255,nothing)
cv2.createTrackbar("US",track_win,255,255,nothing)
cv2.createTrackbar("UV",track_win,255,255,nothing)
cv2.createTrackbar("min r",track_win,30,1000,nothing)
cv2.createTrackbar("max r",track_win,100,2000,nothing)

cv2.setTrackbarPos("LH", track_win, 4)
cv2.setTrackbarPos("LS", track_win, 39)
cv2.setTrackbarPos("LV", track_win, 126)
cv2.setTrackbarPos("UH", track_win, 46)
cv2.setTrackbarPos("US", track_win, 121)
cv2.setTrackbarPos("UV", track_win, 171)
cv2.setTrackbarPos("min r", track_win, 30)
cv2.setTrackbarPos("max r", track_win, 1000)

kernel = np.ones((4, 4), np.uint8)

while True:
    frame = cv2.imread('./IMG_0645.jpg')
    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

    l_h = cv2.getTrackbarPos("LH",track_win)
    l_s = cv2.getTrackbarPos("LS",track_win)
    l_v = cv2.getTrackbarPos("LV",track_win)
    
    u_h = cv2.getTrackbarPos("UH",track_win)
    u_s = cv2.getTrackbarPos("US",track_win)
    u_v = cv2.getTrackbarPos("UV",track_win)

    m_r = cv2.getTrackbarPos("min r",track_win)
    x_r = cv2.getTrackbarPos("max r",track_win)

    l_g = np.array([l_h, l_s, l_v]) # lower green value
    u_g = np.array([u_h,u_s,u_v])

    mask = cv2.inRange(hsv,l_g,u_g)
    mask = cv2.dilate(mask, kernel, iterations = 1) # dilate
    mask = cv2.erode(mask, kernel, iterations=1)    # erode

    mask = cv2.Canny(mask, 50, 150) # Canny
    mask = cv2.dilate(mask, kernel, iterations = 1) # dilate
    mask = cv2.erode(mask, kernel, iterations=1)    # erode
    
    res=cv2.bitwise_and(frame,frame,mask=mask) # src1,src2

    #Hough Circles Detection
    circles= cv2.HoughCircles(mask,cv2.HOUGH_GRADIENT,1,100,param1=100,param2=50,minRadius=m_r,maxRadius=m_r) #
    if circles is not None:
        print(len(circles))
        for circle in circles:
            # print(circle)
            if len(circle) == 3:
                (x, y, r) = circle
                # draw the circle in the output image, then draw a rectangle
                # corresponding to the center of the circle
                cv2.circle(res, (x, y), r, (0, 255, 0), 4)
                # cv2.rectangle(res, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
     
    cv2.imshow("frame", frame)
    cv2.moveWindow("frame",100,100)
    cv2.imshow("mask", mask)
    cv2.moveWindow("mask",100+540,100)
    cv2.imshow("res", res)
    cv2.moveWindow("res",100+540*2,100)

    key = cv2.waitKey(1)
    if key == 27: # Esc
        break

cv2.destroyAllWindows()

here is the original image:

enter image description here

Upvotes: 0

Views: 127

Answers (0)

Related Questions