Skip to content

Commit d49462e

Browse files
committed
ch4-ch9
1 parent 091ed4c commit d49462e

16 files changed

+310
-0
lines changed

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__author__ = 'play'

ch4/4.1_imread_imshow.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import cv2
5+
6+
# Load an color image in grayscale
7+
img = cv2.imread('messi5.jpg',0)
8+
9+
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
10+
cv2.imshow('image',img)
11+
cv2.waitKey(0)
12+
cv2.destroyAllWindows()
13+
14+
#
15+
cv2.imwrite('messigray.png',img)

ch4/4.imread_imshow_imwrite.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
import cv2
3+
4+
img = cv2.imread('messi5.jpg',0)
5+
cv2.imshow('image',img)
6+
k = cv2.waitKey(0)
7+
if k == 27: # wait for ESC key to exit
8+
cv2.destroyAllWindows()
9+
elif k == ord('s'): # wait for 's' key to save and exit
10+
cv2.imwrite('messigray.png',img)
11+
cv2.destroyAllWindows()

ch4/4.matplotlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import numpy as np
2+
import cv2
3+
from matplotlib import pyplot as plt
4+
5+
img = cv2.imread('messi5.jpg',0)
6+
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
7+
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
8+
plt.show()

ch4/messi5.jpg

71.2 KB
Loading

ch4/messigray.png

105 KB
Loading

ch5/5.VideoCapture.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Jan 3 21:06:22 2014
4+
5+
@author: duan
6+
"""
7+
8+
import numpy as np
9+
import cv2
10+
11+
cap = cv2.VideoCapture(0)
12+
# while (True):
13+
while (cap.isOpened()):
14+
# Capture frame-by-frame
15+
ret, frame = cap.read()
16+
17+
# Our operations on the frame come here
18+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
19+
20+
# Display the resulting frame
21+
cv2.imshow('frame', gray)
22+
if cv2.waitKey(1) & 0xFF == ord('q'):
23+
break
24+
25+
# When everything done, release the capture
26+
cap.release()
27+
cv2.destroyAllWindows()

ch5/5.VideoPlay.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
import cv2
3+
4+
cap = cv2.VideoCapture('vtest.avi')
5+
6+
while(cap.isOpened()):
7+
ret, frame = cap.read()
8+
9+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
10+
11+
cv2.imshow('frame',gray)
12+
if cv2.waitKey(1) & 0xFF == ord('q'):
13+
break
14+
15+
cap.release()
16+
cv2.destroyAllWindows()

ch5/5.VideoWriter.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
import cv2
3+
4+
cap = cv2.VideoCapture(0)
5+
6+
# Define the codec and create VideoWriter object
7+
fourcc = cv2.VideoWriter_fourcc(*'XVID')#opencv 3.0
8+
#Error: 'module' object has no attribute 'VideoWriter_fourcc'
9+
#
10+
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
11+
12+
while(cap.isOpened()):
13+
ret, frame = cap.read()
14+
if ret==True:
15+
frame = cv2.flip(frame,0)
16+
17+
# write the flipped frame
18+
out.write(frame)
19+
20+
cv2.imshow('frame',frame)
21+
if cv2.waitKey(1) & 0xFF == ord('q'):
22+
break
23+
else:
24+
break
25+
26+
# Release everything if job is finished
27+
cap.release()
28+
out.release()
29+
cv2.destroyAllWindows()

ch5/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__author__ = 'play'

0 commit comments

Comments
 (0)