Python车牌特征提取(朴素线性特征)
·
Python模式识别特征提取
文章目录
1.图像灰度化
import cv2
img = cv2.imread("a.jpeg") #读取图片
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #灰度化
height, width, channel = img.shape
print(img.shape)
img=img_gray
2.图像切割
crop1 = img[0:height/3, 0:width/3] # 裁剪坐标为[y0:y1, x0:x1]
crop2=img[0:height/3, width/3:width/3*2]
crop3=img[0:height/3, width/3*2:width/3*3]
crop4 = img[height/3:height/3*2, 0:width/3] # 裁剪坐标为[y0:y1, x0:x1]
crop5=img[height/3:height/3*2, width/3:width/3*2]
crop6=img[height/3:height/3*2, width/3*2:width/3*3]
crop7 = img[height/3*2:height, 0:width/3] # 裁剪坐标为[y0:y1, x0:x1]
crop8=img[height/3*2:height, width/3:width/3*2]
crop9=img[height/3*2:height, width/3*2:width/3*3]
#接下来保存图片
# #一开始我是这样写的:
# cv2.imwrite("crop1.jpg", crop1) #可以单独分别保存
# cv2.imwrite("crop2.jpg", crop2)
# cv2.imwrite("crop3.jpg", crop3)
# cv2.imwrite("crop4.jpg", crop4)
# cv2.imwrite("crop5.jpg", crop5)
# cv2.imwrite("crop6.jpg", crop6)
# cv2.imwrite("crop7.jpg", crop7)
# cv2.imwrite("crop8.jpg", crop8)
# cv2.imwrite("crop9.jpg", crop9)
#可以用下面的代码实现相同的功能:
crops_image=[crop1,crop2,crop3,crop4,crop5,crop6,crop7,crop8,crop9]
crops_name=["crop1","crop2","crop3","crop4","crop5","crop6","crop7","crop8","crop9"]
crop_num=0
for crop_name in crops_name:
cv2.imwrite(crop_name+'.jpg', crops_image[crop_num])
crop_num=crop_num+1
3.特征提取
初步我们希望提取到每一个小方格中的黑白像素占比
3.1 按照阈值二值化图片
那么我们设计一个函数用来计算像素占比
#获取特征值:黑像素数量 : 白像素数量
def get_black_white(crop_image):
height, width= crop_image.shape
black=0.0
white=0.0
#print(crop_image)
for i in range(height):
for j in range(width):
if crop_image[i][j]<128:
white+=1
else:
black+=1
black_white=black/white
# return [black,white] #返回黑白像素值
return [black,white,black_white]
3.2 构造特征值向量
参考:
import numpy as np
Vector0=np.zeros(9,np.float)
i=0
for crop_image in crops_image:
Vector0[i]=get_black_white(crop_image)[2]
i=i+1
#其他向量同理Vector1,Vector2...Vector8,Vector9
print(Vector0)
4.计算欧氏距离
参考:python计算向量欧式距离
新读取一张图片,经历相同的过程获得到新图片的Vector_new
然后分别计算Vector_new与各已有的Vector0,Vector1,Vector2…Vector8,Vector9的欧氏距离,距离最近则表示该张图片与数字示例图片最相似。
Vector1=np.ones(9,np.float)
Vector2=np.zeros(9,np.float)
#计算向量之间的距离
dist = np.linalg.norm(Vector2 - Vector1)
print(dist)
Q&A
Q:向量一定要分割成33的嘛,55的可以嘛?
A:可以,什么规格的都行,只要能够更好的获取特征值就行。
Q:提取到的特征值应该怎么保存呢?保存之后如何更新呢?
A:存到一个文件里面,暂时采用按照权重更新的办法。因为模型本身很简单,所以更新权重也不必很复杂。
Q:你觉得这样提取特征的办法有什么缺陷呢?你能想到更好的特征值用来 感知 数字0,1,2,3,4…8,9字符的形状嘛?
A:有缺陷,黑像素值/白像素值的办法过于粗糙,对于形状的感知能力也很差,
-
比如一张图片里面各有50个黑白像素,那么他们的比值就是1,然而形状却有千万种。
-
向量各个维度之间的相关性很差,因为我们把一张图片裁剪成了单独的9张图片,并且分别计算他们的黑色像素/白色像素的值,所以相关性很差,然而事实上一张图片各个部分之间是有相关性和连续性的。
解决的办法(大致思路): -
可以使用不同尺度的降维来从近到远,从局部到整体地观察图片;同时使用频域分析来增强对于图片形状的感知。
-
可以采用33,55的高斯核卷积来强化各个部分之间的相关性和独立性。
更多推荐



所有评论(0)