Search code examples
pythonopencvfor-loopwhile-loophough-transform

IF loop inside a function in python


I have a function that I would like to perform an if statement inside of. I know this is not possible in Python so I have added a while loop before I implement the if statement but it seems like the while loop is not stopping, or perhaps it maybe another issue.

def hough(frame): 
    
    #calculate the median 
    medianFrame = np.median(randframes,axis=0).astype(dtype=np.uint8)
    grayMedianFrame = cv2.cvtColor(medianFrame, cv2.COLOR_BGR2GRAY)
    
    gframe = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #grayscale frame
    dframe = cv2.absdiff(gframe, grayMedianFrame)  #remove background

    blurred = cv2.GaussianBlur(dframe,(11,11),cv2.BORDER_DEFAULT)  #Gausian blur with standard deviation of 11
    
    while True: 
        
        circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT,1,120,param1= 50, param2=30,minRadius=0,maxRadius=0)
        
        if circles is None: 
            radius.append(0)
            continue
        
        else: 
            circles = np.uint16(np.around(circles))
            
            for j in circles[0, :]: 
            
                # draw the outer circle
                cv2.circle(frame, (j[0], j[1]), j[2], (0, 0, 255), 2)
                # draw the center of the circle
                cv2.circle(frame, (j[0], j[1]), 2, (0, 255, 0), 9)
                
                radius.append(circles[0][0][2])
     
       break 

    return frame

The varaibles not defined inside the function have been done so but for the purpose of simplicity I have not included them.

EDIT: I have made some changes thanks to the comments, however the issue still exists

EDIT 2: The code works fine but it seems to be an issue when cirlces return None.


Solution

  • I am assume at the Moment that you just want a working Hough Transform function. There is a good example from OpenCV itself. Providing solutions for Java, C++ and python:

    Hough Transform