forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit_images.py
executable file
·61 lines (50 loc) · 1.64 KB
/
split_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# *^_^* coding:utf-8 *^_^*
"""
提取图片中的目标区域
USAGE: click with left button the image show the clip image.
press 's' save the clip in current frame, while press 'n', go to next frame
"""
from __future__ import print_function
__author__ = 'stone'
__date__ = '16-1-21'
import cv2
import numpy as np
VIDEO_SRC = '/home/stone/Code/FlameSmokeDetect/medias/videos/CTC_FG.028_9_320x240.avi'
SIZE = (40, 40, 3)
def get_clip(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONUP:
p1 = (x - SIZE[0] / 2, y - SIZE[1] / 2)
p2 = (x + SIZE[0] / 2, y + SIZE[1] / 2)
temp = frame[p1[1]:p2[1], p1[0]:p2[0]]
clip[:, :] = temp
cv2.imshow('clip', clip)
if __name__ == '__main__':
print(__doc__)
frame_count = 0
clip = np.zeros(SIZE, np.uint8)
cap = cv2.VideoCapture(VIDEO_SRC)
cv2.namedWindow('frame')
cv2.setMouseCallback('frame', get_clip)
while True:
block_count = 0
ret, frame = cap.read()
if frame is None:
print("The End!!!")
break
cv2.imshow('frame', frame)
print('current frame: %d' % frame_count)
k = cv2.waitKey(0) & 0xFF
if k == ord('s'):
while True:
clip_name = 'clips/n-clip%d-%d.jpg' % (
frame_count, block_count)
cv2.imwrite(clip_name, clip)
print('saved %s' % clip_name)
block_count += 1
sk = cv2.waitKey(0) & 0xFF
if sk == ord('n'):
break
elif k == 27:
break
frame_count += 1
cv2.destroyAllWindows()