我在rpi上一直在使用python打开opencv。 我试图使用一个需要每个参数为正的函数,但我想把变量保持在可以手动更改的正范围内。但是,它继续返回断言失败,因为我猜每个参数都不能被确认为正数。
有没有办法让这段代码工作或绕道这个断言失败了?提前谢谢!
import numpy as np
import cv2
from matplotlib import pyplot as plt
def nothing(x):
pass
img = cv2.imread('highway2.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.namedWindow('image')
cv2.createTrackbar('number', 'image', 0, 100, nothing)
cv2.createTrackbar('quality', 'image', 0, 100, nothing)
cv2.createTrackbar('mindist', 'image', 0, 100, nothing)
while True:
n = cv2.getTrackbarPos('number', 'image')
q = cv2.getTrackbarPos('quality', 'image')
m = cv2.getTrackbarPos('mindist', 'image')
corners = cv2.goodFeaturesToTrack(gray, n, float(q/100), m)
corners = np.int0(corners)
for i in corners:
x,y = i.ravel()
cv2.circle(img,(x,y),3,255,-1)
plt.imshow('image',img),plt.show()
if cv2.waitKey(0)&0xFF == 27:
break
cv2.destroyAllWindows()
OpenCV Error: Assertion failed (qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0) in goodFeaturesToTrack, file /home/pi/opencv-3.0.0/modules/imgproc/src/featureselect.cpp, line 270
Traceback (most recent call last):
File "detector1.py", line 21, in <module>
corners = cv2.goodFeaturesToTrack(gray, n, float(q/100), m)
cv2.error: /home/pi/opencv-3.0.0/modules/imgproc/src/featureselect.cpp:270: error: (-215) qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 in function goodFeaturesToTrack
答案 0 :(得分:2)
请注意,您正在获取Python回溯。这意味着您可以使用Python异常处理代码处理此问题。尝试用以下内容替换goodFeaturesToTrack
行:
try:
corners = cv2.goodFeaturesToTrack(gray, n, float(q/100), m)
except cv2.error as e:
print("goodFeaturesToTrack({}, {}, {}, {}) failed with {}".format(
gray, n, float(q/100), m), e,
))
另请注意float(q/100)
可能无法按您的想法行事。如果您想确保浮点除法(而不是floor / int除法),请尝试q / 100.0
或float(q) / 100
。
答案 1 :(得分:0)
替换@ Jean-Paul的答案,您可以通过使用带有continue
的简单if-else块来处理此问题,以便在发生错误时跳过下一个语句
if m>0 and n>0 and q>0 and gray.shape[0]>0 and gray.shape[1]>0:
corners = cv2.goodFeaturesToTrack(gray, n, float(q/100), m)
else:
print "incorrect parameters"
continue