图像识别
基础
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
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] # 0 - 蓝色通道
cv.imshow('Blue', blue)
green = np.zeros_like(original)
green[..., 1] = original[..., 1] # 1 - 绿色通道
cv.imshow('Green', green)
red = np.zeros_like(original)
red[..., 2] = original[..., 2] # 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)
# 缩放
scaled1 = cv.resize(
original, (int(w / 4), int(h / 4)),
interpolation=cv.INTER_LINEAR)
cv.imshow('Scaled1', scaled1)
# 缩放
scaled2 = cv.resize(
scaled1, None, fx=4, fy=4,
interpolation=cv.INTER_LINEAR)
cv.imshow('Scaled2', scaled2)
cv.waitKey()
cv.imwrite('../../data/blue.jpg', blue)
cv.imwrite('../../data/green.jpg', green)
cv.imwrite('../../data/red.jpg', red)
cv.imwrite('../../data/cropped.jpg', cropped)
cv.imwrite('../../data/scaled1.jpg', scaled1)
cv.imwrite('../../data/scaled2.jpg', scaled2)
边缘检测
通过梯度剧烈变化,识别轮廓。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import cv2 as cv
original = cv.imread(
'../../data/chair.jpg', cv.IMREAD_GRAYSCALE)
cv.imshow('Original', original)
# 三种获取边缘的方式
hsobel = cv.Sobel(original, cv.CV_64F, 1, 0,
ksize=5)
cv.imshow('H-Sobel', hsobel)
vsobel = cv.Sobel(original, cv.CV_64F, 0, 1,
ksize=5)
cv.imshow('V-Sobel', vsobel)
sobel = cv.Sobel(original, cv.CV_64F, 1, 1,
ksize=5)
cv.imshow('Sobel', sobel)
laplacian = cv.Laplacian(original, cv.CV_64F)
cv.imshow('Laplacian', laplacian)
canny = cv.Canny(original, 50, 240)
cv.imshow('Canny', canny)
cv.waitKey()
亮度提升
通过直方图均衡化,提高较暗的部分。让整体亮度
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
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)
# 直方图均衡化
equalized_gray = cv.equalizeHist(gray)
cv.imshow('Equalized Gray', equalized_gray)
yuv = cv.cvtColor(original, cv.COLOR_BGR2YUV)
yuv[..., 0] = cv.equalizeHist(yuv[..., 0])
equalized_color = cv.cvtColor(yuv, cv.COLOR_YUV2BGR)
cv.imshow('Equalized Color', equalized_color)
cv.waitKey()
特征点检测
识别图片中的特殊结构点,作为特征点记录。
根据颜色梯度得到拐角、边缘等颜色梯度大的地方识别。这种地方变化较大且特征明显。
通过这个可以得到特征描述矩阵。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import cv2 as cv
original = cv.imread(r'C:\Users\Cs\Desktop\table.jpg')
cv.imshow('Original', original)
gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
# 创建STAR特征点监测器
# pip install opencv-contrib-python
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()
图像的特征描述矩阵
来源于反映图像中物体的结构特征关键点的空间直方图。
#特征描述
import cv2 as cv
import matplotlib.pyplot as mp
original = cv.imread(r'C:\Users\Cs\Desktop\table.jpg')
cv.imshow('or',original)
gray=cv.cvtColor(original,cv.COLOR_BGR2GRAY)
star=cv.xfeatures2d.StarDetector_create()
keypoints=star.detect(gray)
sift=cv.xfeatures2d.SIFT_create()
_,desc=sift.compute(gray,keypoints)
mp.matshow(desc)
mp.show()
基于特征矩阵的物体识别
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
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(r'C:\Users\Cs\Desktop\objects\training')
train_x, train_y = [], []
for label, filenames in train_objects.items():
descs = np.array([])
for filename in filenames:
image = cv.imread(filename, cv.IMREAD_ANYCOLOR)
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)
# 获取物体二维特征
# xfeatures2d模块使用有疑问
star = cv.xfeatures2d.StarDetector_create()
#得到物体特征矩阵
# 返回图像中的关键点
keypoints = star.detect(gray)
sift = cv.xfeatures2d.SIFT_create()
# 得到特征点与特征点描述(SIFT SURF ORB AKAZE)
_, 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(
r'C:\Users\Cs\Desktop\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()
人脸识别
视频捕捉
视频捕捉设备->图像帧
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import cv2 as cv
vc = cv.VideoCapture(0)
while True:
frame = vc.read()[1]
cv.imshow('VideoCapture', frame)
if cv.waitKey(33) == 27:
break
# 记得关闭摄像头
vc.release()
cv.destroyAllWindows()
人脸定位
哈尔级联人脸定位。
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import cv2 as cv
# 哈尔级联人脸定位器
fd = cv.CascadeClassifier(r'C:\Users\Cs\Desktop\haar\face.xml')
ed = cv.CascadeClassifier(r'C:\Users\Cs\Desktop\haar\eye.xml')
nd = cv.CascadeClassifier(r'C:\Users\Cs\Desktop\haar\nose.xml')
vc = cv.VideoCapture(0)
while True:
frame = vc.read()[1]
# 参数:图片矩阵,最小尺寸,最多数量??
faces = fd.detectMultiScale(frame, 1.3, 5)
for l, t, w, h in faces:
a, b = int(w / 2), int(h / 2)
# cv.ellipse(图像, 椭圆心, 半径, 旋转角,
# 起始角, 终止角, 颜色, 线宽)
cv.ellipse(frame, (l + a, t + b), (a, b),
0, 0, 360, (255, 0, 255), 2)
face = frame[t:t + h, l:l + w]
eyes = ed.detectMultiScale(face, 1.3, 5)
for l, t, w, h in eyes:
a, b = int(w / 2), int(h / 2)
cv.ellipse(face, (l + a, t + b),
(a, b), 0, 0, 360,
(0, 255, 0), 2)
noses = nd.detectMultiScale(face, 1.3, 5)
for l, t, w, h in noses:
a, b = int(w / 2), int(h / 2)
cv.ellipse(face, (l + a, t + b),
(a, b), 0, 0, 360,
(0, 255, 255), 2)
cv.imshow('VideoCapture', frame)
if cv.waitKey(33) == 27:
break
vc.release()
cv.destroyAllWindows()
3.简单人脸识别:OpenCV的LBPH(局部二值模式直方图)
代码:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import numpy as np
import cv2 as cv
import sklearn.preprocessing as sp
# 级联搜索,定位人脸位置使用
fd = cv.CascadeClassifier('../../data/haar/face.xml')
#获取训练数据
def search_faces(directory):
directory = os.path.normpath(directory)
if not os.path.isdir(directory):
raise IOError("The directory '" +
directory +
"' doesn't exist!")
faces = {}
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 faces:
faces[label] = []
faces[label].append(path)
return faces
train_faces = search_faces(
'../../data/faces/training')
codec = sp.LabelEncoder()
codec.fit(list(train_faces.keys()))
train_x, train_y = [], []
for label, filenames in train_faces.items():
#图片切割
for filename in filenames:
image = cv.imread(filename)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
faces = fd.detectMultiScale(gray, 1.1, 2,
minSize=(100, 100))
for l, t, w, h in faces:
train_x.append(
gray[t:t + h, l:l + w])
train_y.append(
codec.transform([label])[0])
train_y = np.array(train_y)
# 局部二值模式直方图人脸识别分类器
model = cv.face.LBPHFaceRecognizer_create()
model.train(train_x, train_y)
test_faces = search_faces(
'../../data/faces/testing')
test_x, test_y, test_z = [], [], []
for label, filenames in test_faces.items():
for filename in filenames:
image = cv.imread(filename)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
faces = fd.detectMultiScale(gray, 1.1, 2,
minSize=(100, 100))
for l, t, w, h in faces:
test_x.append(
gray[t:t + h, l:l + w])
test_y.append(
codec.transform([label])[0])
a, b = int(w / 2), int(h / 2)
cv.ellipse(image, (l + a, t + b),
(a, b), 0, 0, 360,
(255, 0, 255), 2)
test_z.append(image)
test_y = np.array(test_y)
pred_test_y = []
for face in test_x:
pred_code = model.predict(face)[0]
pred_test_y.append(pred_code)
escape = False
while not escape:
for code, pred_code, image in zip(
test_y, pred_test_y, test_z):
label, pred_label = \
codec.inverse_transform([code, pred_code])
text = '{} {} {}'.format(
label,
'==' if code == pred_code else '!=',
pred_label)
cv.putText(image, text, (10, 60),
cv.FONT_HERSHEY_SIMPLEX, 2,
(255, 255, 255), 6)
cv.imshow('Recognizing...', image)
if cv.waitKey(1000) == 27:
escape = True
break