Save file.bmp from a matrix.

46 ビュー (過去 30 日間)
Michele Vanini
Michele Vanini 2021 年 4 月 19 日
回答済み: Image Analyst 2021 年 4 月 19 日
Dear all,
I am quite new in Matlab and I am eperiencing some issue by saving a matrix as an image.
I have a matrix (202 x 134 double) composed of 0 and 1. I want to generate a black and white image from that.
If used: imshow(mymat), and I save the file from that it is perfect; but I have to save images within a loop.
I tried to use:
imwrite(mymat,sprintf('FIG%d.bmp',z1))%,'Quality',100)
But the quality of the image is very very low as you can see from the attached pictured.
I tried also to use:
image = mat2gray(mymat,[0 1]);
imwrite(image,sprintf('FIG%d.bmp',z1))
But I get the same result.
Thanks in advance.
  4 件のコメント
Rik
Rik 2021 年 4 月 19 日
How did you get the idea you have a 202x134 matrix? Nothing so far indicates that. Using a different function will not fix anything if your input data is not what you expect.
Michele Vanini
Michele Vanini 2021 年 4 月 19 日
編集済み: Michele Vanini 2021 年 4 月 19 日
Because in the script above I changed the value of d from 1 to 0.5 and obtained a 202x134 matrix. With d = 1 I obtain a 101x67 matrix which is the one used to generate the image.

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

採用された回答

Image Analyst
Image Analyst 2021 年 4 月 19 日
In your code, you save the image as JPEG - that's probably why it's blurry. Never use JPG for image analysis.
%save image as jpeg
imwrite(image_rotated,sprintf('FIG%d.jpeg',z1),'Quality',100); % will create FIG1, FIG2,...
Cast it to uint8 and Save it as PNG and it should be fine.
% Save image as PNG.
baseFileName = sprintf('FIG%d.PNG', z1);
fullFileName = fullfile(pwd, baseFileName);
fprintf('Saving image file : "%s".\n', fullFileName);
imwrite(uint8(image_rotated), fullFileName); % Will create FIG1, FIG2,...
fprintf('Success! Done saving image file.\n')
The saved/recalled image will be the same size as image_rotated.
Dont' use image as the name of a variable since that's the name of a built-in function.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by