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'

ch6/6.draw.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
import numpy as np
3+
import cv2
4+
5+
# Create a black image
6+
img = np.zeros((512, 512, 3), np.uint8)
7+
8+
9+
# Draw a diagonal blue line with thickness of 5 px
10+
cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
11+
12+
cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
13+
14+
cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)
15+
16+
cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)
17+
18+
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32)
19+
pts = pts.reshape((-1, 1, 2))
20+
# 这里 reshape 的第一个参数为-1, 表明这一维的长度是根据后面的维度的计算出来的。
21+
22+
font=cv2.FONT_HERSHEY_SIMPLEX
23+
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2)
24+
25+
winname = 'example'
26+
cv2.namedWindow(winname, 0)
27+
cv2.imshow(winname, img)
28+
cv2.waitKey(0)
29+
cv2.destroyAllWindows()

ch7/7.MouseCallback.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
5+
6+
# mouse callback function
7+
8+
9+
def draw_circle(event, x, y, flags, param):
10+
if event == cv2.EVENT_LBUTTONDBLCLK:
11+
cv2.circle(img, (x, y), 100, (255, 0, 0), -1)
12+
13+
14+
# 创建图像与窗口并将窗口与回调函数绑定
15+
img = np.zeros((512, 512, 3), np.uint8)
16+
cv2.namedWindow('image',0)
17+
cv2.setMouseCallback('image', draw_circle)
18+
while (1):
19+
cv2.imshow('image', img)
20+
if cv2.waitKey(20) & 0xFF == 27:
21+
break
22+
cv2.destroyAllWindows()

ch7/7.draw_circle_rectangle.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
# 当鼠标按下时变为 True
5+
drawing = False
6+
# 如果 mode 为 true 绘制矩形。按下'm' 变成绘制曲线。 mode=True
7+
ix, iy = -1, -1
8+
9+
10+
# 创建回调函数
11+
def draw_circle(event, x, y, flags, param):
12+
global ix, iy, drawing, mode
13+
# 当按下左键是返回起始位置坐标
14+
if event == cv2.EVENT_LBUTTONDOWN:
15+
drawing = True
16+
ix, iy = x, y
17+
# 当鼠标左键按下并移动是绘制图形。event 可以查看移动,flag 查看是否按下
18+
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
19+
if drawing == True:
20+
if mode == True:
21+
cv2.rectangle(img, (ix, iy), (x, y), (0, 255, 0), -1)
22+
else:
23+
# 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
24+
cv2.circle(img, (x, y), 3, (0, 0, 255), -1)
25+
# 下面注释掉的代码是起始点为圆心,起点到终点为半径的
26+
# r = int(np.sqrt((x - ix) ** 2 + (y - iy) ** 2))
27+
# cv2.circle(img, (x, y), r, (0, 0, 255), -1)
28+
# 当鼠标松开停止绘画。
29+
elif event == cv2.EVENT_LBUTTONUP:
30+
drawing == False
31+
# if mode == True:
32+
# cv2.rectangle(img, (ix, iy), (x, y), (0, 255, 0), -1)
33+
# else:
34+
# cv2.circle(img, (x, y), 5, (0, 0, 255), -1)
35+
36+
37+
#
38+
img = np.zeros((512, 512, 3), np.uint8)
39+
mode=False
40+
41+
cv2.namedWindow('image', 0)
42+
cv2.setMouseCallback('image', draw_circle)
43+
while (1):
44+
cv2.imshow('image', img)
45+
k = cv2.waitKey(1) & 0xFF
46+
if k == ord('m'):
47+
mode = not mode
48+
elif k == 27:
49+
break

ch8/8.Trackbar_draw.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
5+
6+
def nothing(x):
7+
pass
8+
9+
10+
# 当鼠标按下时变为 True
11+
drawing = False
12+
# 如果 mode 为 true 绘制矩形。按下'm' 变成绘制曲线。 mode=True
13+
ix, iy = -1, -1
14+
15+
16+
# 创建回调函数
17+
def draw_circle(event, x, y, flags, param):
18+
r = cv2.getTrackbarPos('R', 'image')
19+
g = cv2.getTrackbarPos('G', 'image')
20+
b = cv2.getTrackbarPos('B', 'image')
21+
color = (b, g, r)
22+
23+
global ix, iy, drawing, mode
24+
# 当按下左键是返回起始位置坐标
25+
if event == cv2.EVENT_LBUTTONDOWN:
26+
drawing = True
27+
ix, iy = x, y
28+
# 当鼠标左键按下并移动是绘制图形。event 可以查看移动,flag 查看是否按下
29+
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
30+
if drawing == True:
31+
if mode == True:
32+
cv2.rectangle(img, (ix, iy), (x, y), color, -1)
33+
else:
34+
# 绘制圆圈,小圆点连在一起就成了线,3 代表了笔画的粗细
35+
cv2.circle(img, (x, y), 3, color, -1)
36+
# 下面注释掉的代码是起始点为圆心,起点到终点为半径的
37+
# r=int(np.sqrt((x-ix)**2+(y-iy)**2))
38+
# cv2.circle(img,(x,y),r,(0,0,255),-1)
39+
40+
# 当鼠标松开停止绘画。
41+
elif event == cv2.EVENT_LBUTTONUP:
42+
drawing == False
43+
# if mode==True:
44+
# cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
45+
# else:
46+
# cv2.circle(img,(x,y),5,(0,0,255),-1)
47+
48+
49+
img = np.zeros((512, 512, 3), np.uint8)
50+
mode=False
51+
52+
cv2.namedWindow('image')
53+
cv2.createTrackbar('R', 'image', 0, 255, nothing)
54+
cv2.createTrackbar('G', 'image', 0, 255, nothing)
55+
cv2.createTrackbar('B', 'image', 0, 255, nothing)
56+
cv2.setMouseCallback('image', draw_circle)
57+
while (1):
58+
cv2.imshow('image', img)
59+
k = cv2.waitKey(1) & 0xFF
60+
if k == ord('m'):
61+
mode = not mode
62+
elif k == 27:
63+
break

ch8/8.createTrackbar.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import cv2
4+
import numpy as np
5+
6+
def nothing(x):
7+
pass
8+
9+
# Create a black image, a window
10+
img = np.zeros((300,512,3), np.uint8)
11+
cv2.namedWindow('image')
12+
13+
# create trackbars for color change
14+
cv2.createTrackbar('R','image',0,255,nothing)
15+
cv2.createTrackbar('G','image',0,255,nothing)
16+
cv2.createTrackbar('B','image',0,255,nothing)
17+
18+
# create switch for ON/OFF functionality
19+
switch = '0 : OFF \n1 : ON'
20+
cv2.createTrackbar(switch, 'image',0,1,nothing)
21+
22+
while(1):
23+
cv2.imshow('image',img)
24+
k = cv2.waitKey(1) & 0xFF
25+
if k == 27:
26+
break
27+
28+
# get current positions of four trackbars
29+
r = cv2.getTrackbarPos('R','image')
30+
g = cv2.getTrackbarPos('G','image')
31+
b = cv2.getTrackbarPos('B','image')
32+
s = cv2.getTrackbarPos(switch,'image')
33+
34+
if s == 0:
35+
img[:] = 0
36+
else:
37+
img[:] = [b,g,r]
38+
39+
cv2.destroyAllWindows()

data/messi5.jpg

71.2 KB
Loading

0 commit comments

Comments
 (0)