How to visualise the colour with a given set of RGB values?
19 次查看(过去 30 天)
显示 更早的评论
采纳的回答
更多回答(1 个)
DGM
2021-11-3
编辑:DGM
2021-11-3
If you just want a small swatch to see the color:
c = [85 250 100]; % color tuple scaled to [0 255]
imshow(ones(80).*permute(c./255,[1 3 2]));
This should work fine in R2016b and newer.
2 个评论
Navaneeth
2024-3-7
Hellow thank you for this. But I am getting an error implementing it in my code, can you kindly let me know how can I integrate it without getting this error.
clc;clear;close all;
%XYZ space to RGB space
XYZ = [1073;796;697];%XYZ value
x = XYZ(1)/sum(XYZ);
y = XYZ(2)/sum(XYZ);
z = XYZ(3)/sum(XYZ);
xyz = [x y z];
RGB_2 = xyz2rgb(xyz,'OutputType','uint8');
imshow(ones(80).*permute(RGB_2
./255,[1 3 2]));
Thank you in advance.
DGM
2024-3-7
There are a couple reasons we need to pay attention to class and scale here. First is that there are restrictions on certain operations with integer class arrays and with mixed-class arrays. Second is that imshow() and other tools expect images to be within a certain scale according to their numeric class.
The problem is that your RGB tuple is proper uint8, whereas the literal c = [85 250 100] that I used in my example is an improperly-scaled double. This causes an error, since we can't do mixed-class multiplication between a uint8 array and a double array. We can get around that problem various ways, but the second problem is that floating point image data is expected to be unit-scale (0-1). When I was dealing with an improperly-scaled double as input, it's necessary to normalize it properly by dividing by 255. If the input is an actual proper uint8 array, doing this normalization is unnecessary and will destroy the image.
So let's back up. The example I gave was based around the common habit of expressing color tuples as improper uint8-scale double arrays. If we're using proper uint8, we don't need to fix the scaling. The multiplication really isn't necessary unless you want to do in-figure composition with some other objects and you want the image to be a certain size. There are simpler ways to do it.
XYZ = [1073;796;697];%XYZ value
x = XYZ(1)/sum(XYZ);
y = XYZ(2)/sum(XYZ);
z = XYZ(3)/sum(XYZ);
xyz = [x y z];
RGB_2 = xyz2rgb(xyz,'OutputType','uint8');
% expand by multiplication, but make sure both have the same class
imshow(ones(80,class(RGB_2)).*permute(RGB_2,[1 3 2]));
% expand using repmat() instead (probably faster)
imshow(repmat(permute(RGB_2,[1 3 2]),[80 80 1]));
% don't really need to do either
% the image fills the axes even if it's only a single pixel
imshow(permute(RGB_2,[1 3 2]))
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!