Skip to content

Commit 913fdf6

Browse files
committed
Add image and video processing example code
1 parent c70f747 commit 913fdf6

File tree

13 files changed

+770
-0
lines changed

13 files changed

+770
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bouncing_ball_full_[0-9]*.avi
2.36 MB
Loading
668 KB
Loading
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ImageProcessing
2+
Some sample code for image processing using Scikit-Image and video
3+
processing with OpenCV.
4+
5+
## What is it?
6+
1. `denoise.py`: sharpen an image using deconvolution.
7+
1. `segmentation.py`: segmentation using an active cotour model, aka,
8+
snake.
9+
1. `restoration.ipynb`: Jupyter notebook illustrating analysis of a
10+
problematic camera image and custum restoration.
11+
1. `Data`: directory containing image files for illustration purposes.
12+
1. `caputure.py`: capture video from a webcam using OpenCV.
13+
1. `analyze.py`: show a plot of the average RGB values for the frames in
14+
a movie using OpenCV.
15+
1. `split_scenes.py`: very naive utility to split a movie into scences,
16+
based on rapid changes in RGB intenisty.
17+
1. `bouncing_ball_full.avi`: movie to test `scene_spliiter.py` with.
18+
1. `follow_ball.py`: example of using the camshift algorithm to follow
19+
a bouncing ball in a scene.
20+
1. `bouncing_ball.avi`: movie to test `follow_ball.py` with.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
from argparse import ArgumentParser
4+
import cv2
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
if __name__ == '__main__':
9+
arg_parser = ArgumentParser(description='capture video')
10+
arg_parser.add_argument('input', help='file to read from')
11+
options = arg_parser.parse_args()
12+
capture = cv2.VideoCapture(options.input)
13+
counter = 0
14+
try:
15+
densities = []
16+
while (True):
17+
status, frame = capture.read()
18+
if status:
19+
counter += 1
20+
nr_pixels = frame.shape[0]*frame.shape[1]
21+
red_channel = frame[:, :, 0]
22+
green_channel = frame[:, :, 1]
23+
blue_channel = frame[:, :, 2]
24+
densities.append((counter,
25+
red_channel.sum()/nr_pixels,
26+
green_channel.sum()/nr_pixels,
27+
blue_channel.sum()/nr_pixels))
28+
cv2.imshow('frame', frame)
29+
if cv2.waitKey(1) & 0xFF == ord('q'):
30+
break
31+
else:
32+
break
33+
capture.release()
34+
cv2.destroyAllWindows()
35+
densities = np.array(densities).T
36+
plt.plot(densities[0], densities[1], 'r')
37+
plt.plot(densities[0], densities[2], 'g')
38+
plt.plot(densities[0], densities[3], 'b')
39+
plt.xlabel('frame')
40+
plt.ylabel('RGB')
41+
plt.ylim((0, 255))
42+
plt.show()
43+
except KeyboardInterrupt:
44+
capture.release()
45+
cv2.destroyAllWindows()
809 KB
Binary file not shown.
1.9 MB
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
from argparse import ArgumentParser
4+
import cv2
5+
import numpy as np
6+
7+
if __name__ == '__main__':
8+
arg_parser = ArgumentParser(description='capture video')
9+
arg_parser.add_argument('--dev', type=int, default=0,
10+
help='device number to capture')
11+
arg_parser.add_argument('--input', help='file to read from')
12+
arg_parser.add_argument('--output', help='file to write to')
13+
arg_parser.add_argument('--fps', type=float, default=10.0,
14+
help='frames per soecond for recording')
15+
options = arg_parser.parse_args()
16+
if options.input:
17+
capture = cv2.VideoCapture(options.input)
18+
else:
19+
capture = cv2.VideoCapture(options.dev)
20+
if options.output:
21+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
22+
output = cv2.VideoWriter(options.output, fourcc,
23+
options.fps, (640, 480))
24+
counter = 0
25+
try:
26+
while True:
27+
status, frame = capture.read()
28+
if status:
29+
counter += 1
30+
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
31+
if options.output:
32+
output.write(frame)
33+
cv2.imshow('frame', frame)
34+
if cv2.waitKey(1) & 0xFF == ord('q'):
35+
break
36+
else:
37+
break
38+
except KeyboardInterrupt:
39+
capture.release()
40+
if options.output:
41+
output.release()
42+
cv2.destroyAllWindows()
43+
print('captured {0:d} frames'.format(counter))
44+
else:
45+
capture.release()
46+
if options.output:
47+
output.release()
48+
cv2.destroyAllWindows()
49+
print('captured {0:d} frames'.format(counter))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
from scipy.signal import convolve2d
6+
from skimage import color, data, restoration
7+
8+
if __name__ == '__main__':
9+
image = color.rgb2gray(data.astronaut())
10+
point_spread_func = np.ones((5, 5))/25.0
11+
image = convolve2d(image, point_spread_func, 'same')
12+
image += 0.1*image.std()*np.random.standard_normal(image.shape)
13+
denoised, _ = restoration.unsupervised_wiener(image, point_spread_func)
14+
figure, axes = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True,
15+
subplot_kw={'adjustable': 'box-forced'},
16+
figsize=(8,5))
17+
plt.gray()
18+
axes[0].imshow(image, vmin=denoised.min(), vmax=denoised.max())
19+
axes[0].axis('off')
20+
axes[0].set_title('original')
21+
axes[1].imshow(denoised)
22+
axes[1].axis('off')
23+
axes[1].set_title('restored')
24+
figure.tight_layout()
25+
plt.show()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
3+
from argparse import ArgumentParser
4+
import cv2
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
import sys
8+
import time
9+
10+
if __name__ == '__main__':
11+
arg_parser = ArgumentParser(description='follow the yellow ball')
12+
arg_parser.add_argument('input', help='AVI file to use')
13+
arg_parser.add_argument('--row', type=int, default=340,
14+
help='row for ROI')
15+
arg_parser.add_argument('--col', type=int, default=300,
16+
help='dolumn for ROI')
17+
arg_parser.add_argument('--height', type=int, default=210,
18+
help='height for ROI')
19+
arg_parser.add_argument('--width', type=int, default=230,
20+
help='width for ROI')
21+
arg_parser.add_argument('--min-hue', type=int, default=25,
22+
help='minimum hue')
23+
arg_parser.add_argument('--max-hue', type=int, default=50,
24+
help='maximum hue')
25+
arg_parser.add_argument('--iterations', type=int, default=5,
26+
help='number of iterations for tracking')
27+
arg_parser.add_argument('--output', help='output file name')
28+
arg_parser.add_argument('--fps', type=float, default=12.0,
29+
help='frames per second for output')
30+
options = arg_parser.parse_args()
31+
capture = cv2.VideoCapture(options.input)
32+
status, frame = capture.read()
33+
if not status:
34+
capture.release()
35+
sys.exit(1)
36+
if options.output:
37+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
38+
output = cv2.VideoWriter(options.output, fourcc, options.fps,
39+
(frame.shape[1], frame.shape[0]))
40+
else:
41+
output = None
42+
43+
track_window = (options.col, options.row, options.width, options.height)
44+
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
45+
mask = cv2.inRange(hsv_frame,
46+
np.array((options.min_hue, 50, 50)),
47+
np.array((options.max_hue, 255, 255)))
48+
roi_hist = cv2.calcHist([hsv_frame], [0], mask, [50], [0, 50])
49+
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
50+
termination_cond = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
51+
options.iterations, 1)
52+
nr_frames = 1
53+
while True:
54+
status, frame = capture.read()
55+
if status == True:
56+
nr_frames += 1
57+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
58+
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 50], 1)
59+
status, track_window = cv2.meanShift(dst, track_window,
60+
termination_cond)
61+
if not status:
62+
continue
63+
x, y, w, h = track_window
64+
img = cv2.rectangle(frame, (x, y), (x + w, y + h), 255, 2)
65+
cv2.imshow('image', img)
66+
if output:
67+
output.write(img)
68+
if cv2.waitKey(1) % 0xFF == ord('q'):
69+
break
70+
else:
71+
break
72+
73+
print(f'{nr_frames} frames processed')
74+
cv2.destroyAllWindows()
75+
if output:
76+
output.release()
77+
capture.release()

0 commit comments

Comments
 (0)