Saving a grayscale image with scaled values

Hello, here is my problem:
I have a 196*196 image, and such matrix looks like that for example:
using imshow("img",[]) displays the image correctly because it's scaled, while not using it produces something like that:
I want to save the correct output that should looke like this:
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 个评论

Pls attach the image/data, so that we can try on it.
"while not using it produces something like that:" - Please avoid to explain, what you do not do, but mention, what you do to get this image.
Did you try to scale the data by your own?
img = rand(1, 10)
img = 1×10
0.4341 0.8742 0.5018 0.5761 0.0310 0.1867 0.4272 0.2315 0.1484 0.7090
tmp = min(img(:));
img = (img - tmp) / (max(img(:)) - tmp)
img = 1×10
0.4780 1.0000 0.5584 0.6464 0 0.1847 0.4699 0.2377 0.1393 0.8041
Thanks for the replies. I'm including here one of the images as a mat file as @KALYAN ACHARJYA suggested.
I should clarify what I wrote: using imshow(img,[]) produces the second image.
I haven't tried scaling it on my own, but I can try doing what @Jan wrote but I'm note sure how to save it correctly still.
This is the loop I was using to save all the images as png's (but I'm not even sure if that's the right format)
destinationFolder = "directory"
for i=1:100
name=strcat("rep",string(i),".png")
fullName = string(strcat(destinationFolder,name)); % replace '.bmp' with the format you want
imwrite(fisp1(:,:,1,i),fullName)
end
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
Incredible, it worked.
Can you refer me to the explanation of this manual scaling?
Thanks!

请先登录,再进行评论。

 采纳的回答

Jan
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 个评论

Instead of
tmp = min(img(:));
img = (img - tmp) / (max(img(:)) - tmp);
you can use the built in mat2gray
img = mat2gray(tmp);
or use rescale or normalize
Image Analyst's suggestions are smart:
img = rand(1,8)
img = 1×8
0.2261 0.6774 0.9945 0.3414 0.2208 0.1807 0.9315 0.6300
img2 = normalize(img, 'range')
img2 = 1×8
0.0558 0.6103 1.0000 0.1975 0.0492 0 0.9227 0.5521
% Or:
img2 = rescale(img)
img2 = 1×8
0.0558 0.6103 1.0000 0.1975 0.0492 0 0.9227 0.5521
% Or:
img2 = mat2gray(img)
img2 = 1×8
0.0558 0.6103 1.0000 0.1975 0.0492 0 0.9227 0.5521
% Or less nice:
tmp = min(img(:));
img = (img - tmp) / (max(img(:)) - tmp)
img = 1×8
0.0558 0.6103 1.0000 0.1975 0.0492 0 0.9227 0.5521

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心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!

Translated by