decreasing number of gray levels in a image

14 views (last 30 days)
hi all, can any one tell me how to decrease the no of gray levels in a image.i have to decrease in the powers of 2. regards k.v.swamy.

Answers (1)

Image Analyst
Image Analyst on 30 Nov 2012
Perhaps I don't understand what you want, but why can't you simply divide the image by 2, 4, 8, or whatever power of 2 you want? For examples:
reducedGrayLevelsImage = originalGrayImage / 8;
reducedGrayLevelsImage = originalGrayImage / 16;
reducedGrayLevelsImage = originalGrayImage / 4;
Is that all you're looking for? Or is it more complicated than that? If so, explain the nuances that I've overlooked.
  2 Comments
knight
knight on 21 Sep 2022
How do we reduce graylevel when the desired number of gray levels does not have to be a power of 2?
Walter Roberson
Walter Roberson on 21 Sep 2022
There are two different possible interpretations here.
If you want to reduce your image from 256 levels to N levels, and you want those N levels to be uint8 0, 1, 2, .. N-1 then
reducedImage = uint8(rescale(double(YourImage), [0 N-1], 'InputMin', 0, 'InputMax', 255));
or
thresh = multithresh(YourImage, N);
reducedImage = imquantize(YourImage, thresh);
or
thresh = linspace(0, 255, N);
reducedImage = imquantize(YourImage, thresh); %might need adjustment
But sometimes you want to keep the range of values but have fewer distinct values.
thresh = multithresh(YourImage, N);
reducedImage = imquantize(YourImage, thresh, [0 thresh(2:end) 255]);
or
thresh = linspace(0, 255, N);
reducedImage = imquantize(YourImage, thresh, [0 thresh(2:end) 255]); %might need adjustment
The versions that use multithresh dynamically calculate grayscale levels, possibly unevenly -- for example if you had three coins with different grayscale levels then it could pick them out.
The versions that use linspace calculate grayscale levels evenly.
The version that uses rescale() calculates grayscale levels evenly.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!