Converting a image matrix from RGB to XYZ color space

13 Ansichten (letzte 30 Tage)
Youngsam
Youngsam am 10 Jan. 2012
Beantwortet: Image Analyst am 3 Apr. 2025
Hi,
I would like to convert RGB data in 24x3 to xyz, using makecform and applycform. But, I am getting the number out of range - not scaled right.
function [xyz2D]=convertsrgb2xyz(srgb)
format3D = reshape(srgb,24,1,3);
C=makecform('srgb2xyz');
xyz=applycform(format3D,C);
xyz2D=reshape(xyz,24,3);
I am not sure what I am doing wrong here. Could you help me?
Thank you to whomever!
Sam
  1 Kommentar
Andrew Newell
Andrew Newell am 10 Jan. 2012
Bearbeitet: Image Analyst am 3 Apr. 2025
I don't get any errors when I try
srgb=rand(24,3);
xyz2D=convertsrgb2xyz(srgb);

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 3 Apr. 2025
% In range 0-1
srgb=rand(24,3);
[xyz2D]=convertsrgb2xyz(srgb)
xyz2D = 24×3
0.2166 0.3875 0.1050 0.1330 0.1169 0.2647 0.2169 0.2089 0.5100 0.0206 0.0132 0.0559 0.3766 0.2759 0.0429 0.3256 0.4499 0.1962 0.2665 0.1453 0.0219 0.3679 0.2249 0.2715 0.2920 0.3793 0.0530 0.3256 0.5459 0.0745
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% uint8 variables in range 0-255
srgbUint8 = uint8(255 * rand(24, 3));
[xyz2D8]=convertsrgb2xyz(srgbUint8)
xyz2D8 = 24×3
16236 21925 9541 14108 18405 10245 3491 2639 12682 3828 2515 4063 15859 17125 5405 17960 16536 14349 6258 7821 8273 19041 17838 22050 1251 685 1280 14814 13690 3156
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Your function
function [xyz2D]=convertsrgb2xyz(srgb)
format3D = reshape(srgb,24,1,3);
C=makecform('srgb2xyz');
xyz=applycform(format3D,C);
xyz2D=reshape(xyz,24,3);
end
There doesn't seem to be any error thrown but the values are not correct for uint8-ranged values.

nick
nick am 3 Apr. 2025
Hi Youngsam,
Kindly share the data used as input for the function to help debug the issue. I didn't get any errors while using the function 'convertssrgb2xyz':
srgb=rand(24,3);
xyz2D=convertsrgb2xyz(srgb);
Please ensure that the input RGB values are correctly scaled and formatted. The 'makecform('srgb2xyz')' expects the RGB values to be in the range of [0, 1]. A possible cause for out of scale error could be RGB data is in the range [0, 255], and hence need normalization before applying the color transformation.
You can refer to the documentation of 'makecform' to know more about it by executing the following commmand in MATLAB Command window:
doc makecform

Community Treasure Hunt

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

Start Hunting!

Translated by