フィルターのクリア

GUI - Batch Image Proessing Help!

2 ビュー (過去 30 日間)
Ellis Berry
Ellis Berry 2016 年 3 月 15 日
回答済み: Geoff Hayes 2016 年 8 月 14 日
Hi everyone, This question is a duplicate to one I have already asked...for some reason I cannot comment on answers to my question on this PC so am forced to make a new question. Anyway here is my question: So I've got a challenging problem. I have made a GUI and basically I want press 'pushbutton1' and load a folder of images to process. These image names will be loaded onto listbox1 to see and then, I want to press 'pushbutton3' to run the main code. This code, which works fine in editor, processes all the images and outputs them to another folder. Now, the trouble I'm having is putting this code into a gui. So I want the 'inDir' to be whatever folder I chose from pushbutton1? But I cant get it to work. This 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)
if(nargin<2)
outDir = 'H:\Documents\DIP\Batch_processed_HSV';
end
if(nargin<1)
inDir = 'MyListOfFiles';
end
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);
imds.ReadFcn = @readSupportedImage;
% 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);
% Create (sub)directory if needed
outSubDir = fileparts(outImageFile);
createDirectory(outSubDir);
% 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=input('Enter the time interval for the camera (Seconds):') %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
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 this is the error message I get once I click pushbutton3:
>> GUI_2
Undefined function or variable 'inDir'.
Error in GUI_2>pushbutton3_Callback (line 154)
imds = datastore(inDir,...
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI_2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI_2('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
The GUI works fine in loading the image names to listbox1, but implementing this batch image processing code into the GUI is hard. A kind person named Geoff did help me with this problem and said that a there is a problem with my 'vargin'? Apparently the number of arguments for 'vargin' will always be three? And so, inDir and outDir will never be defined? How do I fix this?
Many thanks everyone, Ellis

回答 (1 件)

Geoff Hayes
Geoff Hayes 2016 年 8 月 14 日
Ellis - while I suspect that your question has been answered already, the problem is with the pushbutton3 callback and the use of nargin
function pushbutton3_Callback(hObject, eventdata, handles)
The number of arguments in to this function, nargin, will always be three since GUIDE has defined three input parameters to this function: hObject, eventdata, and handles. So the subsequent code
if(nargin<2)
outDir = 'H:\Documents\DIP\Batch_processed_HSV';
end
if(nargin<1)
inDir = 'MyListOfFiles';
end
will never be evaluated since nargin is identical to three. So what should outDir be? For inDir it is clear - you have added this as a field to the handles structure so when you want to access inDir it is through this structure as
imds = datastore(handles.inDir, ...
etc.

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by