how to use imwrite in matlab?

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
Walter Roberson on 17 Jun 2015
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

But Sir it shows the error Can't open file "Compressed Image.bmp" for writing. You may not have write permission.
Check your directory. It is not allowed to write into one of the subdirectories of the MATLAB installation.
Abdoo
Abdoo on 18 Jun 2015
Edited: Abdoo on 18 Jun 2015
Notice, Check the file is close before you go on, because can't write data and file is open.
Note: to close all open file handles:
fclose all

Sign in to comment.

B.k Sumedha
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

But Sir it shows the error Can't open file "Compressed Image.bmp" for writing. You may not have write permission.
B.k Sumedha
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.
but what about imrwite of dicom images plz i need answer
You cannot use imwrite() to write dicom images. You need dicomwrite()

Sign in to comment.

Anyone Know how to save a block of images with imwrite?

8 Comments

Could you expand on your question ?
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.
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
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.
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
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 ?
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
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

Sign in to comment.

for k=1:numel(I)
imwrite(I{k}, ['filename' num2str(k) '.pgm']);
end
%I is arraycells

Asked:

on 17 Jun 2015

Answered:

on 29 Jan 2020

Community Treasure Hunt

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

Start Hunting!