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
.
I can not comment so writing it here, Your requirement is not so much clear, first of all if
is not a loop, secondly there is nothing we can understand when to break the While loop, i am simplifying your issue, here is sample Code.
if circles is None:
radius.append(0)
continue
else:
break
The continue
in this situation is also not necessary since after the if
no more code to run inside the while
Before replying, let me point that if
command refers to a statement, and not a loop (more infos here )
According to here :
The continue statement is used to skip the rest of the code inside a loop for the current iteration only.
In your specific case, when the condition of the if statement is True, you append the value 0 to the radius list and then you continue with the next iteration of the while loop. Since the loop is while True:
it continues infinitely. If what you wanted to achieve was to exit from the loop when the condition on the if statement is achieved, then one way may be to use the break
operator instead the continue
one.
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
:
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
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT,1,120,param1= 50, param2=30,minRadius=0,maxRadius=0)
if circles is None:
return [0]
else:
circles = np.uint16(np.around(circles))
output = []
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)
output.append(circles[0][0][2])
return output
## main program
radius = []
for frame in frames:
radius.append(hough(frame))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.