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

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 1x3776.

4 commentaires

What variable did you pass to xlswrite()? What were you expecting? Did that part work as desired but you are only looking for how to batch process a sequence of images?
Thank you for your response Image Analyst. i used these two lines to save the features in excel:
filename = 'C:\Users\Desktop\Tests\features.xlsx';
F= xlswrite(filename,LBP_image);
i was expecting to extract LBPfeatures for each of the 200 images, which means having 200 features vectors (coresponding to each of the images) but i did get this, as a result i got only on feature vector.
for the xlswrite, the goal was to store those 200 features vectors in an excel sheet, but since i got only one feature vector it didn't happen.
Hi, I have 100 images in my file to be extracted. I have used the coding shown but still not able to display all 100 LBP features. Below is the coding I use.
image_folder = 'C:\Users\HP\Desktop\ABC\A';
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 = 100;%numel(files);
results = zeros(numberOfFiles);
for k = 1 : numberOfFiles
baseFileName = files(k).name;
fullFileName = fullfile(files(k).folder, baseFileName);
thisImage = imread(fullFileName);
grayImage = rgb2gray(thisImage);
LBP_image = extractLBPFeatures(grayImage,'CellSize',[32 32],'Normalization','None');
numNeigbors = 8;
Cell_Hists = reshape(LBP_image,numBins,[]);
Cell_Hists = bsxfun(@rdivide,Cell_Hists,sum(Cell_Hists));
results(k, :) = reshape(Cell_Hists,1,[]);
end
if numberOfFiles >= 0
writematrix(LBP_image,results);
end
Below error showed when I run the coding.
Unable to perform assignment because the size of the left side is 1-by-100 and the size of the right side is 1-by-59.
Error in array (line 25)
results(k, :) = reshape(Cell_Hists,1,[]);
@Lee Hau declare results with the right number of bins before the loop
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;
results = zeros(numberOfFiles, numBins);

Connectez-vous pour commenter.

 Réponse acceptée

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.)

5 commentaires

  • I tried this code and it did creat an array of 200 rows (for each of the image), but it computed the LBPfeatures of only the first and the last image, the images in between were all zeros
  • i checked randomly the images and computer the LBPfeature of some to see if there's nthg wrong with them and i do get valid values not zeros.
What is this line of yours for?
LBP_image = extractLBPFeatures(gray,'Cellsize',[32 32], 'Normaas i lization','None');
It doesn't even run. Instead of that name, let's just make a separate array for the results:
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 = 5;%numel(files);
results = 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], 'Normalization','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.
results(k, :) = reshape(Cell_Hists,1,[]);%Reshape the LBP features vector back to 1-by- N feature vector
end
if numberOfFiles >= 0
xlswrite(fileName, results);
end
I'm sorry, the "Normalization" in that line was just an error while coping it here.
I did what you suggested and it worked (separate array). Thank you sooo much, I appreciate it.
@nissrine Neyy can you share your update code?I'm getting this error -
Unable to perform assignment because the size of the left side is 1-by-3776 and the size of the right side is
1-by-343852.
@Jarin Ritu, then why didn't you share your code? Attach it in a new question. You didn't even tell us the line of code that threw that error. So in the new question put all the red text, not just part of it.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by