image processing, (thresholding images to obtain image with the required gray values )

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)

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.
You can also use imadjust.

7 Comments

What I want is to determin the limts of a gray scale image, and I used imadjust (I=imadjust(A, [0 150]))and the following error was apeared!
I hope my point was clear, what I am looking for is to save the resulting image from the command imshow( I, [low high]) at variable A.
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]);
I know that, it works fine in this range but sometimes I need to change the ranges such as 100 to 140 ...etc.
Many thanks, I will try both of them and if there is no progress I will let you know.
Thanks a lot for fast responce.
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(:))
maxInputGrayLevel = uint8 253
% 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(:))
maxOutputGrayLevel = uint8 255
subplot(1, 3, 3);
imshow(outputImage)
%impixelinfo;
% imwrite(outputImage, 'A150.png');
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.
Thank you all,
I will try to use these methods, and if I face any problems I will tell you.
Many thanks

Sign in to comment.

Asked:

on 30 May 2022

Commented:

on 31 May 2022

Community Treasure Hunt

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

Start Hunting!