Skip to content

Instantly share code, notes, and snippets.

@macrat
Last active September 29, 2016 02:28
Show Gist options
  • Save macrat/3ce34fe3c6d8f82573dcb78f8ff5e1dc to your computer and use it in GitHub Desktop.
Save macrat/3ce34fe3c6d8f82573dcb78f8ff5e1dc to your computer and use it in GitHub Desktop.
動画から物体を認識して切り出すやつ。
import math
import cv2
import numpy
MOVIE_NUM = 12
count = 0
for i in range(MOVIE_NUM):
print('makeing {0}.mp4 background image'.format(i+1))
video = cv2.VideoCapture('src/{0}.mp4'.format(i+1))
background = numpy.array(video.read()[1], numpy.uint32)
frame_count = 1
while True:
ok, img = video.read()
if not ok:
break
background += img
frame_count += 1
background = numpy.array(background / frame_count, numpy.uint8)
video.release()
video = cv2.VideoCapture('src/{0}.mp4'.format(i+1))
while cv2.waitKey(1):
print('taking {0}/{1}'.format(i, count))
ok, raw = video.read()
if not ok:
break
img = raw.copy()
diff = cv2.cvtColor(cv2.absdiff(background, img), cv2.COLOR_BGR2HSV)
diff = numpy.array(((diff[:,:,1]/255.0 * diff[:,:,2]/255.0) >= 0.2) * 255,
numpy.uint8)
#cv2.imshow('diff', diff)
canny = cv2.Canny(diff, 80, 40)
#cv2.imshow('canny', canny)
contour = numpy.zeros_like(canny)
cnts = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[1]
if len(cnts) > 0:
for cnt in cnts:
c = cv2.minEnclosingCircle(
cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt, True), True)
)
cv2.circle(contour, tuple(map(int, c[0])), int(c[1]), 255, -1)
cnts = cv2.findContours(contour, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[1]
if len(cnts) > 0:
for cnt in cnts:
rect = cv2.boundingRect(
cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt, True), True)
)
if rect[2] > 32 and rect[3] > 32:
found = raw[
max(0, rect[1]-16):min(img.shape[0], rect[1]+rect[3]+16),
max(0, rect[0]-16):min(img.shape[1], rect[0]+rect[2]+16),
]
cv2.imshow('found', found)
cv2.imwrite('out/{0:04d}.png'.format(count), found)
count += 1
#cv2.rectangle(img,
# rect[:2], (rect[0]+rect[2], rect[1]+rect[3]),
# (0, 0, 255), 2)
cv2.imshow('img', img)
video.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment