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

5 次查看(过去 30 天)
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 个评论
Unqua Laraib
Unqua Laraib 2020-7-2
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.
Walter Roberson
Walter Roberson 2020-7-2
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()

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by