How can i extract and save LBP features of multiple images, so that i can use them as a DB features ?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
nissrine Neyy
el 25 de Dic. de 2020
Comentada: Image Analyst
el 27 de Jun. de 2022
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 comentarios
Lee Hau
el 27 de Jun. de 2022
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,[]);
Image Analyst
el 27 de Jun. de 2022
@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);
Respuesta aceptada
Image Analyst
el 26 de Dic. de 2020
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 comentarios
Jarin Ritu
el 17 de Jun. de 2021
@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.
Image Analyst
el 17 de Jun. de 2021
@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.
Más respuestas (0)
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!