请检查此代码:
import cv2
import numpy as np
import numpy.linalg as la
import time
cap = cv2.VideoCapture("3.mp4")
fgbg = cv2.createBackgroundSubtractorMOG2()
while(True):
_, frame = cap.read()
if _ == False:
print("No frame left")
break
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
fgmask = fgbg.apply(gray_frame)
fgmask = cv2.resize(fgmask, (640, 360))
gray_frame = cv2.medianBlur(fgmask, 3)
#gray_frame = cv2.GaussianBlur(gray_frame, (3, 3), 5)
erode_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilate_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
erosion = cv2.erode(gray_frame, erode_element, iterations = 3)
dilation = cv2.dilate(erosion, dilate_element, iterations = 2)
cv2.imshow("sub", dilation)
cv2.imshow('abc', fgmask)
rows = gray_frame.shape[0]
circles = cv2.HoughCircles(dilation, cv2.HOUGH_GRADIENT, 1, rows / 0.01, 200, 100, 20, 10)
print(circles)
if circles is not None:
for i in circles[0, :]:
center = (i[0], i[1])
cv2.circle(frame, center, 5, (0,0,255))
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
问题所在的行:
circles = cv2.HoughCircles(dilation, cv2.HOUGH_GRADIENT, 1, rows / 0.01, 200, 100, 20, 10)
错误是:
center = (i[0], i[1])
IndexError: invalid index to scalar variable.
我不知道发生了什么事。霍夫圆的参数取自opencv文档。我想玩转它,但连它都无法运行 一次。
谢谢。
答案 0 :(得分:1)
只需检查一维np.array。
看着this的答案只是改变
if circles is not None:
收件人
if circles is not None and circles[0][0].ndim == 1: