一、KNN可视化
1、KNN(K-Nearest Neighbor,K邻近分类法)这种算法把要分类的对象与训练集中已知类标记的所有对象进行对比,并由K邻近对指派到哪个类进行投票。
2、可视化结果:
3、为了可视化所有测试数据点的分类,并展示分类器将两个不同的类分开得怎样,我们可以定义绘图函数以及绘制分类的边界函数
#定义绘图函数
def classify(x,y,model=model):
return array([model.classify([xx,yy]) for (xx,yy) in zip(x,y)])
#绘制分类边界
imtools.plot_2D_boundary([-6,6,-6,6],[class_1,class_2],classify,[1,-1])
show()
另外为了获得x和y二维坐标数组和分类器,并返回一个预测的类标记数组,需要把函数作为参数传递给实际的绘图函数。
将下列函数添加到文件imtools中:
def plot_2D_boundary(plot_range,points,decisionfcn,labels,values=[0]):
""" Plot_range is (xmin,xmax,ymin,ymax), points is a list
of class points, decisionfcn is a funtion to evaluate,
labels is a list of labels that decisionfcn returns for each class,
values is a list of decision contours to show. """
clist = ['b','r','g','k','m','y'] # colors for the classes
# evaluate on a grid and plot contour of decision function
x = arange(plot_range[0],plot_range[1],.1)
y = arange(plot_range[2],plot_range[3],.1)
xx,yy = meshgrid(x,y)
xxx,yyy = xx.flatten(),yy.flatten() # lists of x,y in grid
zz = array(decisionfcn(xxx,yyy))
zz = zz.reshape(xx.shape)
# plot contour(s) at values
contour(xx,yy,zz,values)
# for each class, plot the points with '*' for correct, 'o' for incorrect
for i in range(len(points)):
d = decisionfcn(points[i][:,0],points[i][:,1])
correct_ndx = labels[i]==d
incorrect_ndx = labels[i]!=d
plot(points[i][correct_ndx,0],points[i][correct_ndx,1],'*',color=clist[i])
plot(points[i][incorrect_ndx,0],points[i][incorrect_ndx,1],'o',color=clist[i])
axis('equal')
这个函数需要一个决策函数,并且用meshgrid()函数在一个网格上进行预测。决策函数的等值线可以显示边界的位置,默认边界为零等值线。
二、Dense SIFT原理
1、Dense SIFT算法,是一种对输入图像进行分块处理,再进行SIFT运算的特征提取过程。Dense SIFT根据可调的参数大小,来适当满足不同分类任务下对图像的特征表征能力。Dense SIFT是SIFT的密集采样板,由于SIFT的实时性差,目前特征提取多采用密集采样。
2、Dense SIFT和SIFT的区别:
Dense SIFT :不构建高斯尺度空间,只在a single scale上提取SIFT特征,可以得到每一个位置的SIFT descriptor。
SIFT :尺度空间,只能得到Lowe算法计算得到的点的SIFT descriptor。
三、手势识别
1、出现error:
RuntimeWarning: invalid value encountered in double_scalars
acc = sum(1.0*(res==test_labels)) / len(test_labels)
出现这个问题是因为dsift.py中的python使用的版本不对,导致不能生成.dsift文件。
2、这是我拍的0~5的手势图:
3、跑出来的结果如下: