やりたいこと
検出した物体(ボール)が地面に落ちたことを検出したい
質問内容
映像にラインを引き、検出した物体(ボール)がラインと重なったかどうかの
判断をしたいのですがどうすればいいですか?
作成したプログラム
カメラからの映像を取得し、限定した範囲だけ物体検出するプログラムを作成しました。
このプログラムに付け加える形にしたいです。
Python
1import cv2 2import sys 3import numpy as np 4 5xmin,xmax = 112, 483 # 左上,右下のX座標 6ymin,ymax = 172, 408 # 左上,右下のy座標 7 8def dilation(dilationSize, kernelSize, img): # 膨張した画像にして返す 9 kernel = np.ones((kernelSize, kernelSize), np.uint8) 10 element = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * dilationSize + 1, 2 * dilationSize + 1), (dilationSize, dilationSize)) 11 dilation_img = cv2.dilate(img, kernel, element) 12 return dilation_img 13 14 15def detect(gray_diff, thresh_diff=100, dilationSize=9, kernelSize=20): # 一定面積以上の物体を検出 16 retval, black_diff = cv2.threshold(gray_diff, thresh_diff, 255, cv2.THRESH_BINARY) # 2値化 17 dilation_img = dilation(dilationSize, kernelSize, black_diff) # 膨張処理 18 img = dilation_img.copy() 19 image, contours, hierarchy = cv2.findContours(dilation_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE,offset=(xmin,ymin)) # 境界線検出 20 21 ball_pos = [] 22 23 for i in range(len(contours)): # 重心位置を計算 24 count = len(contours[i]) 25 area = cv2.contourArea(contours[i]) # 面積計算 26 x, y = 0.0, 0.0 27 for j in range(count): 28 x += contours[i][j][0][0] 29 y += contours[i][j][0][1] 30 31 x /= count 32 y /= count 33 x = int(x) 34 y = int(y) 35 ball_pos.append([x, y]) 36 37 return ball_pos, img 38 39 40def displayCircle(image, ballList, thickness=5): 41 for i in range(len(ballList)): 42 x = int(ballList[i][0]) 43 y = int(ballList[i][1]) 44 cv2.circle(image, (x, y), 10, (0, 0, 255), thickness) 45 return image 46 47 48def resizeImage(image, w=2, h=2): 49 height = image.shape[0] 50 width = image.shape[1] 51 resizedImage = cv2.resize(image, (int(width / w), int(height / h))) 52 return resizedImage 53 54 55def blackToColor(bImage): 56 colorImage = np.array((bImage, bImage, bImage)) 57 colorImage = colorImage.transpose(1, 2, 0) 58 return colorImage 59 60 61video = cv2.VideoCapture(0) # videoファイルを読み込む 62# fourcc = cv2.VideoWriter_fourcc(*'MJPG') 63fourcc = cv2.VideoWriter_fourcc(*'XVID') 64 65if not video.isOpened(): # ファイルがオープンできない場合の処理. 66 print("Could not open video") 67 sys.exit() 68 69vidw = video.get(cv2.CAP_PROP_FRAME_WIDTH) 70vidh = video.get(cv2.CAP_PROP_FRAME_HEIGHT) 71 72 73ok, frame = video.read() # 最初のフレームを読み込む 74if not ok: 75 print('Cannot read video file') 76 sys.exit() 77 78frame_pre = frame.copy() 79frame_pre[ymin:ymax,xmin:xmax] 80 81while True: 82 ok, frame = video.read() # フレームを読み込む 83 if not ok: 84 break 85 frame_next = frame.copy() 86 87 color_diff = cv2.absdiff(frame_next[ymin:ymax, xmin:xmax], frame_pre[ymin:ymax, xmin:xmax]) # フレーム間の差分計算 88 gray_diff = cv2.cvtColor(color_diff, cv2.COLOR_BGR2GRAY) # グレースケール変換 89 retval, black_diff = cv2.threshold(gray_diff,30, 255, cv2.THRESH_BINARY) 90 91 ball, dilation_img = detect(gray_diff) 92 93 frame = displayCircle(frame, ball, 2) # 丸で加工 94 95 cv2.imshow("Tracking", frame) # フレームを画面表示 96 cv2.imshow("a",color_diff) 97 98 frame_pre = frame_next.copy() # 次のフレームの読み込み 99 frame_pre[ymin:ymax, xmin:xmax] 100 101 k = cv2.waitKey(10) & 0xff # ESCを押したら中止 102 if k == 27: 103 break 104 105video.release() 106cv2.destroyAllWindows()
回答2件
あなたの回答
tips
プレビュー