图像预处理
滤去干扰,噪声等。如图像信息微弱,还要进行增强处理,几何调整,着色校正等。
图象分割
从图像中定位,分离出不同的待识别物体。这一过程输入的是整幅图像,输出是像元图像。
图像特征抽取
提到需要特征并对某些参数进行计算,测量,根据结果进行分类。
图像分类
根据撮特征值,利用模式识别方法进行分类,确定相关信息。
使用多输出估计来完成图像。目标是根据人脸的上半部分来预测其下半部分。图像的第一列显示真实面。下一列显示用极随机树、k最近邻、线性回归和岭回归完成这些脸的下半部分预测。
结果如下:
实现:
#coding=utf-8 或者:#coding=gbk
‘’’
在代码上面注释上它的用途,如果开头不声明保存编码的格式是什么,那么它会默认使用ASKII码保存文件,这时如果你的代码中有中文就会出错了,即使你的中文是包含在注释里面的。所以加上中文注释很重要。
‘’’
‘’’
Python有个特性叫做文档字符串,即DocString,这个特性可以让你的程序文档更加清晰易懂,在python的系统文件里,也都在使用这个特性。因此推荐每个Python爱好者都使用它。
DocString是有自己的格式的。一般是放在自己函数定义的第一行,用‘’符号指示,在这‘’里面添加函数功能说明就可以了。这个说明可以使用.doc(注意前后都是双_)属性,将DocString特性print打印出来。
DocSting的典型用法就是help()调用,它抓取DocString属性,清晰的给你展示出来。
‘’’
print(doc)
#改模块名为 np,是一个由多维数组对象和用于处理数组的例程集合组成的库。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces
from sklearn.utils.validation import check_random_state
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV
加载面部数据
data = fetch_olivetti_faces()
targets = data.target
data = data.images.reshape((len(data.images), -1))
train = data[targets < 30]
test = data[targets >= 30] # Test on independent people
对一部分人进行测试
n_faces = 5
rng = check_random_state(4)
face_ids = rng.randint(test.shape[0], size=(n_faces, ))
test = test[face_ids, :]
n_pixels = data.shape[1]
上半部脸
X_train = train[:, :(n_pixels + 1) // 2]
下半部脸
y_train = train[:, n_pixels // 2:]
X_test = test[:, :(n_pixels + 1) // 2]
y_test = test[:, n_pixels // 2:]
拟合估计
ESTIMATORS = {
“Extra trees”: ExtraTreesRegressor(n_estimators=10, max_features=32,
random_state=0),
“K-nn”: KNeighborsRegressor(),
“Linear regression”: LinearRegression(),
“Ridge”: RidgeCV(),
}
y_test_predict = dict()
for name, estimator in ESTIMATORS.items():
estimator.fit(X_train, y_train)
y_test_predict[name] = estimator.predict(X_test)
画完整的脸
image_shape = (64, 64)
n_cols = 1 + len(ESTIMATORS)
plt.figure(figsize=(2. * n_cols, 2.26 * n_faces))
plt.suptitle(“Face completion with multi-output estimators”, size=16)
for i in range(n_faces):
true_face = np.hstack((X_test[i], y_test[i]))
if i:
sub = plt.subplot(n_faces, n_cols, i * n_cols + 1)
else:
sub = plt.subplot(n_faces, n_cols, i * n_cols + 1,
title="true faces")
sub.axis("off")
sub.imshow(true_face.reshape(image_shape),
cmap=plt.cm.gray,
interpolation="nearest")
for j, est in enumerate(sorted(ESTIMATORS)):
completed_face = np.hstack((X_test[i], y_test_predict[est][i]))
if i:
sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j)
else:
sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j,
title=est)
sub.axis("off")
sub.imshow(completed_face.reshape(image_shape),
cmap=plt.cm.gray,
interpolation="nearest")
plt.show()