前提・実現したいこと
pythonでpcカメラで動画を撮影し、縦横16pxごとに区切って、勾配計算をした画像(laplacian)と背景差分計算をした画像(fgmask)を作っています。背景は黒くマスクしてエッジを赤くするプログラムを作っています。
自分が手をつけたのはfgmask = fgbg.apply(frame)以降なので、そのどこかに間違いがあるはずですが、エラーメッセージが下記のものが表示されてしまいます。
発生している問題・エラーメッセージ
rows, cols, channels = frame.shape AttributeError: 'NoneType' object has no attribute 'shape'
該当のソースコード
python
import numpy as np import cv2 REGION_WIDTH=16 REGION_HIGH=16 cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) fgbg = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16, detectShadows=True) red_mask = np.full((1, 1, 3), (0,0,255), dtype=np.uint8) white_mask = np.full((REGION_HIGH, REGION_WIDTH, 3), (255,255,255), dtype=np.uint8) while(True): ret, frame = cap.read() rows, cols, channels = frame.shape#この行がエラーメッセージで表示されます # maskを定義 laplacian_mask = np.zeros((rows, cols, channels), dtype=np.uint8) fgmask_mask = np.zeros((rows, cols, channels), dtype=np.uint8) frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) laplacian = cv2.Laplacian(frame_gray, cv2.CV_64F) for y in range(rows): for x in range(cols): if laplacian[y,x] > 10: laplacian_mask[y,x] = np.copy(red_mask) #ここから自分が手をつけました fgmask = fgbg.apply(frame) fgmask_sum = np.zeros((30,40)) for i in range(int(rows/REGION_HIGH)): for j in range(int(cols/REGION_WIDTH)): for y in range(REGION_HIGH): for x in range(REGION_WIDTH): if (fgmask[i*16+y][j*16+x]==255): fgmask_sum[y,x] += 1 fgmask_mean = np.sum(fgmask_sum)/30*40 for y in range(int(rows/REGION_HIGH)): for x in range(int(cols/REGION_WIDTH)): if fgmask_sum[x][y] >= fgmask_mean: fgmask_mask[16*y:16*y+16,16*x:16*x+16] = np.copy(white_mask) #ここまでの自分が手をつけた部分を消すとエラーは発生しません mask_or = cv2.bitwise_or(laplacian_mask, fgmask_mask) masked_frame = cv2.bitwise_and(frame, mask_or) cv2.imshow('frame', masked_frame) k = cv2.waitKey(30) & 0xff if k == 27: break # 'ESC' key is pressed cap.release() cv2.destroyAllWindows()
まだ回答がついていません
会員登録して回答してみよう