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

5 ビュー (過去 30 日間)
sanjeev a
sanjeev a 2017 年 11 月 3 日
回答済み: Guillaume 2017 年 11 月 3 日
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
mask=zeros(1080,1920);
% imshow(mask,[])
for k=1:500
for i=1:1080
mask(i,k)=randperm(256,1);
end
end
% mask3=Fit_GrayLevel_To_SLM_Vector(mask);
imshow(mask,[])
imwrite(mask,'mymask4.bmp')

回答 (2 件)

Chenchal
Chenchal 2017 年 11 月 3 日
編集済み: Chenchal 2017 年 11 月 3 日
When writing the bmp use uint8 values
% code
imwrite(uint8(mask),'mymask4.bmp')
imshow(imread('mymask4.bmp'),[]);

Guillaume
Guillaume 2017 年 11 月 3 日
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
imshow(mask);
since your image would now have the correct displayrange assumed by imshow for its class.

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by