Info

此问题已关闭。 请重新打开它进行编辑或回答。

GUI - alpha numerical ordering problem with dir() help

1 次查看(过去 30 天)
Hi everybody,
--The problem below, I have been told is due to Matlab using an 'alpha-numerical' ordering system when retrieving files for a directory. As I am now aware, is there anyway I can sort this? I need to make sure the files are processed sequentially from the directory as in 'image1' 'image2' etc!--
Hi everyone,
I have a very trivial issue here but I cannot see what I have done wrong. In a for loop I have created I get the code to retrieve the images from a folder (myFolder) and then run some code on them. It processes one picture at a time and works well, but for some reason doesn't process them in order. In the folder, the images are numbered from '1' to '33' so it should process 1 first and 33 last. However, it processes them randomly, number 9 being the last one to get processed for some bizarre reason! Here is my code, can anyone please see how I have misused the for loop for this to happen? :
% --- Executes on button press in pushbutton4. RUN PROGRAMME function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%The picture interval is entered in editbox1, if not, this error message %appears. Interval=str2num(char(get(handles.edit1,'String'))); %Prompt for user to enter camera interval. if isempty(Interval) errordlg('Error, please load pictures to be processed and enter a picture time interval before clicking Run.'); else
outDir = handles.outDir;
inDir = handles.inDir;
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
myFolder=handles.inDir;
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(inDir, '*.JPG');
theFiles=dir(filePattern);
caListBoxItems = cell(length(theFiles), 1);
h=waitbar(0, 'Please Wait...');
for k=1:length(theFiles)
RGB = imread(fullfile(inDir, theFiles(k).name));
perc = numel(theFiles);
newRGB = imcrop(RGB,[handles.xMin handles.yMin handles.xMax-handles.xMin handles.yMax-handles.yMin]);
% Convert RGB image to chosen color space
I = rgb2hsv(newRGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.053;
channel1Max = 0.083;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.116;
channel2Max = 0.130;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.608;
channel3Max = 0.643;
% Create mask based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% Invert mask
BW = ~BW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
%newName = sprintf('Image_%d.jpg', k);
newName = ['Processed Image', num2str(k), '.jpg'];
newFile = fullfile(outDir, newName);
imwrite(BW, newFile);
%Update waitbar with current image/total number of images to process:
waitbar(k/perc, h);
drawnow;
end
delete(h);
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
myFolder=handles.outDir;
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(myFolder, '*.JPG');
theFiles=dir(filePattern);
caListBoxItems = cell(length(theFiles), 1);
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
thisString = fprintf(1, 'Now reading %s', fullFileName);
fprintf('%s\n', thisString);
caListBoxItems{k} = thisString;
OutputFileNames{k} = theFiles(k).name;
set(handles.listbox2, 'String', OutputFileNames); %listbox2 will display file names of processed images to listbox2.
drawnow; % Force immediate screen repaint.
image=imread(fullFileName);
white=nnz(image);
black=(numel(image)-white);
time=((k-1)*Interval);
if black>(numel(image)*0.0193); %if black pixels is more than 1.93% of pixels in image, experiment complete. Value can be altered.
disp('The experiment is complete at this stage!')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
disp('Time of Picture (Secs): ');
disp((k-1)*Interval); %Here, "Interval" is a variable chosen by user (15 secs, 30 secs etc)
Status = ('Complete');
else
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
Status = ('Incomplete');
end
PhotoDetails={fullFileName, black, time, Status};
Matrix(k,:)=PhotoDetails; %Matrix of three variables produced.
guidata(hObject,handles);
end end Header={'Image', 'Number of Black Pixels', 'Time (Seconds)', 'Status'}; xlswrite('PhotoResults', Header, 'Results'); xlRange='A2'; xlswrite('PhotoResults', Matrix, 'Results', xlRange);
Now, here is what the output of running this code, as you can see it processes the images in a random order, not from 1 to 33!! :
Now reading H:\Documents\o2\Processed Image1.jpg0 The experiment is not complete yet. The number of Black Pixels is: 27
Now reading H:\Documents\o2\Processed Image10.jpg1 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image11.jpg1 The experiment is not complete yet. The number of Black Pixels is: 12
Now reading H:\Documents\o2\Processed Image12.jpg1 The experiment is not complete yet. The number of Black Pixels is: 13
Now reading H:\Documents\o2\Processed Image13.jpg1 The experiment is not complete yet. The number of Black Pixels is: 24
Now reading H:\Documents\o2\Processed Image14.jpg1 The experiment is not complete yet. The number of Black Pixels is: 117
Now reading H:\Documents\o2\Processed Image15.jpg1 The experiment is not complete yet. The number of Black Pixels is: 18
Now reading H:\Documents\o2\Processed Image16.jpg1 The experiment is not complete yet. The number of Black Pixels is: 10
Now reading H:\Documents\o2\Processed Image17.jpg1 The experiment is not complete yet. The number of Black Pixels is: 159
Now reading H:\Documents\o2\Processed Image18.jpg1 The experiment is not complete yet. The number of Black Pixels is: 159
Now reading H:\Documents\o2\Processed Image19.jpg1 The experiment is not complete yet. The number of Black Pixels is: 41
Now reading H:\Documents\o2\Processed Image2.jpg0 The experiment is not complete yet. The number of Black Pixels is: 0
Now reading H:\Documents\o2\Processed Image20.jpg1 The experiment is not complete yet. The number of Black Pixels is: 229
Now reading H:\Documents\o2\Processed Image21.jpg1 The experiment is not complete yet. The number of Black Pixels is: 194
Now reading H:\Documents\o2\Processed Image22.jpg1 The experiment is not complete yet. The number of Black Pixels is: 110
Now reading H:\Documents\o2\Processed Image23.jpg1 The experiment is not complete yet. The number of Black Pixels is: 225
Now reading H:\Documents\o2\Processed Image24.jpg1 The experiment is not complete yet. The number of Black Pixels is: 375
Now reading H:\Documents\o2\Processed Image25.jpg1 The experiment is not complete yet. The number of Black Pixels is: 125
Now reading H:\Documents\o2\Processed Image26.jpg1 The experiment is not complete yet. The number of Black Pixels is: 869
Now reading H:\Documents\o2\Processed Image27.jpg1 The experiment is not complete yet. The number of Black Pixels is: 568
Now reading H:\Documents\o2\Processed Image28.jpg1 The experiment is not complete yet. The number of Black Pixels is: 547
Now reading H:\Documents\o2\Processed Image29.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1121
Now reading H:\Documents\o2\Processed Image3.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Now reading H:\Documents\o2\Processed Image30.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1875
Now reading H:\Documents\o2\Processed Image31.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1617
Now reading H:\Documents\o2\Processed Image32.jpg1 The experiment is not complete yet. The number of Black Pixels is: 2108
Now reading H:\Documents\o2\Processed Image33.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1849
Now reading H:\Documents\o2\Processed Image4.jpg0 The experiment is not complete yet. The number of Black Pixels is: 21
Now reading H:\Documents\o2\Processed Image5.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Now reading H:\Documents\o2\Processed Image6.jpg0 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image7.jpg0 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image8.jpg0 The experiment is not complete yet. The number of Black Pixels is: 1
Now reading H:\Documents\o2\Processed Image9.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Does anyone have any idea what I have done wrong?
Thanks so much,
Ellis

回答(1 个)

Francesco Onorati
It is not in a random order, it is AN order (actually, the order you asked, i.e. alphanumerical order) smthng_Image1 smthng_Image10 smthng_Image11 ... smthng_Image2 smthng_Image21 ... smthng_Image9
solution 1
re-name Image1 --> Image01; Image2 --> Image02; ... ; Image9 --> Image09
solution 2
use regular expressions and rules
good luck,
F

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by