Saving a grayscale image with scaled values
4 ビュー (過去 30 日間)
古いコメントを表示
Hello, here is my problem:
I have a 196*196 image, and such matrix looks like that for example:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1185653/image.png)
using imshow("img",[]) displays the image correctly because it's scaled, while not using it produces something like that:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1185658/image.png)
I want to save the correct output that should looke like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1185663/image.png)
But I can't find the correct parameters for imwrite...
Also I have to say that Im running it with a loop, and planning later on to do image processing, with batch processor and registration and etc.
6 件のコメント
Jan
2022 年 11 月 8 日
folder = "directory"
for i = 1:100
% Scale the image:
img = fisp1(:,:,1,i);
tmp = min(img(:));
img = (img - tmp) / (max(img(:)) - tmp);
% Save the file:
name = fullfile(folder, sprintf('rep%d.png', i));
imwrite(img, name);
end
採用された回答
Jan
2022 年 11 月 8 日
編集済み: Jan
2022 年 11 月 8 日
folder = "directory"
for i = 1:100
% Scale the image:
img = fisp1(:,:,1,i);
tmp = min(img(:));
img = (img - tmp) / (max(img(:)) - tmp);
% Nicer scaling (Thanks Image Analyst):
% normalize(img, 'range'), or: rescale(img), or: mat2gray(img)
% Save the file:
name = fullfile(folder, sprintf('rep%d.png', i));
imwrite(img, name);
end
If the original values of the image have the range [a, b], so a is the minimal value and b maximal one.
Then img-a has range [0, b-a]. If you divide this by b-a, you get the range [0, 1]. This scales the interval [a,b] to [0,1] to get the maximum contrast.
2 件のコメント
Image Analyst
2022 年 11 月 8 日
Jan
2022 年 11 月 8 日
Image Analyst's suggestions are smart:
img = rand(1,8)
img2 = normalize(img, 'range')
% Or:
img2 = rescale(img)
% Or:
img2 = mat2gray(img)
% Or less nice:
tmp = min(img(:));
img = (img - tmp) / (max(img(:)) - tmp)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Introduction to Installation and Licensing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!