Skip to content

Commit 0394d8c

Browse files
author
niphy
committed
update
0 parents  commit 0394d8c

File tree

110 files changed

+5043
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+5043
-0
lines changed

ClassfyPictureTest.m

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function predicted_label = ClassfyPictureTest(testpath,ext,svmtestpath,n,kmeansfunc,model)
2+
%生成svmttext文件数据
3+
4+
5+
[telabel,tedata]=creatsvmfrompath(testpath,ext,svmtestpath,n,kmeansfunc);
6+
%进行预测
7+
%[predicted_label,accuracy,prob_estimates]=svmpredict(telabel,tedata,model);
8+
predicted_label = svmclassify(model,tedata);
9+
10+
% matwrite('labels.txt',predicted_label);
11+
% matwrite('accuracy.txt',accuracy);
12+
% matwrite('prob_estimates.txt',prob_estimates);
13+
%下面的要自己统计(自己做了)
14+
15+
end

ClassfyPictureTrain.m

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function model = ClassfyPictureTrain(trainpath,ext,svmtrainpath,n,kmeansfunc)
2+
%%通过trainpath内图像与kmeansfunc方式进行模型构建,获得model并保存
3+
%picturepath保存各个类别的图形的文件路径
4+
%kmeansfunc进行kmeans分类方式
5+
%%实现过程
6+
%生成svmtrain文件数据
7+
[trlabel,trdata]=creatsvmfrompath(trainpath,ext,svmtrainpath,n,kmeansfunc);
8+
%读取svmtrain数据
9+
%!python grid.py svmpath
10+
%得到c g
11+
%0.03125 0.0078125 25.0
12+
%预测模型
13+
model=svmtrain(trdata,trlabel);
14+
end
15+
16+
17+

accuracy.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1.000000 50.000000
2+
1.000000 0.500000
3+
1.000000 NaN

creatsvmdata.m

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function creatsvmdata(idens,des,path)
2+
%%
3+
%idens为图片类别号
4+
%des为图片描述
5+
%path为保存路径
6+
%%
7+
%获得图片数目
8+
picsz=size(idens,1);
9+
picdsz=size(des,2);
10+
fid=fopen(path,'w');
11+
for i=1:picsz
12+
fprintf(fid,'%d\t',idens(i));
13+
for j=1:picdsz
14+
fprintf(fid,'%d:%f\t',j,double(des(i,j)));
15+
end
16+
fprintf(fid,'\n');
17+
end
18+
fclose(fid);
19+
end

creatsvmfrompath.m

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function [palliden,palldes]=creatsvmfrompath(srcpaths,ext,savepath,n,kmeansfunc)
2+
%%由路径创建图片的svmdata的数据文件
3+
%srcpath保存各个类别的图形的文件路径
4+
%savepath保存svmdata的文件路径
5+
%kmeansfunc进行kmeans分类方式
6+
%%
7+
%获得分类数
8+
pclassnum=size(srcpaths,2);
9+
palliden=[];
10+
palldes=[];
11+
%进行各分类赋值
12+
for i=1:pclassnum
13+
%获得一路径下所有文件特征
14+
[idens,des]=getpathpicdes(srcpaths{1,i},ext,i,n,kmeansfunc);
15+
%附加新值
16+
palliden=[palliden;idens];
17+
palldes=[palldes;des];
18+
end
19+
%保存训练数据
20+
creatsvmdata(palliden,palldes,savepath);
21+
end

fromkmeansresult.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function despic=fromkmeansresult(siftimg,n,IDX, C, sumd, D)
2+
%统计各聚类中心点的数目
3+
allcount(1,1:n)=0;
4+
potsz=size(IDX,1);
5+
for i=1:potsz
6+
allcount(1,IDX(i))=allcount(1,IDX(i))+1;
7+
end
8+
maxcount=max(allcount);
9+
for i=1:n
10+
if maxcount==allcount(i);
11+
maxindex=i;
12+
end
13+
end
14+
%统计maxnidexclus
15+
allclus=sum(C(maxindex,:));
16+
%获得sumd
17+
sumd=sumd';
18+
%获得各点距中心和
19+
allcenter=sum(D,1);
20+
%图片特征值返回
21+
despic=[allcount,allclus,sumd,allcenter];
22+
end

fromsiftresult.m

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function siftdes=fromsiftresult(siftpot,siftdes)
2+
potsz=size(siftpot,1);
3+
ad=[];
4+
for i=1:potsz
5+
ang(1,1:potsz)=0;
6+
dis(1,1:potsz)=0;
7+
for j=1:potsz
8+
x=siftpot(j,1)-siftpot(i,1);
9+
y=siftpot(j,2)-siftpot(i,2);
10+
dis(1,j)=sqrt(x*x+y*y);
11+
ang(1,j)=atan2(y,x);
12+
end
13+
mang=mean(ang);
14+
mdis=mean(dis);
15+
diss(1,1:4)=0;
16+
for j=1:potsz
17+
if ang(1,j)>=mang
18+
diss(1,1)=diss(1,1)+1;
19+
end
20+
if ang(1,j)<mang
21+
diss(1,2)=diss(1,2)+1;
22+
end
23+
if dis(1,j)>=mdis
24+
diss(1,3)=diss(1,3)+1;
25+
end
26+
if dis(1,j)<mdis
27+
diss(1,4)=diss(1,4)+1;
28+
end
29+
end
30+
ad=[ad;diss];
31+
end
32+
siftdes=[siftpot(:,3:4),ad];
33+
end

getallfiles.m

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function [filenames]=getallfiles(folderdir,ext)
2+
%%获得folderdir下的以ext为后缀的所有文件
3+
fileFolder=fullfile(folderdir);
4+
dirOutput=dir(fullfile(fileFolder,ext));
5+
filenames={dirOutput.name}';
6+
num=size(filenames,1);
7+
for i=1:num
8+
filenames{i}=strcat(folderdir,filenames{i});
9+
end
10+
end

getpathpicdes.m

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function [cliden,cldes]=getpathpicdes(picturespath,ext,type,n,kmeanfunc)
2+
%%获得picturespath这一路径下所有图片的特征
3+
%picturespath路径
4+
%type分类号
5+
%kmeanfunc分类方式
6+
%%
7+
%获得所有图片路径
8+
filenames=getallfiles(picturespath,ext);
9+
%获得所有图片数目
10+
clnum=size(filenames,1);
11+
%获得特征Idens
12+
cliden=type*ones(clnum,1);%type是标签
13+
%构造图像特征
14+
cldes=[];
15+
for i=1:clnum
16+
cldes=[cldes;picidening(filenames{i},n,kmeanfunc)];
17+
end
18+
end

labels.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
1.000000 2.000000
2+
1.000000 2.000000
3+
1.000000 2.000000
4+
1.000000 2.000000
5+
1.000000 2.000000
6+
1.000000 2.000000
7+
1.000000 2.000000
8+
1.000000 2.000000
9+
1.000000 2.000000
10+
1.000000 2.000000
11+
1.000000 2.000000
12+
1.000000 2.000000
13+
1.000000 2.000000
14+
1.000000 2.000000
15+
1.000000 2.000000
16+
1.000000 2.000000
17+
1.000000 2.000000
18+
1.000000 2.000000
19+
1.000000 2.000000
20+
1.000000 2.000000
21+
1.000000 2.000000
22+
1.000000 2.000000

main.m

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
trains = {'accordion','airplanes'};
2+
predicted_label = usingpro(trains,trains,'*.jpg',2,[]);

mat.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1.000000 0.433938 2.000000 0.785353 3.000000 0.106040 4.000000 0.385658 5.000000 0.961285
2+
1.000000 0.045593 2.000000 0.433492 3.000000 0.006074 4.000000 0.856946 5.000000 0.822906
3+
1.000000 0.655473 2.000000 0.347821 3.000000 0.275042 4.000000 0.674825 5.000000 0.010499
4+
1.000000 0.781042 2.000000 0.055389 3.000000 0.942771 4.000000 0.608097 5.000000 0.141617
5+
1.000000 0.862900 2.000000 0.819441 3.000000 0.674259 4.000000 0.439426 5.000000 0.482810

matwrite.m

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function matwrite(path,mat)
2+
%%
3+
%mat为Matrix
4+
%path为保存路径
5+
%%
6+
%获得图片数目
7+
picsz=size(mat,1);
8+
picdsz=size(mat,2);
9+
fid=fopen(path,'w');
10+
for i=1:picsz
11+
for j=1:picdsz
12+
fprintf(fid,'%f\t',j,double(mat(i,j)));
13+
end
14+
fprintf(fid,'\n');
15+
end
16+
fclose(fid);
17+
end

picidening.m

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function despic=picidening(picpath,n,kmeansfunc)
2+
%%获得pic的sift聚类特征
3+
%picpath为图片路径
4+
%n聚类数目
5+
%kmeansfunc聚类参数
6+
%%过程
7+
%获得sift特征点与特征点向量
8+
[siftimg,siftpot,siftdes]=sift(picpath);
9+
%由m,theta,与128维特征计算sift特征点相关性
10+
alldes=fromsiftresult(siftpot,siftdes);
11+
%进行基于特征点及特征的用kmeanfunc参数进行kmeans分类
12+
ifsz=size(kmeansfunc,2);
13+
if floor(ifsz/2)~=ifsz/2
14+
error(message('kmeansfunc必须是一一对应的参数'));
15+
end
16+
if ifsz==0
17+
[IDX,C,sumd,D]=kmeans(alldes,n);
18+
end
19+
if ifsz==2
20+
[IDX,C,sumd,D]=kmeans(alldes,n,kmeansfunc{1,1},kmeansfunc{1,2});
21+
end
22+
if ifsz==4
23+
[IDX,C,sumd,D]=kmeans(alldes,n,kmeansfunc{1,1},kmeansfunc{1,2},kmeansfunc{1,3},kmeansfunc{1,4});
24+
end
25+
if ifsz==6
26+
[IDX,C,sumd,D]=kmeans(alldes,n,kmeansfunc{1,1},kmeansfunc{1,2},kmeansfunc{1,3},kmeansfunc{1,4},kmeansfunc{1,5},kmeansfunc{1,6});
27+
end
28+
if ifsz==8
29+
[IDX,C,sumd,D]=kmeans(alldes,n,kmeansfunc{1,1},kmeansfunc{1,2},kmeansfunc{1,3},kmeansfunc{1,4},kmeansfunc{1,5},kmeansfunc{1,6},kmeansfunc{1,7},kmeansfunc{1,8});
30+
end
31+
%据分类结果计算特征量
32+
despic=fromkmeansresult(siftimg,n,IDX, C, sumd, D);
33+
end

prob_estimates.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
1.000000 0.000000
2+
1.000000 0.000000
3+
1.000000 0.000000
4+
1.000000 0.000000
5+
1.000000 0.000000
6+
1.000000 0.000000
7+
1.000000 0.000000
8+
1.000000 0.000000
9+
1.000000 0.000000
10+
1.000000 0.000000
11+
1.000000 0.000000
12+
1.000000 0.000000
13+
1.000000 0.000000
14+
1.000000 0.000000
15+
1.000000 0.000000
16+
1.000000 0.000000
17+
1.000000 0.000000
18+
1.000000 0.000000
19+
1.000000 0.000000
20+
1.000000 0.000000
21+
1.000000 0.000000
22+
1.000000 0.000000

sift.m

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
% [image, descriptors, locs] = sift(imageFile)
2+
%
3+
% This function reads an image and returns its SIFT keypoints.
4+
% Input parameters:
5+
% imageFile: the file name for the image.
6+
%
7+
% Returned:
8+
% image: the image array in double format
9+
% descriptors: a K-by-128 matrix, where each row gives an invariant
10+
% descriptor for one of the K keypoints. The descriptor is a vector
11+
% of 128 values normalized to unit length.
12+
% locs: K-by-4 matrix, in which each row has the 4 values for a
13+
% keypoint location (row, column, scale, orientation). The
14+
% orientation is in the range [-PI, PI] radians.
15+
%
16+
% Credits: Thanks for initial version of this program to D. Alvaro and
17+
% J.J. Guerrero, Universidad de Zaragoza (modified by D. Lowe)
18+
19+
function [image, descriptors, locs] = sift(imageFile)
20+
21+
% Load image
22+
23+
image = imread(imageFile);
24+
25+
% If you have the Image Processing Toolbox, you can uncomment the following
26+
% lines to allow input of color images, which will be converted to grayscale.
27+
if ndims(image) == 3
28+
image = rgb2gray(image);
29+
end
30+
31+
[rows, cols] = size(image);
32+
33+
% Convert into PGM imagefile, readable by "keypoints" executable
34+
f = fopen('tmp.pgm', 'w');
35+
if f == -1
36+
error('Could not create file tmp.pgm.');
37+
end
38+
fprintf(f, 'P5\n%d\n%d\n255\n', cols, rows);
39+
fwrite(f, image, 'uint8');
40+
fclose(f);
41+
42+
% Call keypoints executable
43+
if isunix
44+
command = '!./sift ';
45+
else
46+
command = '!siftWin32.exe ';
47+
end
48+
command = [command ' <tmp.pgm >tmp.key'];
49+
eval(command);
50+
51+
% Open tmp.key and check its header
52+
g = fopen('tmp.key', 'r');
53+
if g == -1
54+
error('Could not open file tmp.key.');
55+
end
56+
[header, count] = fscanf(g, '%d %d', [1 2]);
57+
if count ~= 2
58+
error('Invalid keypoint file beginning.');
59+
end
60+
num = header(1);
61+
len = header(2);
62+
if len ~= 128
63+
error('Keypoint descriptor length invalid (should be 128).');
64+
end
65+
66+
% Creates the two output matrices (use known size for efficiency)
67+
locs = double(zeros(num, 4));
68+
descriptors = double(zeros(num, 128));
69+
70+
% Parse tmp.key
71+
for i = 1:num
72+
[vector, count] = fscanf(g, '%f %f %f %f', [1 4]); %row col scale ori
73+
if count ~= 4
74+
error('Invalid keypoint file format');
75+
end
76+
locs(i, :) = vector(1, :);
77+
78+
[descrip, count] = fscanf(g, '%d', [1 len]);
79+
if (count ~= 128)
80+
error('Invalid keypoint file value.');
81+
end
82+
% Normalize each input vector to unit length
83+
descrip = descrip / sqrt(sum(descrip.^2));
84+
descriptors(i, :) = descrip(1, :);
85+
end
86+
fclose(g);
87+
88+

siftWin32.exe

92 KB
Binary file not shown.

0 commit comments

Comments
 (0)