You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How mat2gray work in matlab and How can I scale image using mat2gray as the format, 0 for max value and 255 for min value?
14 views (last 30 days)
Show older comments
first of all i want to know how mat2gray works ?. i mean back end work and formula etc. then i want scale the image using it so that it follows the format, 0 for max value and 255 for min value.
Thanks
Answers (2)
Guillaume
on 8 Jul 2017
You can look at the code of mat2gray and understand how it works by typing
edit mat2gray
at the command line. You'll find that it works exactly as explained in the documentation.
As for your other question, 0 for max value and 255 for min value, unfortunately for you, matlab follows the same mathematical rules as everybody else, where 0 < 255. You will have to write your own version of matlab if you want 255 < 0 (or a different definition of min and max). Perhaps if you explained your ultimate goal we may understand better why you ask such a question.
1 Comment
Image Analyst
on 8 Jul 2017
I'm not sure what " 0 for max value and 255 for min value" means, but you can invert your image like this:
invertedGrayImage = 255 - grayImage;
What used to be the max will now show up as 0 and imshow() will show it as dark/black.
Alternatively, you could use a colormap, which doesn't change the image or create a new one, it changes the way it appears upon display
imshow(grayImage); % Scale min to max
colormap(gca, flipud(gray(256)));
mat2gray scales the min to 0 and the max to 255, no matter where they started. To create a new array where the min starts at 0 and the max gets mapped to 255, do this:
uint8Image = uint8(255 * mat2gray(grayImage));
23 Comments
Walter Roberson
on 9 Jul 2017
I prefer
uint8Image = im2uint8(1 - mat2gray(grayImage));
When you use uint8(255*X) then you only get 1/2 bucket for the value 0 and 1/2 bucket for the value 1 because of rounding.
sufian ahmed
on 9 Jul 2017
Edited: sufian ahmed
on 9 Jul 2017
@image analyst, sir i want to scale image using mat2gray.by deafault it considers 0 for min value of image and 1 for max value of image.but i just do it in inverse order. for example: it scales as: maximage value=0 and minimage value=255. how can i ?.. what modification need in mat2gary. if i want to inverse it. Thanks
Image Analyst
on 9 Jul 2017
Pass 255-grayImage into mat2gray instead of grayImage to invert the brightness.
Walter can you give some code showing where there is a difference in the output images, because I can't think of a situation off the top of my head?
% Create a uint8 image where every gray level occurs 10 times.
grayImage = uint8(repmat([0:255], [10, 1]));
uint8Image1 = uint8(255 * mat2gray(255 - grayImage));
uint8Image2 = im2uint8(1 - mat2gray(grayImage));
theyreEqual12 = isequal(uint8Image1, uint8Image2)
% Create a double image where every gray level occurs 10 times.
grayImage = repmat([0.25: 0.5 : 9.75], [10, 1]);
uint8Image3 = uint8(255 * mat2gray(255 - grayImage));
uint8Image4 = im2uint8(1 - mat2gray(grayImage));
theyreEqual34 = isequal(uint8Image3, uint8Image4)
histObj3 = histogram(uint8Image3, 0:255);
counts3 = histObj3.Values
histObj4 = histogram(uint8Image4, 0:255);
counts4 = histObj4.Values
For the above code, both your code and my code give the same number of output pixels at 0 and 255.
sufian ahmed
on 9 Jul 2017
@image analyst. this is not RGB image and it is a depth image in uint16 format. In this case, how inverted this image ? I think 255-gray ,in this case not work ? m i right ?
Walter Roberson
on 9 Jul 2017
Hmmm... I know I've seen a difference between im2uint8(x) vs uint8(255*x) in the past; I cannot seem to replicate it at the moment.
Walter Roberson
on 9 Jul 2017
sufian ahmed, my suggestion of
uint8Image = im2uint8(1 - mat2gray(grayImage));
should work for your grayImage being uint16
Image Analyst
on 9 Jul 2017
Yes that shoudl work. A handy function to know if you want to know the max possible value for the data type of your image is intmax, so
maxPossibleGrayLevel = intmax(class(yourImage));
It will return 255 or 65535 depending on if your image is uint8 or uint16, respectively.
sufian ahmed
on 10 Jul 2017
Edited: sufian ahmed
on 10 Jul 2017
@imageanlyst then it will be final code : uint8Image1 = uint8(255 * mat2gray(maxPossibleGrayLevel - grayImage));
for mapping uint16 depth image into 0-255 using inverse order ?
sufian ahmed
on 10 Jul 2017
Edited: sufian ahmed
on 10 Jul 2017
@ walter , i dont undersatnd how u r code works. please explain ? is it work for me if i want to map, 0 for max value nad 255 for min value for uint16 depth image ? for knowledge i want, how mat2gray scalize the value, which formula it uses ?
Walter Roberson
on 10 Jul 2017
Edited: Walter Roberson
on 10 Jul 2017
mat2gray = @(A) double((A-min(A(:)))./double(max(A(:))-min(A(:)))
If you really want to do everything in one step then you could do
uint8(255*double((max(A(:))-A-min(A(:)))./double(max(A(:))-min(A(:))))
Guillaume
on 10 Jul 2017
how mat2gray scalize the value
As already stated in my answer, you can look at the code of mat2gray to understand exactly how it works. It works exactly as explained in its documentation, so it's not clear what you don't understand.
sufian ahmed
on 10 Jul 2017
@walter sir, thanks... i understand ... now tell me one thing... my max depth image value is 8000 and this image is uint16 that means 0 to 65530 scale range. now if i map this depth image value 0 to 255 considering 0 for maxVal and 255 for minval. how can i modify it ?
Walter Roberson
on 10 Jul 2017
In the situation you describe, the code I posted would map 8000 to 0, and would map 0 to 255, and would map everything else linearly between those. In other words, the code already examines the data that is present in the image and maps the largest data value in the image to 0 and maps the smallest data value (probably 0) to 255.
If you want 65535 (largest possible uint16 value rather than largest value in your image) mapped to 0, and 0 mapped to 255, then you could use
uint8Image = uint8(255 * mat2gray(grayImage,[0 65535]));
Raka Mukherjee
on 27 Jan 2020
I have an image of 7731x7561 uint16 dimension. The highest value is 7699 and the lowest value is 0. How can I map the value between 0-255 for processing the image further? I tried with the im2uint8 function but that does not give unique values as only 4 to 5 values cover up the entire range due to loss of information. Is there a way to get 255 unique values where 0 shall be the lowest value and 7699 shall be the highest ,i.e. 255? I want to play with the colour of the images, as I have 3 bands of R, G and B of the same dimension.
Walter Roberson
on 27 Jan 2020
mat2gray() to scale it to the range 0 to 1, then im2uint8() to scale that to 0 to 255.
Is there a way to get 255 unique values
You told us that there are only 4 to 5 unique values in the input. How should the 4 to 5 unique values be split into 255 unique values?
Image Analyst
on 27 Jan 2020
You can do
uint8Image = uint8(255 * mat2gray(yourImage));
but why? What's wrong with leaving it as uint16, and leaving it in the original range with original values? But like Walter said, if you have only 4 or 5 values before scaling, you'll still have 4 or 5 values after scaling, not 255.
Raka Mukherjee
on 28 Jan 2020
Thank you so much for your answers. No I said about the 4 to 5 values after perfoming the im2uint8 function.
Walter Roberson
on 28 Jan 2020
I thought that I had suggested rgb2ind on the original followed by ind2rgb with a different colormap.
Your values peak at just below 8192 which is 2^13. If you have a bunch of different values that are within 32 of each other and so would become the same when the image is scaled to 256 by division by 32, then it is not obvious how you would represent them as different 8 bit values -- unless, that is, you are willing to let go of the correct relative scaling of values?
Raka Mukherjee
on 28 Jan 2020
Sorry I did'nt get you. On what basis can we say that a bunch of values are within 32 of each other?Why should we divide it with 32?
Guillaume
on 28 Jan 2020
@Raka, Can you start your own question? It would be a much better place to help you than the comments of an answer to a completely unrelated question.
Image Analyst
on 28 Jan 2020
Yes, I agree with Guillaume. And attach your image file and answer my question above about why you can't just work with the original values.
Raka Mukherjee
on 29 Jan 2020
Yes sure. I am starting my own question and attaching the images as well. I am quite new to MATLAB and more new to work with the colour component of images so I am not quite sure whether we can work with a 16 bit data to find out particular colour (say,gray) from images as it is easier to work with 8 bit data. The images are Landsat Red, Green and Blue bands which have a depth of unsigned 16 bit.
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)