【发布时间】:2019-01-07 14:18:10
【问题描述】:
我正在使用 Python 和 OpenCV 制作实时反馈程序:网络摄像头将观察一个过程并根据正在发生的事情生成反馈。
这是我的代码:
points = get_points_xml()
rect = cv2.boundingRect(np.array(points))
x, y, w, h = rect
cap = cv2.VideoCapture(0)
while (True):
ret, frame = cap.read()
cropped = frame[y: y + h, x: x + w]
ycb = cv2.cvtColor(cropped.copy(), cv2.COLOR_BGR2YCrCb)
y, cr, br = cv2.split(ycb)
blur = cv2.blur(y, (5, 5))
size = blur.size
ret, thresh = cv2.threshold(blur, 60, 255, cv2.THRESH_BINARY)
saturated, area_sat = get_saturated(thresh)
print(saturated, "pixels", area_sat, "cm2")
ret, thresh = cv2.threshold(blur, 80, 140, cv2.THRESH_BINARY_INV)
empty, area_empty = get_saturated(thresh)
print(empty, "pixels", area_empty, "cm2")
unsaturated = size - saturated - empty
area_unsat = unsaturated/cm2
print(unsaturated, "pixels", area_unsat, "cm2")
if (cv2.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
执行行
cropped = frame[y: y + h, x: x + w]
导致以下错误:
TypeError: only integer scalar arrays can be converted to a scalar index
这很奇怪,因为在 while 循环的“第一轮”中我的程序运行良好,然后在 while 循环的“第二轮”中它提示我上述错误。
这可能是什么原因造成的?
【问题讨论】:
-
第一次和第二次迭代有什么不同:
frame?没有。h?没有。x?没有。w?没有。y?是的,它被重新分配。在第一次迭代中,它可能是矩形的垂直位置,从第二次迭代开始,它是颜色的 Y 分量。这两件事应该命名不同。
标签: python-3.x opencv error-handling roi