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?

Réponses (4)

Walter Roberson
Walter Roberson le 17 Juin 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 commentaires

Anushka
Anushka le 17 Juin 2015
But Sir it shows the error Can't open file "Compressed Image.bmp" for writing. You may not have write permission.
Walter Roberson
Walter Roberson le 18 Juin 2015
Check your directory. It is not allowed to write into one of the subdirectories of the MATLAB installation.
Abdoo
Abdoo le 18 Juin 2015
Modifié(e) : Abdoo le 18 Juin 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

Connectez-vous pour commenter.

B.k Sumedha
B.k Sumedha le 17 Juin 2015
Modifié(e) : B.k Sumedha le 17 Juin 2015

0 votes

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 commentaires

Anushka
Anushka le 17 Juin 2015
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 le 17 Juin 2015
Modifié(e) : B.k Sumedha le 17 Juin 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
mima zebouchi le 18 Avr 2016
but what about imrwite of dicom images plz i need answer
Walter Roberson
Walter Roberson le 18 Avr 2016
You cannot use imwrite() to write dicom images. You need dicomwrite()

Connectez-vous pour commenter.

Alejandro Cruz Rubio
Alejandro Cruz Rubio le 1 Juin 2019

0 votes

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

8 commentaires

Walter Roberson
Walter Roberson le 1 Juin 2019
Could you expand on your question ?
Alejandro Cruz Rubio
Alejandro Cruz Rubio le 1 Juin 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.
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
Guillaume le 1 Juin 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.
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
Walter Roberson le 2 Juin 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
Alejandro Cruz Rubio le 3 Juin 2019
Modifié(e) : Rik le 3 Juin 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
Rik le 3 Juin 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

Connectez-vous pour commenter.

kass
kass le 29 Jan 2020

0 votes

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

Question posée :

le 17 Juin 2015

Réponse apportée :

le 29 Jan 2020

Community Treasure Hunt

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

Start Hunting!

Translated by