简体   繁体   English

如何使用矩阵创建图像?

[英]How to create an image using a matrix?

I want to know what color RGB[228, 198, 208] is, so I wrote this function: 我想知道RGB [228,198,208]是什么颜色,所以我写了这个函数:

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() ? 我们创建的矩阵与从imread()获得的矩阵之间有什么区别吗?

    Why the second function works well? 为什么第二个功能运行良好?

  2. Can we create an image just by writing a matrix? 我们可以仅通过编写矩阵来创建图像吗?

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. Matlab支持两种不同的图像格式。 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)) ). 一种是基于double阵列,其中的值是在0到1.0的范围(这是由创建的默认类型ones 。请尝试输入class(ones(500,500)) The other is more efficient and is based on 8 bit per dimension. 另一个效率更高,并且基于每个维度8位。 These arrays are created by ones(N,M,'uint8') . 这些数组由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. 要使用double图像格式,请使用原始代码,但请确保该值在0到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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM