필터 지우기
필터 지우기

Saving a grayscale image with scaled values

조회 수: 6 (최근 30일)
Zinoviy
Zinoviy 2022년 11월 8일
편집: Jan 2022년 11월 8일
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
Jan
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
Zinoviy
Zinoviy 2022년 11월 8일
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
Image Analyst
Image Analyst 2022년 11월 8일
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
Jan
Jan 2022년 11월 8일
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개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by