How to adjust the intensity of grayscale image?

26 views (last 30 days)
ali brk
ali brk on 1 Jan 2022
Commented: Image Analyst on 1 Jan 2022
I create a 200x200 image then convert it to grayscale, how can i adjust its intensity between 0-255?

Answers (3)

Simon Chan
Simon Chan on 1 Jan 2022
Use function imcontrast

Image Analyst
Image Analyst on 1 Jan 2022
There are lots of ways.
grayImage = imadjust(grayImage);
grayImage = rescale(grayImage, 0, 255);
grayImage = 255 * mat2gray(grayImage);
  2 Comments
Image Analyst
Image Analyst on 1 Jan 2022
They are not totally black. Did you look at them in the workspace? They will have non-zero values. Try displaying with [] as the second argument to imshow:
imshow(grayImage, []);
Of course with the sample code you gave, where you defined Image as a totally black image, you will get a totally black image even after it's contrast stretched. You have to use an image that actually has some structure to it and is not all black.
Don't use Image as the name of a variable since there is a built-in function image().
grayImage = imread('pout.tif');
subplot(1, 2, 1);
imshow(grayImage);
if ndims(grayImage) == 3
grayImage = rgb2gray(grayImage);
end
grayImage = imadjust(grayImage);
subplot(1, 2, 2);
imshow(grayImage, []);

Sign in to comment.


yanqi liu
yanqi liu on 1 Jan 2022
yes,sir,may be use
im = im2uint8(mat2gray(im));
  2 Comments
yanqi liu
yanqi liu on 1 Jan 2022
yes,sir,may be use
%create a 200x200 image then convert it to grayscale,
%how can i adjust its intensity between 0-255?
% create a 200x200 image
im = rand(200,200,3);
% then convert it to grayscale
im = rgb2gray(im);
im(1:10,1:10)
ans = 10×10
0.1511 0.6153 0.2279 0.2951 0.8098 0.3878 0.5535 0.4655 0.4006 0.3543 0.3550 0.3707 0.3934 0.4972 0.5180 0.6047 0.4820 0.6322 0.3626 0.5538 0.3975 0.5218 0.5015 0.2773 0.8051 0.7308 0.6066 0.6486 0.6455 0.1662 0.2567 0.5993 0.5951 0.8354 0.3259 0.0769 0.1316 0.7617 0.7265 0.0289 0.6379 0.5649 0.6424 0.3031 0.4472 0.8407 0.7261 0.4992 0.6738 0.4763 0.5342 0.4933 0.6417 0.6855 0.5028 0.4122 0.3783 0.6598 0.5657 0.5953 0.2551 0.4213 0.5610 0.4059 0.4440 0.5908 0.6452 0.5935 0.2587 0.3949 0.8427 0.4115 0.6005 0.8031 0.6722 0.7559 0.5617 0.4079 0.7655 0.4716 0.6689 0.5936 0.3186 0.6318 0.2224 0.3110 0.4287 0.5690 0.4872 0.4361 0.3908 0.4320 0.3599 0.2860 0.5236 0.3848 0.7476 0.3502 0.2442 0.3136
% adjust its intensity between 0-255
im = im2uint8(mat2gray(im));
im(1:10,1:10)
ans = 10×10
37 158 57 74 209 98 142 119 102 90 90 94 100 127 132 155 123 162 92 142 101 133 128 70 207 188 155 166 166 41 64 154 152 215 82 17 32 196 187 5 164 145 165 76 114 217 187 127 173 121 137 126 165 176 128 105 96 169 145 153 64 107 144 103 113 151 166 152 65 100 217 105 154 207 173 194 144 104 197 120 172 152 80 162 55 78 109 146 124 111 99 110 91 72 134 98 192 89 61 79

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by