1 function Luv_back = Rgb_to_luv (Im_rgb, Type)
\r
2 % takes an image in Rgb coords and returns either a vector or an image
\r
3 % in Luv coords, depending on Type (Image or vector).
\r
5 Vector = shiftdim(Im_rgb, 2);
\r
6 Vector = double(reshape(Vector, [3, size(Im_rgb, 1)*size(Im_rgb, 2)]));
\r
7 Vector_nz = 4096*(sum(Vector, 1) > 0);
\r
8 Vector_z = not(Vector_nz);
\r
9 Vector = max(Vector, [Vector_z; Vector_z; Vector_z]); % to avoid all zero pixels
\r
12 0.490 0.310 0.200; ...
\r
13 0.177 0.812 0.011; ...
\r
16 XYZ = Matrix*Vector; % now get a vector in XYZ coords
\r
18 Y0 = 1; % is it true?
\r
22 u0 = 4*X0/(X0+15*Y0+3*Z0);
\r
23 v0 = 9*Y0/(X0+15*Y0+3*Z0);
\r
25 Luv_back = zeros(size(Vector));
\r
26 Luv_back(1, :) = 25*(100*XYZ(2, :)/Y0).^(0.333)-16;
\r
27 Zeros = zeros(size(Luv_back(1, :)));
\r
28 Luv_back(1, :) = max(Luv_back(1, :), Zeros);
\r
29 u = 4*XYZ(1, :) ./ (XYZ(1, :)+15*XYZ(2, :)+3*XYZ(3, :));
\r
30 v = 9*XYZ(2, :) ./ (XYZ(1, :)+15*XYZ(2, :)+3*XYZ(3, :));
\r
31 Luv_back(2, :) = 13*Luv_back(1, :).*(u-u0);
\r
32 Luv_back(2, :) = max(Luv_back(2, :), Zeros);
\r
33 Luv_back(3, :) = 13*Luv_back(1, :).*(v-v0);
\r
34 Luv_back(3, :) = max(Luv_back(3, :), Zeros);
\r
35 Luv_back = min(Luv_back, [Vector_nz; Vector_nz; Vector_nz]); % zeroing out
\r
36 Luv_back = Luv_back/(718.3176/256);
\r
37 if strcmp(Type, 'Image')
\r
38 Luv_back = reshape(Luv_back, [3, size(Im_rgb, 1), size(Im_rgb, 2)]);
\r
39 Luv_back = shiftdim(Luv_back, 1);
\r