Skip to content

Commit f8f6b2d

Browse files
committed
10-21
1 parent d49462e commit f8f6b2d

33 files changed

+537
-0
lines changed

ch10/10.addWeighted.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
5+
img1 = cv2.imread('../data/ml.png')
6+
img2 = cv2.imread('../data/opencv_logo.jpg')
7+
8+
dst = cv2.addWeighted(img1,0.7,img2,0.3,0)#opencv 3.0
9+
10+
cv2.imshow('dst',dst)
11+
cv2.waitKey(0)
12+
cv2.destroyAllWindows()

ch10/10.bitwise_and.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import cv2
4+
import numpy as np
5+
6+
# Load two images
7+
img1 = cv2.imread('../data/messi5.jpg')
8+
img2 = cv2.imread('../data/opencv_logo.png')
9+
10+
# I want to put logo on top-left corner, So I create a ROI
11+
rows,cols,channels = img2.shape
12+
roi = img1[0:rows, 0:cols ]
13+
14+
# Now create a mask of logo and create its inverse mask also
15+
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
16+
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
17+
mask_inv = cv2.bitwise_not(mask)
18+
19+
# Now black-out the area of logo in ROI
20+
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)#opencv 3.0
21+
22+
# Take only region of logo from logo image.
23+
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
24+
25+
# Put logo in ROI and modify the main image
26+
dst = cv2.add(img1_bg,img2_fg)
27+
img1[0:rows, 0:cols ] = dst
28+
29+
cv2.imshow('res',img1)
30+
cv2.waitKey(0)
31+
cv2.destroyAllWindows()

ch11/11.getTickCount.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
5+
img1 = cv2.imread('../data/ml.jpg')
6+
7+
e1 = cv2.getTickCount()
8+
for i in xrange(5,49,2):
9+
img1 = cv2.medianBlur(img1,i)
10+
e2 = cv2.getTickCount()
11+
t = (e2 - e1)/cv2.getTickFrequency()
12+
print t
13+
# Result I got is 0.521107655 seconds

ch11/11.useOptimized.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
# check if optimization is enabled
5+
"""
6+
In [5]: cv2.useOptimized()
7+
Out[5]: True
8+
In [6]: %timeit res = cv2.medianBlur(img,49)
9+
10 loops, best of 3: 34.9 ms per loop
10+
# Disable it
11+
In [7]: cv2.setUseOptimized(False)
12+
In [8]: cv2.useOptimized()
13+
Out[8]: False
14+
In [9]: %timeit res = cv2.medianBlur(img,49)
15+
"""

ch13/13.VideoCapture_blue_object.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2
3+
import numpy as np
4+
5+
cap = cv2.VideoCapture(0)
6+
while (1):
7+
# 获取每一帧
8+
ret, frame = cap.read()
9+
# 换到 HSV
10+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
11+
# 定蓝色的 值
12+
lower_blue = np.array([110, 50, 50])
13+
upper_blue = np.array([130, 255, 255])
14+
15+
# 根据 值构建掩模
16+
mask = cv2.inRange(hsv, lower_blue, upper_blue)
17+
# 对原图像和掩模位 算
18+
res = cv2.bitwise_and(frame, frame, mask=mask)
19+
# 显示图像
20+
cv2.imshow('frame', frame)
21+
cv2.imshow('mask', mask)
22+
cv2.imshow('res', res)
23+
k = cv2.waitKey(5) & 0xFF
24+
if k == 27:
25+
break
26+
# 关 窗口
27+
cv2.destroyAllWindows()

ch13/13.cvtColor_flag.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import cv2
4+
5+
# flags=[i for in dir(cv2) if i startswith('COLOR_')]
6+
# print flags

ch13/13.find_object_hsv.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+
#wrong
6+
# green=np.uint8([0,255,0])
7+
# print green
8+
# hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
9+
# print hsv_green
10+
11+
12+
#scn (the number of channels of the source),
13+
#i.e. self.img.channels(), is neither 3 nor 4.
14+
#
15+
#depth (of the source),
16+
#i.e. self.img.depth(), is neither CV_8U nor CV_32F.
17+
# 所以不能用 [0,255,0] 而 用 [[[0,255,0]]]
18+
#的三层括号应 分别对应于 cvArray cvMat IplImage
19+
green=np.uint8([[[0,255,0]]])
20+
hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
21+
print hsv_green
22+
#[[[60 255 255]]]

ch14/14.getAffineTransform.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import cv2
4+
import numpy as np
5+
from matplotlib import pyplot as plt
6+
img=cv2.imread('drawing.png')
7+
rows,cols,ch=img.shape
8+
pts1=np.float32([[50,50],[200,50],[50,200]])
9+
pts2=np.float32([[10,100],[200,50],[100,250]])
10+
M=cv2.getAffineTransform(pts1,pts2)
11+
dst=cv2.warpAffine(img,M,(cols,rows))
12+
plt.subplot(121,plt.imshow(img),plt.title('Input'))
13+
plt.subplot(121,plt.imshow(img),plt.title('Output'))
14+
plt.show()

ch14/14.getPerspectiveTransform.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import cv2
4+
import numpy as np
5+
from matplotlib import pyplot as plt
6+
img=cv2.imread('sudokusmall.png')
7+
rows,cols,ch=img.shape
8+
9+
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
10+
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
11+
M=cv2.getPerspectiveTransform(pts1,pts2)
12+
dst=cv2.warpPerspective(img,M,(300,300))
13+
14+
plt.subplot(121,plt.imshow(img),plt.title('Input'))
15+
plt.subplot(121,plt.imshow(img),plt.title('Output'))
16+
plt.show()

ch14/14.getRotationMatrix2D.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import cv2
4+
import numpy as np
5+
6+
img=cv2.imread('../data/messi5.jpg',0)
7+
rows,cols=img.shape
8+
#的第一个参数为旋 中心 第二个为旋 度 第三个为旋 后的缩放因子
9+
# 可以 置旋 中心 缩放因子 以及窗口大小来 止旋 后 出 界的
10+
M=cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)
11+
# 第三个参数是 出图像的尺寸中心
12+
dst=cv2.warpAffine(img,M,(2*cols,2*rows))
13+
while(1):
14+
cv2.imshow('img',dst)
15+
if cv2.waitKey(1)&0xFF==27:
16+
break
17+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)