3,424
社区成员




function getSiftValue
father = 'D:\users\luo_he\DATA';
mkdir(father, 'desData');
mkdir([father, '\', 'desData'], 'tempTrain');
mkdir([father, '\', 'desData'], 'tempTest');
Train = [father, '\', 'tempTrain'];
Test = [father, '\', 'tempTest'];
siftTrn = [father, '\', 'desData', '\', 'tempTrain'];
siftTst = [father, '\', 'desData', '\','tempTest'];
subTrn = dir(Train);
lenTrn = length(subTrn);
if isunix
command = '!./sift ';
else
command = '!siftWin32 ';
end
command = [command ' <tmp.pgm >tmp.key'];
%classDescriptors = [];
for i = 3 : lenTrn
className = subTrn(i).name;
imgNames = dir([Train, '\', className]);
imgNum = length(imgNames);
%open a file to save class descriptors
fileName = [siftTrn, '\', [className, '.des']];
classDes = fopen(fileName, 'a');
fprintf(classDes, '[type:sift]\n');
%fclose(classDes);
for j = 3 : imgNum
imgPth = [Train, '\', className, '\', imgNames(j).name];
image = imread(imgPth);
image = prcssImage(image);
[rows, cols] = size(image);
% in case of file open fail, try 10 times
for x = 1 : 10
f = fopen('tmp.pgm', 'w');
if f ~= -1
break;
end
end
if f ~= -1
fprintf(f, 'P5\n%d\n%d\n255\n', cols, rows);
fwrite(f, image', 'uint8');
fclose(f);
% Call keypoints executable
eval(command);
tmpkey = fopen('tmp.key', 'r');
keyData = fread(tmpkey);
fclose(tmpkey);
fwrite(classDes, keyData);
else
errfile = fopen([father, '\', 'error.txt'], 'a');
fprintf(errfile, 'open tmp.pgm error, when computing %s', imgPth);
fclose(errfile);
end
end
fclose(classDes);
end
subTst = dir(Test);
lenTst = length(subTst);
for i = 3 : lenTst
className = subTst(i).name;
mkdir(siftTst, className);
imgNames = dir([Test, '\', className]);
imgNum = length(imgNames);
for j = 3 : imgNum
imgPth = [Test, '\', className, '\', imgNames(j).name];
image = imread(imgPth);
image = prcssImage(image);
[rows, cols] = size(image);
fileName = [siftTst, '\', className, '\',imgNames(j).name, '.des'];
imgDes = fopen(fileName, 'a');
fprintf(imgDes, '[type:sift]\n');
%fclose(imgDes);
for x = 1 : 10
f = fopen('tmp.pgm', 'w');
if f ~= -1
break;
end
end
if f ~= -1
fprintf(f, 'P5\n%d\n%d\n255\n', cols, rows);
fwrite(f, image', 'uint8');
fclose(f);
% Call keypoints executable
eval(command);
tmpkey = fopen('tmp.key', 'r');
keyData = fread(tmpkey);
fclose(tmpkey);
fwrite(imgDes, keyData);
else
errfile = fopen([father, '\', 'error.txt'], 'a');
fprintf(errfile, 'open tmp.pgm error, when computing %s', imgPth);
fclose(errfile);
end
end
end