Last active
April 5, 2017 17:36
-
-
Save endorsement/1fd138b9ea3ff8e4f7fa8430e1df50d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
# cog calculation | |
def cog_calculation(start, end): | |
# 頭出し | |
cap.set(cv2.CAP_PROP_POS_FRAMES, int(start - 1)) | |
# X,Y,AREA,宣言 | |
centroid = [] | |
for num in range(start,end): | |
# フレーム番号取得 | |
curpos = int(cap.get(cv2.CAP_PROP_POS_FRAMES)) + 1 | |
# 1フレーム読む | |
ret, frame = cap.read() | |
# 読めなかったら抜ける | |
if ret == False: | |
break | |
# グレースケール化 | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
# しきい値をセットして二値化処理 | |
ret, bins = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY) | |
# ラベリング処理 | |
nlabels, labelimg, contours, CoGs = cv2.connectedComponentsWithStats(bins) | |
if nlabels > 0: | |
for nlabel in range(1,nlabels): | |
x,y,w,h,size = contours[nlabel] | |
xg,yg = CoGs[nlabel] | |
# 面積フィルタ | |
if size >= 100 and size <= 1000: | |
centroid.append([xg, yg, size, curpos]) | |
print(centroid) | |
# ファイル名を指定する | |
im_addr = './cut.avi' | |
# AVIファイルを読む | |
cap = cv2.VideoCapture(im_addr) | |
# AVIファイルのフレーム数を取得する | |
frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
# cog calculation | |
cog_calculation(1,2000) | |
# AVIファイルを解放 | |
cap.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aviファイルの先頭2000フレーム分を読んで重心位置、面積、フレーム番号を表示するやつ
OpenCV3系でなければ動作しない(connectedComponentWithStats)