how to use imwrite in matlab?
Show older comments
how to use imwrite in matlab?The file which that we are specifying to write,should it be already be created in the folder?
Answers (4)
Walter Roberson
on 17 Jun 2015
1 vote
No. If the file does not already exist then it will be created. If the file does already exist then it will be overwritten.
4 Comments
Anushka
on 17 Jun 2015
Walter Roberson
on 18 Jun 2015
Check your directory. It is not allowed to write into one of the subdirectories of the MATLAB installation.
Guillaume
on 18 Jun 2015
Note: to close all open file handles:
fclose all
B.k Sumedha
on 17 Jun 2015
Edited: B.k Sumedha
on 17 Jun 2015
imwrite(A,filename,fmt);
This is the general format of imwrite. Its not necessary that image needs to be present in ur folder. Where A is ur image which u want to save,specify the file name and its format. For ex:
imwrite(im_DIF,'Image difference.bmp','bmp');
4 Comments
Anushka
on 17 Jun 2015
B.k Sumedha
on 17 Jun 2015
Edited: B.k Sumedha
on 17 Jun 2015
Do u have the same filename existing in the current folder? Have u specified the correct path where the image needs to be written.
If ur saving it in a new directory then be sure that the directory exists in the current path.
mima zebouchi
on 18 Apr 2016
but what about imrwite of dicom images plz i need answer
Walter Roberson
on 18 Apr 2016
Alejandro Cruz Rubio
on 1 Jun 2019
0 votes
Anyone Know how to save a block of images with imwrite?
8 Comments
Walter Roberson
on 1 Jun 2019
Could you expand on your question ?
Alejandro Cruz Rubio
on 1 Jun 2019
Yes, sorry.
I have 45 blocks of 256x256 resolution each block in a same image, tha is, I have an image with 45 blocks and I need to save each block in a different image. Do you know how to do that? I am using imwrite function but I only get the first block and I need to save all of them as a different image each of them.
Walter Roberson
on 1 Jun 2019
If the blocks are a cell array, then
for K = 1 : numel(YourImageCell)
filename = sprintf('Output_block_%d.png', K);
imwrite(YourImageCell{K}, filename);
end
Guillaume
on 1 Jun 2019
I'd recommend %02d instead of plain %d in the format string of sprintf just so that when you list the files the alphabetical order and the numerical order match.
Alejandro Cruz Rubio
on 2 Jun 2019
I have a cell array whose cells contains subarrays, that is, I`ve used mat2cell function and if I use the code above I get an error message indicating "Unable to determine the file format from the file name" in imwrite function. After that, I extract the array of numbers out of the cell and I get one simple image with all the blocks. The code is:
wholeBlockR = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockR), rem(rows, blockSizeR)];
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
arrayCells=mat2cell(Image,blockVectorR, blockVectorC);
plotIndex=1;
numPlotsR = size(arrayCeldas, 1);
numPlotsC = size(arrayCeldas, 2);
for r = 1 : numPlotsRow
for c = 1 : numPlotsCol
subplot(numPlotsRow, numPlotsCol, plotIndex);
block = arrayCells{r,c};
imshow(block); % In this moment I show all the blocks in a simple image and each %block is an image.
close
plotIndex = plotIndex + 1;
end
end
% In this point I need to extract each block and save it as an image.
for k=1:numel(arrayCells)
filename = sprintf('Output_block_%d.png', K);
imwrite(arrayCells{k}, filename); % in this point I get the error shown % above
end
Walter Roberson
on 2 Jun 2019
Be careful, you switched from for k with lower-case K, to using upper-case K in the filename.
Is it possible that you are using a very old MATLAB? Such as MATLAB 5.x or MATLAB 6.0 ?
Alejandro Cruz Rubio
on 3 Jun 2019
Edited: Rik
on 3 Jun 2019
My version of Matlab is 2018b. Yeah I solve the problem with K and k and I get the same error. And if I use the arrayCells inside of for, that is:
for k=1:numel(arrayCells)
filename = sprintf('Output_block_%d.png', K);
imwrite(arrayCells{k}, filename);
end
I get this error: "Error using imwrite (line 433)
Unable to determine the file format from the file name" because I use arrayCells but if I use "block" I don´t have any problem but I don´t get my goal
Rik
on 3 Jun 2019
With the code you show you actually haven't fixed the code yet. The code below should work a lot better.
for k=1:numel(arrayCells)
filename = sprintf('Output_block_%d.png', k);%<--- lower case k, instead of upper case K
imwrite(arrayCells{k}, filename);
end
kass
on 29 Jan 2020
for k=1:numel(I)
imwrite(I{k}, ['filename' num2str(k) '.pgm']);
end
%I is arraycells
Categories
Find more on Import, Export, and Conversion 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!