背景差分法を用いて、差分を二値化処理し、輪郭をcv2.findContoursで抽出し、重心を求めたのですが私が知りたいのは輪郭の最下点から重心までの高さです。輪郭から最下点を求めるにはどのようにすればよいでしょうか?アドバイスよろしくお願いいたします。
以下は現在私が作成したプログラムです。雑なプログラムですがよろしくお願いします
python
1#coding:utf-8 2 3import numpy as np 4import cv2 5import wave 6import winsound as ws 7import math 8 9cap = cv2.VideoCapture(0) 10 11fgbg = cv2.bgsegm.createBackgroundSubtractorGSOC() 12 13# 4近傍の定義 14neiborhood4 = np.array([[0, 1, 0], 15 [1, 1, 1], 16 [0, 1, 0]], 17 np.uint8) 18 19 # 8近傍の定義 20neiborhood8 = np.array([[1, 1, 1], 21 [1, 1, 1], 22 [1, 1, 1]], 23 np.uint8) 24 25def padding_position(x, y, w, h, p): 26 return x - p, y - p, w + p * 2, h + p * 2 27 28 29while True: 30 31 ret, frame = cap.read() 32 33 #グレースケール 34 gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) 35 36 #メディアンフィルタ 37 res_img = cv2.medianBlur(gray,3) 38 39 #ガウシアンフィルタ 40 blur = cv2.GaussianBlur(gray,(5,5),0) 41 42 #背景差分法 43 fgmask1= fgbg.apply(blur) 44 45 46 #2値化処理 47 ret,img_ot = cv2.threshold(fgmask1,128,255,cv2.THRESH_OTSU) 48 49 # 近傍8のオープニング 50 img_dst = cv2.morphologyEx(img_ot, cv2.MORPH_OPEN, neiborhood8) 51 52 # 近傍8のクロージング 53 img_dst = cv2.morphologyEx(img_dst, cv2.MORPH_CLOSE, neiborhood8) 54 55 #輪郭抽出 56 imge, contours, hierarch=cv2.findContours(img_dst,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE) 57 58 59 60 #各輪郭に対する処理 61 for c in contours: 62 63 #矩形作成 64 x,y,w,h = cv2.boundingRect(c) 65 x, y, w, h = padding_position(x, y, w, h, 5) 66 67 #最大面積 68 max_area=[] 69 area = cv2.contourArea(c) 70 max_area.append(area) 71 72 #輪郭描画 73 cv2.drawContours(frame,c, -1,(255,255,255),5) 74 if max(max_area) > 200: 75 cv2.rectangle(frame,(x,y),(x+w, y+h),(0,255,0),5) 76 77 78 maxCont = contours[0] 79 for c in contours: 80 if len(maxCont)<len(c): 81 maxCont=c 82 83 #輪郭の重心を求める 84 mu = cv2.moments(maxCont) 85 x,y= int(mu["m10"]/mu["m00"]) , int(mu["m01"]/mu["m00"]) 86 cv2.circle(frame, (x,y), 4, (0,0,255), 4) 87 88 89 90 #表示 91 cv2.imshow('2',imge) 92 cv2.imshow('frame',frame) 93 94 if cv2.waitKey(30) & 0xff == ord('q'): 95 break 96 97cap.release() 98cv2.destroyAllWindows()

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。