Unable to image files automatically with different file names

조회 수: 1 (최근 30일)
Warid Islam
Warid Islam 2021년 6월 7일
댓글: Warid Islam 2021년 6월 8일
Hi,
I have a GUi where I want to save the images from the 2nd image axes(handles.axes2) and generate file names automatically. Please find my code below:
function pushbutton1_Callback(hObject, eventdata, handles)
myFolder = 'D:\regionGrowing_MLT';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
num_files=length(theFiles);
imgs=cell(1,num_files);
handles.num_files=num_files;
handles.myFolder=myFolder;
handles.filePattern=filePattern;
handles.theFiles=theFiles;
handles.InputImage = {};
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
imshow(imageArray,'parent',handles.axes1); % Display image.
drawnow; % Force display to update immediately.
InputImage=imageArray;
% axes(handles.axes1);
handles.num_files=num_files;
handles.theFiles=theFiles;
handles.InputImage{end+1}= InputImage;
setappdata(handles.pushbutton1,'img',handles.InputImage);
guidata(hObject, handles);
end
end
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)
InputImage=handles.InputImage;
theFiles = handles.theFiles;
J=InputImage;
handles.seg={};
% J = getappdata(handles.pushbutton1,'img');
for i = 1:length(theFiles)
try
J{i}=rgb2gray(J{i});
end
x=300; y=340;
a=imgaussfilt(J{i},2);
b=adapthisteq(a);
m=regiongrowing_MLT(b,x,y,12);
m=imfill(m,'holes');
% figure,imshow(m)
bw=imbinarize(m);
bw=bwareafilt(bw,1);
% figure,imshow(bw)
I = imresize(m,.5); %-- make image smaller
m1 = imresize(bw,.5); % for fast computation
% subplot(2,2,1); imshow(I); title('Input Image');
% subplot(2,2,2); imshow(m1); title('Initialization');
% subplot(2,2,3); title('Segmentation');
seg = region_seg(I, m1, 30); %-- Run segmentation
imshow(seg,'parent',handles.axes2);
% axes(handles.axes2);
handles.seg{end+1}=seg;
setappdata(handles.pushbutton2,'img',handles.seg);
end
filename = fullfile(myFolder, sprintf('file%d.jpg',seg));
imwrite(InputImage, filename);
guidata(hObject, handles);
end
I want the results to be saved as file1.jpg,file2.jpg,file3.jpg, etc. Unfortunately, I get the following errors.
D:\regionGrowing_MLT\file0.jpgfile0.jpgfile0.jpgfile0.jpgfile0,...
for writing. You might not have write permission.
Error in ngui>pushbutton2_Callback (line 167)
imwrite(seg, filename);
I have attached few imput images. Any suggestions would be appreciated.

채택된 답변

Geoff Hayes
Geoff Hayes 2021년 6월 8일
Warid - the code error message
Error in ngui>pushbutton2_Callback (line 167)
imwrite(seg, filename);
does not correspond exactly to a line of code in your pushbutton2_Callback. The closest I could see was
imwrite(InputImage, filename);
Note that filename is set as
filename = fullfile(myFolder, sprintf('file%d.jpg',seg));
but nowhere in the function do you define what myFolder is. Perhaps you need to be using handles.myFolder.
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2021년 6월 8일
Hi Warid - but is myFolder set in your callback function? Have you tried using handles.myFolder?
Warid Islam
Warid Islam 2021년 6월 8일
Using handles.myFolder solved the problem for me. Thank you for your suggestion.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by