求助TypeError: only length-1 arrays can be converted to Pyth
opencv吧
全部回复
仅看楼主
level 3
1034218471 楼主
我想先对视频用KNN背景分割器进行运动目标检测
然后在运动目标上进行特征检测
代码:
#-*-coding:utf-8-*-
import cv2
import numpy as np
camera = cv2.VideoCapture('3.mp4')
bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
es =cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 4))
kernel = np.ones((5, 5), np.uint8)
#bgr模型判断,图像二值化
def detection(img_bgr):
rows,cols = img_bgr.shape[:2]#列和行
hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)
b, g, r = cv2.split(img_bgr)
h, s, v = cv2.split(hsv)
#for(i,j) in zip([x1,x1+w-1],[y1,y1+h-1]):
for i in range(x1,x1+w):
for j in range(y1,y1+h):
if g[i,j]<200 and b[i,j]<150 and r[i,j]>220:
img[i, j]=255
else:img[i, j] = 0
return img
while True:
ret, img_bgr= camera.read()
img = img_bgr.copy()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# KNN背景分割器
fgmask =bs.apply(img)
th = cv2.threshold(fgmask.copy(),244,255,cv2.THRESH_BINARY)[1]
eroded = cv2.erode(th,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)),iterations=2)
dilated = cv2.dilate(eroded,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15,10)),iterations=2)
image,contours,hier = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for c1 in contours:
(x1, y1, w, h) = cv2.boundingRect(c1)
cv2.rectangle(img_bgr, (x1, y1), (x1+ w, y1 + h), (255, 255, 0), 2)
img_flame =flame_detection(img_bgr)
运行时出现了错误:
第23行
for j in range(y1,y1+h):
TypeError: only length-1 arrays can be converted to Python scalars
2018年01月14日 16点01分 1
1