필터 지우기
필터 지우기

How can i save images at the end of every iteration?

조회 수: 3 (최근 30일)
Khushi Patni
Khushi Patni 2021년 6월 10일
댓글: Khushi Patni 2021년 6월 13일
I have a code in which I am taking images from 2 different folders, processing it according to my need and trying to save the output of every iteration in a separate folder. But as I try to save the file with the help of imwrite command, I am getting the following error:
Unable to open file "Img.jpg" for writing. You might not have write permission.
I tried using different formats of images. I also tried fullfile, sprintf but its still giving the same error.
Here's the code:
raw_folder = 'C:\raw';
back_folder = 'C:\background';
raw_filename = dir(fullfile(raw_folder,'*.tif'))
back_filename = dir(fullfile(back_folder,'*.tif'))
total = numel(raw_filename)
for n = 1:total
r = fullfile(raw_folder, raw_filename(n).name);
b = fullfile(back_folder, back_filename(n).name);
R = imread(r);
B = imread(b);
%---------------------------------------------------
% Some code.....
%---------------------------------------------------
figure;imagesc(out,[50 60]);
colormap(jet(255));
fileLoc = 'C:\newcolor';
fname = fullfile(fileLoc, strcat('Image',num2str(n),'.jpg'));
imwrite(out,fname,'jpg');
% path = 'C:\color';
% imwrite(out,fullfile(path,strcat(num2str(i),'.jpg')));
%---------------------------------------------------
end
How should I modify this code? Thank you.
  댓글 수: 4
Rik
Rik 2021년 6월 10일
And the code I posted tries to determine if Matlab can write in that folder at all.
Khushi Patni
Khushi Patni 2021년 6월 10일
I am getting this error: No such file or directory

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

채택된 답변

Image Analyst
Image Analyst 2021년 6월 12일
Try it this way:
% By Image Analyst
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
raw_folder = pwd; %'C:\raw';
back_folder = pwd; %'C:\background';
raw_filename = dir(fullfile(raw_folder,'*.tif'))
back_filename = dir(fullfile(back_folder,'*.tif'))
numRawFiles = numel(raw_filename)
outputFolder = fullfile(raw_folder, 'Output');
if ~isfolder(outputFolder)
fprintf('Creating new folder for output called "%s".\n', outputFolder);
mkdir(outputFolder);
end
for n = 1:numRawFiles
inputFileName = fullfile(raw_folder, raw_filename(n).name);
backgroundFileName = fullfile(back_folder, back_filename(n).name);
R = imread(inputFileName);
B = imread(backgroundFileName);
%---------------------------------------------------
% Some code.....
out = R;
%---------------------------------------------------
hFig = figure;
imagesc(out,[50 60]);
colormap(jet(255));
drawnow;
%---------------------------------------------------
baseFileName = sprintf('Image %2.2d.png', n); % Don't use JPG - use PNG instead.
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf('Writing out "%s"\n', fullOutputFileName);
imwrite(out, fullOutputFileName);
% Delete figure
close(hFig);
end
  댓글 수: 6
Image Analyst
Image Analyst 2021년 6월 13일
@Khushi Patni in the code you showed, n was not a vector, but then you did leave out some code where maybe you reassigned n. You should not reassign the loop iterator variable inside a loop. It will use it with your new value but once the loop starts again, it will ignore your change. My code did not have n as a vector. So, if we're done here, could you "Accept this answer"? Thanks in advance.
Khushi Patni
Khushi Patni 2021년 6월 13일
Yes. You are right! Thankyou!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 6월 10일
Try this:
fileLoc = 'E:\Internship\SAMEER\dataset\CONTROLLED\advanced\newcolor';
if ~isfolder(fileLoc)
fprintf('Creating new folder called "%s".\n', fileLoc);
mkdir(fileLoc);
end
  댓글 수: 6
Rik
Rik 2021년 6월 12일
Why did you change your code so that n is a vector with non-integer values, but didn't say anything about what code you were using?
Image Analyst
Image Analyst 2021년 6월 12일
See code in my other answer I posted earlier today.

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

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by