You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
matrix manipulation for color spaces.
2 views (last 30 days)
Show older comments
Malini Bakthavatchalam
on 19 May 2020
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
Malini Bakthavatchalam
on 19 May 2020
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.
Accepted Answer
Walter Roberson
on 19 May 2020
Edited: Walter Roberson
on 19 May 2020
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
Malini Bakthavatchalam
on 19 May 2020
Thank you for the answer. Could I use this matrix for working my image on DKL color space?
Malini Bakthavatchalam
on 19 May 2020
This code shows me error of
Undefined function or variable 'T'.
Error in DKLtry (line 20)
transformed = M * T;
Walter Roberson
on 19 May 2020
As I wrote, "let T be a 3 x 3 transformation matrix"
Walter Roberson
on 19 May 2020
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.
Malini Bakthavatchalam
on 19 May 2020
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 ?
Malini Bakthavatchalam
on 19 May 2020
[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 ?
Walter Roberson
on 19 May 2020
I don't think it is a linear transformation from LMS.
Walter Roberson
on 19 May 2020
Search for
LMS2DKL<-function(bg,diffcone.coords,DKL2LMS=FALSE){
which is currently line 229 at https://github.com/cran/colorscience/blob/master/R/colorscience.R for R code.
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)
Walter Roberson
on 19 May 2020
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);
Malini Bakthavatchalam
on 20 May 2020
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')
Walter Roberson
on 20 May 2020
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')
Malini Bakthavatchalam
on 20 May 2020
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.
Walter Roberson
on 20 May 2020
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.
Malini Bakthavatchalam
on 20 May 2020
Edited: Walter Roberson
on 20 May 2020
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);
Walter Roberson
on 20 May 2020
I suggest you use im2double() instead of double()/255
Malini Bakthavatchalam
on 24 May 2020
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?
Walter Roberson
on 24 May 2020
That sounds okay.
Malini Bakthavatchalam
on 24 May 2020
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;
Walter Roberson
on 24 May 2020
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));
Malini Bakthavatchalam
on 25 May 2020
Edited: Image Analyst
on 25 May 2020
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?
Image Analyst
on 25 May 2020
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.
Malini Bakthavatchalam
on 25 May 2020
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 .. ?
Walter Roberson
on 25 May 2020
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.
Walter Roberson
on 25 May 2020
You would not inv() the image. You might want to
reshape(reshape(XYZ_space, [], 3) * inv(RGB2XYZtranscol), size(XYZ_space))
Malini Bakthavatchalam
on 25 May 2020
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);
Malini Bakthavatchalam
on 25 May 2020
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
More Answers (1)
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);
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)