GUI - Saving image to pre specified folder

조회 수: 3 (최근 30일)
Ellis Berry
Ellis Berry 2016년 6월 8일
답변: Geoff Hayes 2016년 6월 8일
Hi everybody,
On one pushbutton I have made some code that allows the user to select a folder of their choice to use as the folder in which processed images will be saved. Here is the code for the 'choose save location' button:
% --- Executes on button press in pushbutton3. SAVE
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)
%Choose which folder processed pics will go in.
save_folder_name = uigetdir;
%Update handles with the outDir
handles.outDir = save_folder_name;
guidata(hObject,handles);
Now, when the processing is done on another pushbutton, I want to save the image each time the code loops to the folder specified in that last pushbutton. Is there a way I can use the imwrite function in a way such as 'imwrite(outDir)' or something like that? If I just use imwrite then it saves the images to my documents but not in the folder I chose in the other pushbutton. Any ideas?
Here is the code which runs the loop:
% --- Executes on button press in pushbutton1. RUN
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)
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);
% if k ==1
% axes(handles.axes1)
% [BW, xi, yi] = roipoly(RGB);
% xMin = min(xi);
% xMax = max(xi);
% yMin = min(yi);
% yMax = max(yi);
% end
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;
pathname = outDir; %handles.outDir??
imwrite(BW,[pathname, 'BW', num2str(k), '.jpg'], outDir);
%Update waitbar with current image/total number of images to process:
waitbar(k/perc, h);
drawnow;
end
delete(h);
many thanks,
Ellis

답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 6월 8일
Ellis - since you are saving the output directory to handles.outDir then you will need to use this in your call to imwrite. For example,
function pushbutton1_Callback(hObject, eventdata, handles)
% your code
for k=1:length(theFiles)
% etc.
% define the file name
filename = sprintf('BW%d.jpg',k);
% define the full path and filename
fullFilename = fullfile(handles.outDir, filename);
imwrite(BW, fullFilename);
end
Try the above and see what happens!

카테고리

Help CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by