本文还是对网上openCV教程中例程的注释和修改。openCV里面封装了不少利用了机器学习算法的函数,下面代码完成了对他们的使用。
#K近邻分类算法
import cv2
import numpy as np
import matplotlib.pyplot as plt
trainData = np.random.randint(0,100,(25,2)).astype(np.float32)
responses = np.random.randint(0,2,(25,1)).astype(np.float32)
red = trainData[responses.ravel()==0]
plt.scatter(red[:,0],red[:,1],80,'r','^')
blue = trainData[responses.ravel()==1]
plt.scatter(blue[:,0],blue[:,1],80,'b','s')
new = np.random.randint(0,100,(2,2)).astype(np.float32)
plt.scatter(new[:,0],new[:,1],80,'g','o')
print new
knn = cv2.KNearest()
knn.train(trainData,responses)
ret,result,neighbour,dist = knn.find_nearest(new,11)
print 'result:',result,'\n'
plt.show()
#识别手写数字
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('digits.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]
x = np.array(cells)
train = x[:,:50].reshape(-1,400).astype(np.float32)
test = x[:,50:100].reshape(-1,400).astype(np.float32)#把数字逐个分开成独立图片
k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis]#标记数字图片
test_labels = train_labels.copy()
knn = cv2.KNearest()
knn.train(train,train_labels)#训练
im0 = cv2.imread('25.jpg')
'''ker = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(im0,cv2.MORPH_OPEN,ker)
di = cv2.dilate(im,ker,iterations = 1)'''
im = np.ones(im0.shape)*128-im0
res = cv2.resize(im,(20,20),interpolation=cv2.INTER_CUBIC)
res2 = res.reshape(-1,400).astype(np.float32)
cv2.imshow('1',im)
cv2.waitKey(0)
ret,result,neighbours,dist = knn.find_nearest(res2,k=20)#测试
match = result==test_labels
correct = np.count_nonzero(match)
print (correct*100.0)/result.size#正确率
print result
cv2.destroyAllWindows()
import cv2
import numpy as np
from matplotlib import pyplot as plt
#手写字母识别
#读取数据,转换成asc码
data = np.loadtxt('letter-recognition.data',dtype = 'float32',delimiter = ',',converters={0:lambda ch:ord(ch)-ord('A')})
train,test = np.vsplit(data,2)
responses,train_data = np.hsplit(train,[1])
labels,test_data = np.hsplit(test,[1])#区分训练集和测试集
knn = cv2.KNearest()
knn.train(train_data,responses)#训练
ret,result,neighbour,dist = knn.find_nearest(test_data,k=5)#测试
correct = np.count_nonzero(result==labels)
accuracy = correct*100.0/10000
cv2.imshow('1',train[0])
cv2.waitKey(0)
print accuracy
cv2.destroyAllWindows()
#支持向量机识别手写数字
import cv2
import numpy as np
SZ=20#图片大小
bin_n = 16 # bin数
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
def deskew(img):#抗扭矩计算
m = cv2.moments(img)#图像的矩
if abs(m['mu02']) < 1e-2:
return img.copy()
skew = m['mu11']/m['mu02']
M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
img = cv2.warpAffine(img,M,(SZ, SZ),flags=affine_flags)
return img
def hog(img):#使用HOG作为特征向量
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)#笛卡尔坐标转换为极坐标
bins = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
return hist
svm_params =dict(kernel_type = cv2.SVM_LINEAR,svm_type=cv2.SVM_C_SVC,C=2.67,gamma=5.383)
img = cv2.imread('digits.png',0)
'''if img is None:
raise Exception("we need the digits.png image from samples/data here !")
cells = [np.hsplit(row,100) for row in np.vsplit(img,50)]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
'''
cells = [np.hsplit(row,100) for row in np.vsplit(img,50)]
train_labels = np.repeat(k,250)[:,np.newaxis]#标记数字图片
test_labels = train_labels.copy()
train_cells = [ i[:50] for i in cells ]
test_cells = [ i[50:] for i in cells]
deskewed = [map(deskew,row) for row in train_cells]
hogdata = [map(hog,row) for row in deskewed]
trainData = np.float32(hogdata).reshape(-1,64)
responses = np.repeat(np.arange(10),250)[:,np.newaxis]
#训练
svm = cv2.SVM()
svm.train(trainData,responses,params=svm_params)
svm.save('svm_data.dat')
#预测
deskewed = [map(deskew,row) for row in test_cells]
hogdata = [map(hog,row) for row in deskewed]
testData = np.float32(hogdata).reshape(-1,bin_n*4)
result = svm.predict_all(testData)
#准确率
mask = result==responses
correct = np.count_nonzero(mask)
print correct*100.0/result.size
#Kmean聚类
import numpy as np
import cv2
from matplotlib import pyplot as plt
X = np.random.randint(25,50,(25,2))
Y = np.random.randint(60,85,(25,2))
Z = np.vstack((X,Y))
Z = np.float32(Z)
#print Z
#设置参数,应用kmean算法
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret,label,center=cv2.kmeans(Z,2,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
A = Z[label.ravel()==0]
B = Z[label.ravel()==1]
#可视化
plt.scatter(A[:,0],A[:,1],c='b')
plt.scatter(B[:,0],B[:,1],c = 'r')
plt.scatter(center[:,0],center[:,1],s = 80,c = 'y', marker = 's')
plt.xlabel('Height'),plt.ylabel('Weight')
plt.show()
#利用Kmean来对图像的颜色进行聚类,减少颜色,压缩图片,减少内存占用
import numpy as np
import cv2
img = cv2.imread('21.jpg')
Z = img.reshape((-1,3))
Z = np.float32(Z)
#对像素点进行聚类
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
#用聚类中心的值代替同组所有像素值
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.destroyAllWindows()