" Unable to determine the file formt from the file name" ERROR. Please help!
5 views (last 30 days)
Show older comments
Hi everyone,
I have two push buttons in my GUI. One allows the user to choose a folder in which the processed images will go and the other runs some ode to process images and then uses 'imwrite' to save them into the folder as previously chosen.
However, using imwrite I get the error (as stated in the title) and as far as I can see it should work!
Here is the code for my 'save' pushbutton:
% --- 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);
Here is the code for the main processing bit. At the end is the issue I'm having with the 'imwrite' function:
% --- 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);
outDirectory = fullfile(outDir);
imwrite(BW, outDirectory);
%Update waitbar with current image/total number of images to process:
waitbar(k/perc, h);
drawnow;
end
delete(h);
And finally, here is the error:
Error using imwrite (line 426) Unable to determine the file format from the file name.
Error in ROI_loop_gui>pushbutton1_Callback (line 142) imwrite(BW, outDirectory);
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in ROI_loop_gui (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ROI_loop_gui('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Can anyone see what I have done wrong? Have I misused the handles feature or the imwrite function?
Many thanks,
Ellis
0 Comments
Answers (1)
Stephen23
on 8 Jun 2016
Edited: Stephen23
on 8 Jun 2016
Assuming that outDir is a directory path, then you are calling imwrite without any file extension or format (and actually even without any filename), so it rightly throws an error (because it can't just imagine what file format you want to use). Actually neither of the two instances of imwrite are correct. In the commented-out version, why is outDir supplied as an input argument? This is not a supported syntax:
%imwrite(BW,[pathname, 'BW', num2str(k), '.jpg'], outDir);
And for the one that you are calling, where is the filename or file format defined?:
outDirectory = fullfile(outDir); % where is the filename?
imwrite(BW, outDirectory); % filename or format ?
The imwrite documentation tell us how to use the function imwrite, and it shows us all of the correct ways to use the function. You probably want to do something like his:
%newName = sprintf('Image_%d.jpg',k); % in a loop
newName = 'MyImage.jpg'; % or whatever you want..
newFile = fullfile(outDir,newName);
imwrite(BW, newFile)
2 Comments
See Also
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!