图像基础变换

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
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取
img = plt.imread(r'C:\Users\86181\Desktop\CVLab\lab1\666.jpg')

#平移
rows,cols = img.shape[:2]
M = np.float32([[1,0,100],[0,1,50]])
move = cv2.warpAffine(img,M,(cols,rows))

#缩小
zoom = cv2.resize(img, (200, 200))

#90度旋转
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
revolve = cv2.warpAffine(img,M,(cols,rows))

#翻转
flip = cv2.flip(img,1)

# 展示不同的图片
titles = ['src','move', 'zoom', 'revolve', 'flip']
imgs = [img, move, zoom, revolve, flip]

for i in range(5):
plt.subplot(2,3,i+1)
plt.imshow(imgs[i])
plt.title(titles[i])
plt.show()

pp7Tgb9.png

图像空域滤波

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
import cv2
import matplotlib.pyplot as plt

img = plt.imread(r'C:\Users\86181\Desktop\CVLab\lab2\666.jpg',1)

# 均值滤波
img_mean = cv2.blur(img, (33,33))

# 高斯滤波
img_Guassian = cv2.GaussianBlur(img,(33,33),0)

# 中值滤波
img_median = cv2.medianBlur(img, 33)

# 展示不同的图片
titles = ['srcImg','mean', 'Gaussian', 'median']
imgs = [img, img_mean, img_Guassian, img_median]

for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(imgs[i])
plt.title(titles[i])
plt.show()

pp7Tf4x.png

边缘检测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
import cv2
import matplotlib.pyplot as plt

img = plt.imread(r'C:\Users\86181\Desktop\CVLab\lab2\666.jpg',1)

# Sobel
sobel = cv2.Sobel(img, cv2.CV_64F, 1, 1, 5)
# Laplacian
laplacian = cv2.Laplacian(img, cv2.CV_64F)
# Canny
canny = cv2.Canny(img, 50, 240)

# 展示不同的图片
titles = ['src','sobel', 'laplacian', 'canny']
imgs = [img, sobel, laplacian, canny]

for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(imgs[i].astype('uint8'))
plt.title(titles[i])
plt.show()

pp7THDH.png

特征提取

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
from skimage import feature as ft
import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread(r'C:\Users\86181\Desktop\CVLab\lab4\666.jpg', 0)

#HOG特征提取
hog = ft.hog(img,
orientations=6,
pixels_per_cell=[20,20],
cells_per_block=[2,2],
visualize=True)
img1 = hog[1]


#SIFT特征提取
sift = cv2.SIFT_create()

kp,des = sift.detectAndCompute(img,None)

img2=cv2.drawKeypoints(img,kp,img,color=(255,0,0))


#SIFT特征匹配
img3 = cv2.imread(r'C:\Users\86181\Desktop\CVLab\lab4\666.jpg', 0)
img4 = cv2.imread(r'C:\Users\86181\Desktop\CVLab\lab4\revolve.jpg', 0)

psd_kp1, psd_des1 = sift.detectAndCompute(img3, None)
psd_kp2, psd_des2 = sift.detectAndCompute(img4, None)


FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)

flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(psd_des1, psd_des2, k=2)
goodMatch = []
for m, n in matches:
if m.distance < 0.50*n.distance:
goodMatch.append(m)

goodMatch = np.expand_dims(goodMatch, 1)


img5 = cv2.drawMatchesKnn(img3, psd_kp1, img4, psd_kp2, goodMatch, None, flags=2)


# 展示不同的图片
titles = ['src','hog', 'sift', 'match']
imgs = [img, img1, img2, img5]

for i in range(4):
plt.subplot(1,4,i+1)
plt.imshow(imgs[i].astype('uint8'))
plt.title(titles[i])
plt.show()

pp7TLVA.png