Function returns different value when applied on multiple images using a loop

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 comentarios

What you men by different values ? Is the vector different for a particular image?
What i mean is that let's say i ran the code(without the loop) on a single image "img1" and i got the feature vector of size 1x256 for that image. But when i ran the code on all 196 images i got the feature vector of size 196x256. Now that i compare the values of img1 in both feature vectors they are Not the same they are different rather they should have been same. All i did was ran the same code and appended the obtained vectors in an array.
And yes for each image the vector is different...its basically pixel counts of gray level in the Lbp img
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.
All the images in the folder are sorted by names like this: Kang0, Kang1,Kang2,......,kang196. I dont believe if Matlab is doing it otherwise. Ive checked the the column of first img1 has correct values but for later imgs the values are different.
Please if you can provide me some other LBP code i'll be greatful because now im unsure there might something be wrong with my LBP code.
MaryD thank you so much!
you were right...this is exaclty what MATLAB is doing.its reading the images this way kang0 kang1 kang10 and so on
Matlab is reading images like this :kang0 ,kang1, kang10,kang100,kang101 and so on
Please tell me if there is a way i can make Matlab read in the right order like kang0,kang1,kang2,kang3.....so on
something like this in a loop: kang[i]
and "i" should change/increment in each order.
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()

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 1 de Jul. de 2020

Comentada:

el 2 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by