matrix manipulation for color spaces.

Hi , I have an image . I want to convert that to [3 3] matrix value to play with color space. I understand imead will convert image to matrix form but if I want 3 3 matrix, How should I proceed?

2 Comments

Can you explain more? What size of your image? And what kind of conversion you want?
tVersion: ''
Width: 480
Height: 502
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: ''
NumberOfSamples: 3
This is my image size. The image is just a face of a girl. So I want to convert this image in DKL space and work on illusion project.

Sign in to comment.

 Accepted Answer

You probably do not want a 3 x 3 images. What you probably want is to let T be a 3 x 3 transformation matrix, and RGB be your RGB image, then
M = reshape(RGB, [], 3);
transformed = M * T;
nonRGB = reshape(transformed, size(RGB));

26 Comments

Thank you for the answer. Could I use this matrix for working my image on DKL color space?
This code shows me error of
Undefined function or variable 'T'.
Error in DKLtry (line 20)
transformed = M * T;
As I wrote, "let T be a 3 x 3 transformation matrix"
Could I use this matrix for working my image on DKL color space?
Looking at https://github.com/nblauch/dkl_conversion the answer would appear to be NO, that the conversion to DKL is non-linear.
Is there a way to work on LMS space ... Because i heard from my colleague DKL is a variant of LMS space? Do You have any opinion about it ?
[L; M; S] = [0.3897 0.6890 -0.0787; 0.3897 0.6890 -0.0787; 0.0000 0.0000 1.0000] [x; Y; Z], this is the matrix how can i apply to the image ... https://www.cs.tau.ac.il/~turkel/imagepapers/ColorTransfer.pdf. I read color transfers from this paper but I am confused how do i use it to tranform to images ?
Search for
LMS2DKL<-function(bg,diffcone.coords,DKL2LMS=FALSE){
If I read it correctly, this was a conversion from MATLAB source. It looks to me as if that might be related to https://github.com/Psychtoolbox-3/Psychtoolbox-3/blob/master/Psychtoolbox/PsychDemos/DKLDemo.m (authors seem to match)
xYZ2LMS_transform = [0.3897 0.6890 -0.0787; 0.3897 0.6890 -0.0787; 0.0000 0.0000 1.0000];
M = reshape(xYZ, [], 3);
transformed = M * xYZ_LMS_transform;
LMS = reshape(transformed, size(xYZ));
L = LMS(:,:,1);
M = LMS(:,:,2);
S = LMS(:,:,3);
It still shows error Undefined function or variable 'xYZ_LMS_transform'.
Error in DKLtry (line 29)
transformed = M * xYZ_LMS_transform;
My code was
RGB = imread('file.jpg');
XYZ = rgb2xyz(RGB);
rgb2xyz([1 1 1])
XYZ_D50 = rgb2xyz(RGB,'WhitePoint','d50');
%Display the first output XYZ image alongside the XYZ image with D50 as reference white.
figure
imshowpair(XYZ,XYZ_D50,'montage');
title('XYZ Image, Without (Left) and With (Right) Reference White');
% xyz = rgb2xyz(RGB);
xYZ2LMS_transform = [0.3897 0.6890 -0.0787;
0.3897 0.6890 -0.0787;
0.0000 0.0000 1.0000];
M = reshape(XYZ, [], 3);
transformed = M * xYZ_LMS_transform;
LMS = reshape(transformed, size(XYZ));
L = LMS(:,:,1);
M = LMS(:,:,2);
S = LMS(:,:,3);
imshowpair(XYZ,M,LMS,L,M,S,'montage')
RGB = imread('file.jpg');
XYZ = rgb2xyz(RGB);
rgb2xyz([1 1 1])
XYZ_D50 = rgb2xyz(RGB,'WhitePoint','d50');
%Display the first output XYZ image alongside the XYZ image with D50 as reference white.
figure
imshowpair(XYZ,XYZ_D50,'montage');
title('XYZ Image, Without (Left) and With (Right) Reference White');
% xyz = rgb2xyz(RGB);
xYZ2LMS_transform = [0.3897 0.6890 -0.0787;
0.3897 0.6890 -0.0787;
0.0000 0.0000 1.0000];
M = reshape(XYZ, [], 3);
transformed = M * xYZ2LMS_transform;
LMS = reshape(transformed, size(XYZ));
L = LMS(:,:,1);
M = LMS(:,:,2);
S = LMS(:,:,3);
imshowpair(XYZ,M,LMS,L,M,S,'montage')
It is still showing me error
Error using imfuse>parse_inputs (line 442)
The value of 'method' is invalid. Expected METHOD to match one of these values:
'falsecolor', 'diff', 'blend', 'montage', 'checkerboard'
The input did not match any of the valid values.
Error in imfuse (line 118)
[A,B,RA,RB,method,options] = parse_inputs(varargin{:});
Error in imshowpair (line 107)
[result, R_ref] = imfuse(varargin{:});
Error in DKLtry (line 35)
imshowpair(XYZ,M,LMS,L,M,S,'montage')
Caused by:
Error using validatestring>checkString (line 89)
Expected input to be one of these types:
char, string
Instead its type was double.
RGB = imread('file.jpg');
XYZ = rgb2xyz(RGB);
rgb2xyz([1 1 1])
XYZ_D50 = rgb2xyz(RGB,'WhitePoint','d50');
%Display the first output XYZ image alongside the XYZ image with D50 as reference white.
figure
montage({XYZ, XYZ_D50});
title('XYZ Image, Without (Left) and With (Right) Reference White');
% xyz = rgb2xyz(RGB);
xYZ2LMS_transform = [0.3897 0.6890 -0.0787;
0.3897 0.6890 -0.0787;
0.0000 0.0000 1.0000];
M = reshape(XYZ, [], 3);
transformed = M * xYZ2LMS_transform;
LMS = reshape(transformed, size(XYZ));
L = LMS(:,:,1);
M = LMS(:,:,2);
S = LMS(:,:,3);
montage( {XYZ, LMS, L, M, S} )
But remember that XYZ, LMS, L, M, S are not RGB images, so displaying them might not give an accurate idea of what they represent.
Thanks for the help. If I change the image to char and divide the imread by 255 would that be better ...
for ii = 1 : length(picName)
MyImrgb = double(imread([pathName picName{ii}]))/255;
MyImrgb = MyImrgb.^2.2;
[x, y, z] = size(MyImrgb);
% Define a DKL2RGB mat based on the classic calibration technic
% this P matrix defines DKL in RGB color space
% ld % rg %yv
ldrgyv2rgbMat = [1,1,0.236748566577269;
1,-0.299338211934457,-0.235643322285071;
1,0.0137437185685517,1]; % B
MyImrgbCol = reshape(MyImrgb, [x*y, z]);
% define each RGB pixel in DKL (inverse matrix Q or P with \)
MyImldrgyvCol = ldrgyv2rgbMat\(MyImrgbCol'*2 - 1);
I suggest you use im2double() instead of double()/255
Thanks for the suggestion. I have a basic doubt, is it ok to use the color conversion available in matlab for some function and matrix trasnformation for rest of the code. for example. In my code, I have used rgb2xyz matlab function, but when i transformed from the paper i used the matrix. is the logic correct?
That sounds okay.
I hve one more basic question as well. How do i convert an image to 3 3 matrix ?
so when I tried this
RGB = imread('file.jpg');
M = reshape(RGB, [],3);
RGB2XYZtranscol = [0.5141 0.3239 0.1604;
0.2651 0.6702 0.0641;
0.0241 0.1228 0.8444]
XYZ_space = RGB.* RGB2XYZtranscol;
Imshowpairwise(RGB,XYZ_space,'montage')
I got this error message.
Error using .*
Integers can only be combined with integers of the same class, or scalar doubles.
Error in dkltry2 (line 21)
XYZ_space = RGB.* RGB2XYZtranscol;
How do i convert an image to 3 3 matrix ?
You do not convert an image to a 3 x 3 matrix.
M = reshape(im2double(RGB), [],3);
XYZ_space = reshape(M * RGB2XYZtranscol, size(RGB));
What if I use a code im2double(RGBimage)/255? Some of my colleagues suggested this to me, but I did not understand why to divide by 255. What is the explanation for this?
You may or may not have to. Check the function. Some, but not all, image processing functions expect floating point images to be in the range of 0-1.
Now i changed the color space to XYZ, but to work on the program i have to convert to RGB to project the image in the screen ... so I used reshape(inv(XYZ_space), [], 3), if I apply it, it shows me error inv works only for 2D.. what is the error in my code .. ?
No, im2double() automatically rescales to the range 0 to 1. You would not want to further divide by 255.
double()/255 could be used for something known to be uint8 and definitely never going to be anything other than uint8, but it is safer in the long run to use im2double() as that will automatically do whatever scaling is needed according to the datatype of the image. So if you happened to read in a uint16 image where you thought you were reading in a uint8 image, then im2double() would automatically adjust.
You would not inv() the image. You might want to
reshape(reshape(XYZ_space, [], 3) * inv(RGB2XYZtranscol), size(XYZ_space))
Yes, thank you I got your point, so I wrote my final code for converting into DKL color space back into RGB..
clear all
close all
clc
RGB = imread('file.jpg')
MyImrgb = reshape(im2double(RGB), [],3);
Imrgb = MyImrgb.^2.2; %gamma correction
[x, y, z] = size(Imrgb);
ldrgyv2rgbMat = [1,1,0.236748566577269;
1,-0.299338211934457,-0.235643322285071;
1,0.0137437185685517,1]; % B (mat based on the classic calibration technic)
MyImrgbCol = reshape(Imrgb, [x*y, z]);
MyImldrgyvCol = ldrgyv2rgbMat\(MyImrgbCol'*2 - 1);
figure(1), imshow(RGB)
figure(2), imshow(MyImldrgyvCol)
But i get error
Error using \
Matrix dimensions must agree.
Error in dkltry2 (line 12)
MyImldrgyvCol = ldrgyv2rgbMat\(MyImrgbCol'*2 - 1);
Also, I have one more doubt, I used an color thresholder to remove my background. and I used that image in the color space transformation. but now when I use my complete code for the project, it is still calculating my histogram with background so how can i solve the issue.. I am attaching my complete code here

Sign in to comment.

More Answers (1)

vecdi
vecdi on 11 Jun 2024
RGB = imread('file.jpg')
MyImrgb = reshape(im2double(RGB), [],3);
Imrgb = MyImrgb.^2.2; %gamma correction
[x, y, z] = size(Imrgb);
ldrgyv2rgbMat = [1,1,0.236748566577269;
1,-0.299338211934457,-0.235643322285071;
1,0.0137437185685517,1]; % B (mat based on the classic calibration technic)
MyImrgbCol = reshape(Imrgb, [x*y, z]);
MyImldrgyvCol = ldrgyv2rgbMat\(MyImrgbCol'*2 - 1);
figure(1), imshow(RGB)
figure(2), imshow(MyImldrgyvCol)
But i get error
Error using \
Matrix dimensions must agree.
Error in dkltry2 (line 12)
MyImldrgyvCol = ldrgyv2rgbMat\(MyImrgbCol'*2 - 1);

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!