image processing, (thresholding images to obtain image with the required gray values )
Show older comments
Hello every one,
I know how to display gray images with specific limits, for an example, if I want to diplay an image (I) with values from 0-150; I only do the following:
imshow (I, [0 150]);
However, now I am trying to save this image at 'A' , but I don't know how can I do that?
I tried the following
A= I<150 ;
but the answer was binary (i.e., black and white, zeros and ones) !! and I want to save gray image as what was obtained from (imshow(I, [0 150])) command in variable 'A'.
Answers (1)
Image Analyst
on 30 May 2022
Try this:
A150 = uint8(double(A) * 255 / 150);
imwrite(A150, 'A150.png');
This will scale A from 0 to 433.5 but then the uint8() function clips it to 255, so that what was 150 now shows up as 255.
7 Comments
Anas Saleh
on 30 May 2022
Image Analyst
on 30 May 2022
OK, it expects images in the range 0-1 so if you wanted to use that function you could use rescale or mat2gray first. But it's just easier to use the first equation I gave you.
Imadjust() doesn't require the images to be floating point, but it requires the input/output limits to be scaled [0 1].
If you really want to use imadjust() on a uint8 image, and you want to map the range [0 150] to [0 255], do
outpict = imadjust(inpict,[0 150]/255,[0 1]);
Otherwise, just use
outpict = mat2gray(inpict,[0 150]);
Anas Saleh
on 31 May 2022
If you always want to scale the max, rather than some predefined fixed value, you can get it from
% Create an image where the max is 253.
grayImage = imread('cameraman.tif');
subplot(1, 3, 1);
imshow(grayImage)
%impixelinfo;
% Find out what the actual max input gray level is.
maxInputGrayLevel = max(grayImage(:))
% Define what level we WANT to be the brightest.
clippingGraylevel = 150; % Could be maxInputGrayLevel if you want.
% Display it such that the clipping level shows up as 255.
subplot(1, 3, 2);
imshow(grayImage, [0, clippingGraylevel])
%impixelinfo;
maxGrayLevel = double(max(grayImage(:)));
% Scale the original image such that the clipping level shows up as 255.
outputImage = uint8(double(grayImage) * 255 / clippingGraylevel);
% Double check to see that the output max gray level is 255 like we expect.
maxOutputGrayLevel = max(outputImage(:))
subplot(1, 3, 3);
imshow(outputImage)
%impixelinfo;
% imwrite(outputImage, 'A150.png');
DGM
on 31 May 2022
If you're trying to automatically normalize based on the image extrema, bear in mind that using imadjust() with the implicit input range syntax will be problematic unless your image is scaled correctly for its class.
Anas Saleh
on 31 May 2022
Categories
Find more on Contrast Adjustment in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
