matrix manipulation for color spaces.
2 次查看(过去 30 天)
显示 更早的评论
Malini Bakthavatchalam
2020-5-19
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 个评论
darova
2020-5-19
Can you explain more? What size of your image? And what kind of conversion you want?
Malini Bakthavatchalam
2020-5-19
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.
采纳的回答
Walter Roberson
2020-5-19
编辑:Walter Roberson
2020-5-19
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 个评论
Malini Bakthavatchalam
2020-5-19
Thank you for the answer. Could I use this matrix for working my image on DKL color space?
Malini Bakthavatchalam
2020-5-19
This code shows me error of
Undefined function or variable 'T'.
Error in DKLtry (line 20)
transformed = M * T;
Walter Roberson
2020-5-19
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
2020-5-19
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
2020-5-19
[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
2020-5-19
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
2020-5-19
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
2020-5-20
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
2020-5-20
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
2020-5-20
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
2020-5-20
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
2020-5-20
编辑:Walter Roberson
2020-5-20
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);
Malini Bakthavatchalam
2020-5-24
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?
Malini Bakthavatchalam
2020-5-24
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
2020-5-24
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
2020-5-25
编辑:Image Analyst
2020-5-25
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
2020-5-25
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
2020-5-25
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
2020-5-25
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
2020-5-25
You would not inv() the image. You might want to
reshape(reshape(XYZ_space, [], 3) * inv(RGB2XYZtranscol), size(XYZ_space))
Malini Bakthavatchalam
2020-5-25
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
2020-5-25
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
更多回答(1 个)
vecdi
2024-6-11
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)