user316354
user316354

Reputation: 1

How to create an image using a matrix?

I want to know what color RGB[228, 198, 208] is, so I wrote this function:

function showColor()
im = ones(500, 500, 3);
color = ones(500, 500);
R = color * 228;
G = color * 198;
B = color * 208;
im(:, :, 1) = R;
im(:, :, 2) = G;
im(:, :, 3) = B;
imshow(im);

The result is white and it doesn't seem right.

Then I tried this:

function showColor2()
im = imread('pic.jpg'); %It's a 2448*3264 picture
color = ones(2448, 3264);
R = color * 228;
G = color * 198;
B = color * 208;
im(:, :, 1) = R;
im(:, :, 2) = G;
im(:, :, 3) = B;
imshow(im);

This function shows the right color but it looks just like the first one (except image size).

So my question is:

  1. Is there any difference between the matrix we create and the one we get from imread()?

    Why the second function works well?

  2. Can we create an image just by writing a matrix?

Upvotes: 0

Views: 135

Answers (1)

nimrodm
nimrodm

Reputation: 23779

Try

im = ones(500, 500, 3,'uint8');
color = ones(500, 500,'uint8');
R = color * 228;
G = color * 198;
B = color * 208;
im(:, :, 1) = R;
im(:, :, 2) = G;
im(:, :, 3) = B;
imshow(im);

Matlab supports two different image formats. One is based on double arrays where the values are in the range 0 to 1.0 (This is the default type created by ones. Try typing class(ones(500,500))). The other is more efficient and is based on 8 bit per dimension. These arrays are created by ones(N,M,'uint8').

To use the double image format use your original code but make sure the values are in the range 0 to 1.0. So, in your case:

im = ones(500, 500, 3);
color = ones(500, 500);
R = color * 228/256;
G = color * 198/256;
B = color * 208/256;
im(:, :, 1) = R;
im(:, :, 2) = G;
im(:, :, 3) = B;
imshow(im);

Upvotes: 4

Related Questions