how to use imwrite in matlab?The file which that we are specifying to write,should it be already be created in the folder?

回答 (4 件)

Walter Roberson
Walter Roberson 2015 年 6 月 17 日

1 投票

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 件のコメント

Anushka
Anushka 2015 年 6 月 17 日
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 2015 年 6 月 18 日
Check your directory. It is not allowed to write into one of the subdirectories of the MATLAB installation.
Abdoo
Abdoo 2015 年 6 月 18 日
編集済み: Abdoo 2015 年 6 月 18 日
Notice, Check the file is close before you go on, because can't write data and file is open.
Guillaume
Guillaume 2015 年 6 月 18 日
Note: to close all open file handles:
fclose all

サインインしてコメントする。

B.k Sumedha
B.k Sumedha 2015 年 6 月 17 日
編集済み: B.k Sumedha 2015 年 6 月 17 日

0 投票

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 件のコメント

Anushka
Anushka 2015 年 6 月 17 日
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 2015 年 6 月 17 日
編集済み: B.k Sumedha 2015 年 6 月 17 日
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 2016 年 4 月 18 日
but what about imrwite of dicom images plz i need answer
Walter Roberson
Walter Roberson 2016 年 4 月 18 日
You cannot use imwrite() to write dicom images. You need dicomwrite()

サインインしてコメントする。

Alejandro Cruz Rubio
Alejandro Cruz Rubio 2019 年 6 月 1 日

0 投票

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

8 件のコメント

Walter Roberson
Walter Roberson 2019 年 6 月 1 日
Could you expand on your question ?
Alejandro Cruz Rubio
Alejandro Cruz Rubio 2019 年 6 月 1 日
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
Walter Roberson 2019 年 6 月 1 日
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 2019 年 6 月 1 日
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
Alejandro Cruz Rubio 2019 年 6 月 2 日
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 2019 年 6 月 2 日
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 2019 年 6 月 3 日
編集済み: Rik 2019 年 6 月 3 日
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 2019 年 6 月 3 日
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
kass 2020 年 1 月 29 日

0 投票

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

質問済み:

2015 年 6 月 17 日

回答済み:

2020 年 1 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by