[英]IF loop inside a function in python
I have a function that I would like to perform an if
statement inside of.我有一个 function ,我想在其中执行一个
if
语句。 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.我知道这在 Python 中是不可能的,所以我在实现
if
语句之前添加了一个while
循环,但似乎while
循环没有停止,或者可能是另一个问题。
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. function 中未定义的变量已经完成,但为了简单起见,我没有包括它们。
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
.编辑 2 :代码工作正常,但当
cirlces
返回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
不是一个循环,其次我们无法理解什么时候打破 While 循环,我正在简化你的问题,这里是示例代码.
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
在这种情况下也不需要
continue
,因为在if
没有更多代码在while
内运行之后
Before replying, let me point that if
command refers to a statement, and not a loop (more infos here )在回复之前,让我指出
if
命令指的是一个语句,而不是一个循环(更多信息在这里)
The continue statement is used to skip the rest of the code inside a loop for the current iteration only.
continue 语句用于跳过循环内代码的 rest,仅用于当前迭代。
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.在您的特定情况下,当 if 语句的条件为 True 时,您将 append 值 0 写入半径列表,然后继续进行 while 循环的下一次迭代。 Since the loop is
while True:
it continues infinitely.由于循环是
while True:
它无限地继续。 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.如果您想要实现的是在满足 if 语句的条件时退出循环,那么一种方法可能是使用
break
运算符而不是continue
运算符。
I am assume at the Moment that you just want a working Hough Transform function.我现在假设您只想要一个有效的霍夫变换 function。 There is a good example from OpenCV itself.
OpenCV 本身就是一个很好的例子。 Providing solutions for
Java
, C++
and python
:为
Java
、 C++
和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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.