動画を背景差分化して白と黒で表示された物体の輪郭を長方形で示したいです。
画像はイメージです
人が2人いるのですがそれぞれ長方形で囲むというような感じです。
下のコードを考えたのですが
エラーをどう対処したらいいか教えて欲しいです
import cv2 import numpy as np import random import sys from matplotlib import pyplot as plt cap = cv2.VideoCapture('move3mov.mp4') fgbg=cv2.bgsegm.createBackgroundSubtractorMOG() width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cap.get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') #writer = cv2.VideoWriter('output.avi', fourcc, fps, (width, height)) kernel = np.ones((3, 3), np.uint8) while(cap.isOpened()): ret, frame = cap.read() cv2.imshow('frame',frame) #背景差分化 fgmask = fgbg.apply(frame) contours,hierarchy = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 膨張処理(方法2) dilate = cv2.dilate(fgmask, kernel) cv2.imshow('dilate',dilate) #輪郭をそのまま書き込み color_src = cv2.cvtColor(dilate, cv2.COLOR_GRAY2BGR) areaframe=cv2.drawContours(color_src,contours,-1,(0,255,0),2) #輪郭を四角で囲む。あまり小さい四角は外す。 for cnt in contours: area=cv2.contourArea(cnt) if area >50: x,y,w,h = cv2.boundingRect(cnt) areaframe=cv2.rectangle(areaframe,(x,y),(x+w,y+h),(0,0,255),2) cv2.imshow('all',areaframe) cv2.imshow('fg',fgmask) cv2.waitKey(0) cap.release() #writer.release() cv2.destroyAllWindows()
このようなエラーが出ます
error: (-215:Assertion failed) L.channels() == 1 && I.channels() == 1 in function 'connectedComponents_sub1'
エラーが出ているのは何行目でしょうか?connectedComponents_sub1にgrayしか渡せないのにRGBで渡していることが原因で出るエラーのようなのですが、該当箇所がわかりません。エラー全体をコピー&ペースとしていただきたいです。
あなたの回答
tips
プレビュー