Function returns different value when applied on multiple images using a loop
Show older comments
I have total 196 (.jpg) images in a folder whose LBP features i want to extract. Also i want to save the feature vectors of all 196 images in one matrix/array.
I have written a code for LBP to be applied on images, when i apply the code on a single images it returns the correct 1x256 size feature vector with the right values(which i need). But when the same code is applied on multiple images in a folder via loop it returns different values...i dont uderstand how come the values are different? Much help is needed. Thankx in advance
clear all;
close all;
clc;
location = 'E:\img_folder\*.jpg'; % folder in which 196 images exists
ds = imageDatastore(location); % datastore
Final =[]; % the final marix in which i wish to append feature vector of all images in the folder
while hasdata(ds) %using while to loop through all images
I2=read(ds); % read image from datastore
w=size(I2,1);
h=size(I2,2);
scale = 2.^[7 6 5; 0 -inf 4; 1 2 3];
%LBP code
for i=2:w-1
for j=2:h-1
J0=I2(i,j);
I3(i-1,j-1)=I2(i-1,j-1)>J0;
I3(i-1,j)=I2(i-1,j)>J0;
I3(i-1,j+1)=I2(i-1,j+1)>J0;
I3(i,j+1)=I2(i,j+1)>J0;
I3(i+1,j+1)=I2(i+1,j+1)>J0;
I3(i+1,j)=I2(i+1,j)>J0;
I3(i+1,j-1)=I2(i+1,j-1)>J0;
I3(i,j-1)=I2(i,j-1)>J0;
LBP(i,j)=I3(i-1,j-1)*2^7+I3(i-1,j)*2^6+I3(i-1,j+1)*2^5+I3(i,j+1)*2^4+I3(i+1,j+1)*2^3+I3(i+1,j)*2^2+I3(i+1,j-1)*2^1+I3(i,j-1)*2^0;
end
end
[pixelCounts, GLs] = imhist(uint8(LBP));
Final=[Final;pixelCounts];
end
I know there is nothing wrong with the LBP code but something with concatenation in the Final[] matrix/array.
8 Comments
MaryD
on 1 Jul 2020
What you men by different values ? Is the vector different for a particular image?
Unqua Laraib
on 1 Jul 2020
Unqua Laraib
on 1 Jul 2020
MaryD
on 2 Jul 2020
It's possible that with such an image names they are read in alphabetical order so after img1 next would be img10 then img11, img111 ang img2 would be read much later. I hope you can understand what i mean. Check if the first image gets same results in single run and in the loop. If not problem might be somewhere else.
Unqua Laraib
on 2 Jul 2020
Unqua Laraib
on 2 Jul 2020
Unqua Laraib
on 2 Jul 2020
Walter Roberson
on 2 Jul 2020
There is nothing you can do directly to have it process the files in numeric order. datastores retrieve the file names from the directories in whatever order the operating system happens to present them, and there is no "order" option.
However, instead of passing the datastore a pattern with wildcards, you can find the file names, and use a sort routine such as https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort and then pass the cell array of file names to the imagedatastore()
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!