There is a system of 3 linear equations composing of matrices which are represented by RGB image. Say
A = A1*x1 + A2*x2 + A3*x3 ......(Eq 1)
B= A1*x4 + A2*x5 + A3*x6 ........(Eq 2)
C= A1*x7 + A2*x8 + A3*x9 ........(Eq 3)
each are of equal dimension say 3D. I performed the following
A11=rgb2gray(A1);
x11=rgb2gray(x1);
A11 =double(A1) ; x11 = double(x11); b = A1*x1;
opts.UT = true; opts.TRANSA = false;
y1 = linsolve(x1,b,opts);
imshow(y1);
% The objective is to obtain A1,A2,A3 On doing this, following issues have surfaced: 1. Error The output y1 is not the same as A1, which should have been. Why is it so? Please help开发者_如何学JAVA
The R,G and B spaces are orthogonal. So you can solve each of those sets independently. The problem here is that mtimes
, which is your matrix multiplication operator, doesn't accept 3D inputs.
To solve this, you can loop through each of R, G and B and use linsolve for each of the resulting 2D matrices. Normally, I wouldn't recommend loops for anything in MATLAB, but here, there won't be any discernable overhead as there are only 3 iterations in the loop.
Your answer will not be any different from what it would be if you were to solve them all in one go (if that were possible), because the three spaces are independent.
EDIT
The way you've written your equations, the xi
's form the coefficient matrix and Ai
's are the unknowns. The system of equations can be written compactly as XY=Z
, where X
is a 3D matrix composed of the coefficients, xi
for each color space,RGB
; Y
is a 2D matrix, with a vector [A1, A2, A3]'
in each color space, and Z
is also a 2D matrix with vectors [A, B, C]'
in each color space.
Assuming that the colorspace is the last dimension, you can try
[xPixels,yPixels,colorSpace]=size(X);
Y=zeros(yPixels,colorspace);
opts.UT=true; opts.TRANSA=false;
for i=1:colorspace
Y(:,i)=linsolve(X(:,:,i),Z(:,i),opts);
end
You'll have to setup the matrices X
, Y
and Z
according to your problem. It is helpful to keep the looped dimension (in this case, colorspace
) as the outermost dimension, as otherwise, you'll have to use squeeze
to remove the singleton dimensions.
精彩评论