Create a Gray level Mask using Matlab and write the same as a gray level image.

13 views (last 30 days)
sanjeev a
sanjeev a on 3 Nov 2017
Answered: Guillaume on 3 Nov 2017
I am producing a gray level pattern to be loaded on my SLM (Spatial Light Modulator). The pattern is 1920x1080 pixels. I have 255 gray level values. I tried out this code to create a gray level mask . when I open in Matlab I could see it as gray level, but when I write the image as a bmp file and then it becomes a binary file. How can I resolve this. Following is my code.
clear all
close all
% imshow(mask,[])
for k=1:500
for i=1:1080
% mask3=Fit_GrayLevel_To_SLM_Vector(mask);

Answers (2)

Chenchal on 3 Nov 2017
Edited: Chenchal on 3 Nov 2017
When writing the bmp use uint8 values
% code

Guillaume on 3 Nov 2017
Basically, the class of the image is crucial to matlab when saving to file (and displaying images). Matlab assumes that the intensity range of a double image is floating point values in the range [0 1], even though a double array can store values outside this range. Anything above 1 is considered to be 1 (= white) anything below 0 is considered to be 0 (= black). By necessity, an image of class uint8 has integer intensity range [0 255] (since that's the range of values that can be store as 8-bit integer).
So, if you store images as double but with range [0 255] strange things will happen. As Chenchal has shown, you can fix that by converting your image to uint8 when you save. Another option would be to create it directly as uint8:
mask = zeros(1080, 1920, 'uint8');
That would also simplify the call to imshow to
since your image would now have the correct displayrange assumed by imshow for its class.

Community Treasure Hunt

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

Start Hunting!

Translated by