MATLAB: How can i extract and save LBP features of multiple images, so that i can use them as a DB features

cbirImage Processing Toolboxlbpfeatureslocal binary pattern

Hii, I have a folder of 200 images, I want to extract the LBP features of each image and save them in an Excel sheet so that I can use them as a DB features and retrieve, based on them, the accurate images in a CBIR project. I used this code for the extraction but the output gives me the feature vector of only one image.
image_folder = 'C:\Users\Desktop\images\resized';
ext_img = '*.png';
files = dir(fullfile(image_folder, ext_img));
assert(numel(files) > 0, 'No file was found. Check that the path is correct');
my_img = struct('img', cell(size(files)));
for i = 1:numel(files)
Img = imread(fullfile(image_folder, files(i).name));
gray = rgb2gray(Img);
LBP_image = extractLBPFeatures(gray,'Cellsize',[32 32], 'Normaas i lization','None');
numNeigbors = 8 %%Reshape the LBP features into a number of neighbors -by- number of cells array to access histograms for each individual cell.
numBins = numNeigbors*(numNeigbors-1)+3;
Cell_Hists = reshape(LBP_image,numBins,[]);
Cell_Hists = bsxfun(@rdivide,Cell_Hists,sum(Cell_Hists));%Normalize each LBP cell histogram using L1 norm
LBP_image = reshape(Cell_Hists,1,[]);%Reshape the LBP features vector back to 1-by- N feature vector
end
My second question is how to store them in Excel (in order to have the feature vectors of the 200 images). I used the xlswrite() function but as I said it gives me the vector of only one images (the last one) with a size of 1×3776.

Best Answer

That code is not very robust at all. Mainly you need to preallocate a matrix for LPB_image and load the results for each image into the proper row of LBP_image. And only write the workbook if there were some files found. So you need to do (as a start):
image_folder = 'C:\Users\Desktop\images\resized';
if ~isfolder(image_folder)
errorMessage = sprintf('Error: Folder not found:\n%s', image_folder);
fprintf('%s\n', errorMessage);
errordlg(errorMessage);
return;
end
ext_img = '*.png';
files = dir(fullfile(image_folder, ext_img));
assert(numel(files) > 0, 'No file was found. Check that the path is correct');
my_img = struct('img', cell(size(files)));
numberOfFiles = numel(files);
LBP_image = zeros(numberOfFiles, 3776); % Preallocate a row for every file.
for k = 1 : numberOfFiles
baseFileName = files(k).name;
fullFileName = fullfile(files(k).folder, baseFileName);
fprintf('Processing image #%d of %d : "%s"\n', k, numberOfFiles, baseFileName);
thisImage = imread(fullFileName);
% Convert to gray scale, if needed.
if ndims(thisImage) > 2
grayImage = rgb2gray(thisImage);
else
% Already gray scale so no conversion needed.
grayImage = thisImage;
end
% Compute LBP features.
LBP_image = extractLBPFeatures(grayImage, 'Cellsize',[32 32], 'Normaas i lization','None');
numNeigbors = 8; %%Reshape the LBP features into a number of neighbors -by- number of cells array to access histograms for each individual cell.
numBins = numNeigbors * (numNeigbors-1) + 3;
Cell_Hists = reshape(LBP_image,numBins,[]);
Cell_Hists = bsxfun(@rdivide,Cell_Hists,sum(Cell_Hists));%Normalize each LBP cell histogram using L1 norm
% Assign the results of this image, a 3776 row vector, to the proper row in our output.
LBP_image(k, :) = reshape(Cell_Hists,1,[]);%Reshape the LBP features vector back to 1-by- N feature vector
end
if numberOfFiles >= 0
xlswrite(fileName, LBP_image);
end
Of course there is lots more you could do to improve this code (add comments, alert the user if no files were found, etc.)
Related Question