机器学习-图像识别

图像识别技术是人工智能的一个重要领域。它是指对图像进行对象识别,以识别各种不同模式的目标和对像的技术。

图像识别

1
2
3
4
业务数据--------->特征数据->学习模型
| 特征工程 ^
| TFIDF、MFCC、SIFT |
+--------------------------+

OpenCV基础

开源计算机视觉库
图像处理
提取图像特征
针对的图像的机器学习

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 numpy as np
import cv2 as cv
original = cv.imread('../data/forest.jpg')
cv.imshow('Original', original)
blue = np.zeros_like(original)
blue[..., 0] = original[..., 0]
cv.imshow('Blue', blue)
green = np.zeros_like(original)
green[..., 1] = original[..., 1]
cv.imshow('Green', green)
red = np.zeros_like(original)
red[..., 2] = original[..., 2]
cv.imshow('Red', red)
h, w = original.shape[:2]
l, t = int(w / 4), int(h / 4)
r, b = int(w * 3 / 4), int(h * 3 / 4)
cropped = original[t:b, l:r]
cv.imshow('Cropped', cropped)
#scaled = cv.resize(original,
# (int(w / 2), int(h / 2)),
# interpolation=cv.INTER_LINEAR)
scaled = cv.resize(original, None,
fx=2, fy=2,
interpolation=cv.INTER_LINEAR)
cv.imshow('Scaled', scaled)
cv.waitKey();
cv.imwrite('./cropped.bmp', cropped)
cv.imwrite('./cropped.png', cropped)
cv.imwrite('./cropped.jpg', cropped)

边缘检测

1
2
3
4
5
6
7
import cv2 as cv
original = cv.imread('../data/chair.jpg',
cv.IMREAD_GRAYSCALE)
cv.imshow('Original', original)
canny = cv.Canny(original, 50, 240)
cv.imshow('Canny', canny)
cv.waitKey()

亮度提升

直方图均衡化

1
2
3
4
5
6
7
8
9
10
11
12
import cv2 as cv
original = cv.imread('../data/sunrise.jpg')
cv.imshow('Original', original)
gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
eq_gray = cv.equalizeHist(gray)
cv.imshow('EQ-Gray', eq_gray)
yuv = cv.cvtColor(original, cv.COLOR_BGR2YUV)
yuv[..., 0] = cv.equalizeHist(yuv[..., 0])
eq_color = cv.cvtColor(yuv, cv.COLOR_YUV2BGR)
cv.imshow('EQ-Color', eq_color)
cv.waitKey()

角点检测

1
2
3
4
5
6
7
8
9
10
import cv2 as cv
original = cv.imread('../data/box.png',
cv.IMREAD_GRAYSCALE)
cv.imshow('Original', original)
corners = cv.cornerHarris(original, 7, 5, 0.04)
corners = cv.dilate(corners, None)
mixture = original.copy()
mixture[corners > corners.max() * 0.01] = 255
cv.imshow('Mixture', mixture)
cv.waitKey()

STAR特征检测

几何结构

1
2
3
4
5
6
7
8
9
10
11
12
import cv2 as cv
original = cv.imread('../data/table.jpg')
cv.imshow('Original', original)
gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
star = cv.xfeatures2d.StarDetector_create()
keypoints = star.detect(gray)
mixture = original.copy()
cv.drawKeypoints(original, keypoints, mixture,
flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv.imshow('Mixture', mixture)
cv.waitKey()

SIFT特征检测

突出亮度变化的方向

1
2
3
4
5
6
7
8
9
10
11
12
import cv2 as cv
original = cv.imread('../data/table.jpg')
cv.imshow('Original', original)
gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
sift = cv.xfeatures2d.SIFT_create()
keypoints = sift.detect(gray)
mixture = original.copy()
cv.drawKeypoints(original, keypoints, mixture,
flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv.imshow('Mixture', mixture)
cv.waitKey()

STAR-SIFT特征描述矩阵

通过对STAR特征点做进一步基于SIFT算法的筛选,以样本矩阵的形式表现的图像特征信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np
import cv2 as cv
import matplotlib.pyplot as mp
original = cv.imread('../data/table.jpg')
cv.imshow('Original', original)
gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
star = cv.xfeatures2d.StarDetector_create()
keypoints = star.detect(gray)
sift = cv.xfeatures2d.SIFT_create()
_, desc = sift.compute(gray, keypoints)
print(desc.shape)
mp.matshow(desc, cmap='gist_rainbow',
fignum='DESC')
mp.title('DESC', fontsize=20)
mp.xlabel('Feature', fontsize=14)
mp.ylabel('Sample', fontsize=14)
mp.tick_params(which='both', top=False,
labeltop=False, labelbottom=True,
labelsize=10)
mp.show()

图像识别

类似的特征描述矩阵必然源自类似的图像

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import warnings
import numpy as np
import cv2 as cv
import hmmlearn.hmm as hl
warnings.filterwarnings('ignore',
category=DeprecationWarning)
np.seterr(all='ignore')
def search_objects(directory):
directory = os.path.normpath(directory)
if not os.path.isdir(directory):
raise IOError("The directory '" +
directory + "' doesn't exist!")
objects = {}
for curdir, subdirs, files in os.walk(
directory):
for jpeg in (file for file in files
if file.endswith('.jpg')):
path = os.path.join(curdir, jpeg)
label = path.split(os.path.sep)[-2]
if label not in objects:
objects[label] = []
objects[label].append(path)
return objects
train_objects = search_objects(
'../data/objects/training')
train_x, train_y = [], []
for label, filenames in train_objects.items():
descs = np.array([])
for filename in filenames:
image = cv.imread(filename)
gray = cv.cvtColor(image,
cv.COLOR_BGR2GRAY)
h, w = gray.shape[:2]
f = 200 / min(h, w)
gray = cv.resize(gray, None,
fx=f, fy=f)
star = cv.xfeatures2d.StarDetector_create()
keypoints = star.detect(gray)
sift = cv.xfeatures2d.SIFT_create()
_, desc = sift.compute(gray, keypoints)
if len(descs) == 0:
descs = desc
else:
descs = np.append(descs, desc, axis=0)
train_x.append(descs)
train_y.append(label)
models = {}
for descs, label in zip(train_x, train_y):
model = hl.GaussianHMM(
n_components=4, covariance_type='diag',
n_iter=1000)
models[label] = model.fit(descs)
test_objects = search_objects(
'../data/objects/testing')
test_x, test_y, test_z = [], [], []
for label, filenames in test_objects.items():
test_z.append([])
descs = np.array([])
for filename in filenames:
image = cv.imread(filename)
test_z[-1].append(image)
gray = cv.cvtColor(image,
cv.COLOR_BGR2GRAY)
h, w = gray.shape[:2]
f = 200 / min(h, w)
gray = cv.resize(gray, None,
fx=f, fy=f)
star = cv.xfeatures2d.StarDetector_create()
keypoints = star.detect(gray)
sift = cv.xfeatures2d.SIFT_create()
_, desc = sift.compute(gray, keypoints)
if len(descs) == 0:
descs = desc
else:
descs = np.append(descs, desc, axis=0)
test_x.append(descs)
test_y.append(label)
pred_test_y = []
for descs in test_x:
best_score, best_label = None, None
for label, model in models.items():
score = model.score(descs)
if (best_score is None) or \
(best_score < score):
best_score, best_label = \
score, label
pred_test_y.append(best_label)
i = 0
for label, pred_label, images in zip(
test_y, pred_test_y, test_z):
for image in images:
i += 1
cv.imshow('{} - {} {} {}'.format(
i, label, '==' if
label == pred_label else '!=',
pred_label), image)
cv.waitKey()