필터 지우기
필터 지우기

GUI - Real-Time updates displayed in listbox

조회 수: 2 (최근 30일)
Ellis Berry
Ellis Berry 2016년 3월 16일
답변: Adam 2016년 3월 16일
Hi everybody, I'm making a gui for image processing. So far, I press pushbutton1, you browse for a folder of images you want to process and select it. The contents of the folder is displayed onto listbox1. Then, I press pushbutton3 and it runs my main code (various image processing techniques). Here is my code so far:
function varargout = GUI_2(varargin)
%GUI_2 M-file for GUI_2.fig
% GUI_2, by itself, creates a new GUI_2 or raises the existing
% singleton*.
%
% H = GUI_2 returns the handle to a new GUI_2 or the handle to
% the existing singleton*.
%
% GUI_2('Property','Value',...) creates a new GUI_2 using the
% given property value pairs. Unrecognized properties are passed via
% varargin to GUI_2_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% GUI_2('CALLBACK') and GUI_2('CALLBACK',hObject,...) call the
% local function named CALLBACK in GUI_2.M with the given input
% arguments.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI_2
% Last Modified by GUIDE v2.5 02-Mar-2016 16:10:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_2_OpeningFcn, ...
'gui_OutputFcn', @GUI_2_OutputFcn, ...
'gui_LayoutFcn', [], ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI_2 is made visible.
function GUI_2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for GUI_2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Load up the listbox with tif files in folder handles.handles.ImageFolder
% get the folder
folder_name = uigetdir;
% get what is inside the folder
Infolder = dir(folder_name);
MyListOfFiles = {Infolder(~[Infolder.isdir]).name};
set(handles.listbox1,'String', MyListOfFiles);
% update handles with the inDir
handles.inDir = folder_name;
guidata(hObject,handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file,path]=uiputfile('*.jpg','Save Destination');
% --- Executes on button press in pushbutton3. %MAIN!!
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
outDir = 'H:\Documents\DIP\Batch_processed_HSV';
inDir = handles.inDir;
includeSubdirectories = true;
% All extensions that can be read by IMREAD
imreadFormats = imformats;
supportedExtensions = [imreadFormats.ext];
% Add dicom extensions
supportedExtensions{end+1} = 'dcm';
supportedExtensions{end+1} = 'ima';
supportedExtensions = strcat('.',supportedExtensions);
% Allow the 'no extension' specification of DICOM
supportedExtensions{end+1} = '';
% Create a image data store that can read all these files
imds = datastore(inDir,...
'IncludeSubfolders', includeSubdirectories,...
'Type','image',...
'FileExtensions',supportedExtensions);
% Process each image using trial_3 (Image Processing toolbox app that let
% me set the HSV thresholds).
for imgInd = 1:numel(imds.Files)
inImageFile = imds.Files{imgInd};
% Output has the same sub-directory structure and file extension as
% input
outImageFile = strrep(inImageFile, inDir, outDir);
try
% Read
im = imds.readimage(imgInd);
% Process
im = trial_3(im);
% Write
if(isdicom(inImageFile))
dicommeta = dicominfo(inImageFile);
dicomwrite(im, outImageFile, dicommeta, 'CreateMode', 'copy');
else
imwrite(im, outImageFile);
end
disp(['PASSED:', inImageFile]);
catch allExceptions
disp(['FAILED:', inImageFile]);
disp(getReport(allExceptions,'basic'));
end
end
%Specify the folder where the files (Pictures) live.
myFolder='H:\Documents\DIP\Batch_Processed_HSV';
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(myFolder, '*.JPG');
theFiles=dir(filePattern);
Interval=str2num(char(get(handles.edit2,'String'))); %Prompt for user to enter camera interval.
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName=fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
image=imread(fullFileName);
white=nnz(image);
black=(numel(image)-white);
if black>(numel(image)*0.0193); %if black pixels is more than 1.94% 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)
else
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
end
set(handles.listbox2,'String', fullFileName);
guidata(hObject,handles);
end
% --- Executes on button press in pushbutton4.
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)
msgbox('Thankyou for using the Image Processing Tool');
pause(3);
close();
close();
% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox2
% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
and it works fine BUT I want the results to be displayed in listbox2 rather than in matlab command window? It seems easy but I can't figure it out. Here are my results from running the code, and I want them to appear, line by line as the code runs, in the listbox2:
>> GUI_2 PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1383.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1384.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1385.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1386.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1387.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1388.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1389.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1390.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1391.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1392.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1393.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1394.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1395.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1396.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1397.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1398.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1399.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1400.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1401.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1402.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1403.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1404.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1405.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1406.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1407.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1408.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1409.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1410.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1411.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1412.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1413.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1414.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1415.JPG Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1383.JPG The experiment is not complete yet. The number of Black Pixels is: 97
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1384.JPG The experiment is not complete yet. The number of Black Pixels is: 44
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1385.JPG The experiment is not complete yet. The number of Black Pixels is: 55
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1386.JPG The experiment is not complete yet. The number of Black Pixels is: 94
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1387.JPG The experiment is not complete yet. The number of Black Pixels is: 22
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1388.JPG The experiment is not complete yet. The number of Black Pixels is: 39
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1389.JPG The experiment is not complete yet. The number of Black Pixels is: 61
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1390.JPG The experiment is not complete yet. The number of Black Pixels is: 44
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1391.JPG The experiment is not complete yet. The number of Black Pixels is: 145
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1392.JPG The experiment is not complete yet. The number of Black Pixels is: 134
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1393.JPG The experiment is not complete yet. The number of Black Pixels is: 82
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1394.JPG The experiment is not complete yet. The number of Black Pixels is: 87
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1395.JPG The experiment is not complete yet. The number of Black Pixels is: 153
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1396.JPG The experiment is not complete yet. The number of Black Pixels is: 267
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1397.JPG The experiment is not complete yet. The number of Black Pixels is: 103
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1398.JPG The experiment is not complete yet. The number of Black Pixels is: 92
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1399.JPG The experiment is not complete yet. The number of Black Pixels is: 351
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1400.JPG The experiment is not complete yet. The number of Black Pixels is: 264
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1401.JPG The experiment is not complete yet. The number of Black Pixels is: 186
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1402.JPG The experiment is not complete yet. The number of Black Pixels is: 379
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1403.JPG The experiment is not complete yet. The number of Black Pixels is: 435
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1404.JPG The experiment is not complete yet. The number of Black Pixels is: 274
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1405.JPG The experiment is not complete yet. The number of Black Pixels is: 614
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1406.JPG The experiment is not complete yet. The number of Black Pixels is: 802
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1407.JPG The experiment is not complete yet. The number of Black Pixels is: 62
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1408.JPG The experiment is not complete yet. The number of Black Pixels is: 1497
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1409.JPG The experiment is not complete yet. The number of Black Pixels is: 442
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1410.JPG The experiment is not complete yet. The number of Black Pixels is: 510
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1411.JPG The experiment is not complete yet. The number of Black Pixels is: 1876
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1412.JPG The experiment is complete at this stage! The number of Black Pixels is: 3182
Time of Picture (Secs): 290
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1413.JPG The experiment is complete at this stage! The number of Black Pixels is: 2762
Time of Picture (Secs): 300
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1414.JPG The experiment is complete at this stage! The number of Black Pixels is: 3740
Time of Picture (Secs): 310
Now reading H:\Documents\DIP\Batch_Processed_HSV\IMG_1415.JPG The experiment is complete at this stage! The number of Black Pixels is: 2758
Time of Picture (Secs): 320
Can anybody advise me? Also, an added challenge; at the moment I have an unused 'pushbutton2'. What I would ideally like to do is use this to browse the computer for a folder to save the processed images to? So the user can choose this instead of having the outDir (save location) preset, like it is now to 'H:\Documents\DIP\Batch_Processed_HSV'. Any ideas? Many thanks,
Ellis

답변 (1개)

Adam
Adam 2016년 3월 16일
Why can you not just do what you did for the other listbox? i.e. collect your outputs into a cell array and set it as the listbox string. Declare a cell array upfront and keep adding to it as you go through your loop. If you can pre-declare the size that would help possibly although I am not too familiar with the ins and outs of growing cell arrays.
To do it live you would have to keep on setting the listbox string within your loop which adds an overhead, but probably not a significant one.

Community Treasure Hunt

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

Start Hunting!

Translated by