I am doing a very interesting Computer Vision project which talks about how to "create manually" images with Matlab.
The teacher gave me three matrices: the illuminant matrix (called E), the camera sensitivity matrix (called R) and finally, the surface reflectance matrix (called S).
The matrix dimensions are as follows:
S: 31x512x512 (reflectance samples x x-dimension x y-dimension)
R: 31x3
E: 31x1
The teacher gave me also the following relationship:
P=transpose(C)*R=transpose(S)*diagonal(E)*R
Where C is the color matrix.
Where P is the sensor response matrix.
The goal is to display the image formed by all the previous matrices. Therefore, we have to compute the P matrix.
The class of all the matrices is double.
This is what I have done:
Diag_D=diag(D);% Diagonal matrix of D
S_reshaped= reshape(S,31,[512*512]);% Reshape the surface reflectance matrix
S_permute=permute(S_reshaped,[2 1]);% The output matrix is a 262144x31 matrix
Color_Signal_D65_buffer=S_permute*Diag_DD;
Color_Signal_D65=reshape(Color_Signal_D65_buffer,[512 512 31]);% This is the final color matrix
Image_D65_buffer= (reshape(Color_Signal_D65,[512*512],31))*R;% Apply the given formula
Image_D65= reshape(Image_D65_buffer,[512 512 3]);% image formation
Image_D65_norm=sqrt(sum(Image_D65.^2,3));% Compute the Image_D65 norm
Image_D65_Normalized=bsxfun(#rdivide, Image_D65, Image_D65_norm);% Divide each element of the matrix by the norm in order to normalize the matrix
figure
imshow(Image_D65_Normalized)% Display the image
However,it did not work at all. The output is an image but the colors are completely wrong (there is too much blue on the image).
I think it could be a matrix reshaping problem but I have tried all the possible combinations but nothing to do.
Thank you so much for your help
I've finaly found the error. It was a problem in the normalization process. I was using the wrong formula.
Related
When I perform QR decomposition on a 3x2 matrix A in Julia, it gives a 3x3 matrix Q. Is there any way I can get a "thin" version of this QR, where it returns a Q that is 3x2 (same dimensions as matrix A)? My goal is just to get an orthonormal basis for the column space of A, so I don't need a 3x3 matrix Q.
This can be achieved with Matrix(qr(A)). qr doesn't return matrices, but rather returns an object that can multiply by other matrices or easily extract the thin or full Q matrix.
I have a 3D matrix containing Cartesian coordinate points. In the example shown below a scaled down version of such a matrix is given (a) with dimensions 10x10x10x3. Then I have a rotation matrix R of size 3x3 and I want to make a dot product of this to each 3x1 position vector given in the matrix a, for example a[0,0,0] is the position vector for top left most coordinate point.
A simple way to do this is by using a for loop -
import numpy as np
a = np.ones([10, 10, 10, 3])
R=np.ones([3,3])
a=np.reshape(a,(1000,3))
b=np.array([np.dot(R,xyz) for xyz in a])
b=np.reshape(b, (10,10,10,3))
But this is way too slow when the matrix a becomes large. Is there a way to do a matrix multiplication type thing to speed up the computation?
I figured out a solution to my problem which speeds up the computation to more than 10 folds. But it is not a fancy matrix multiplication solution, so something that can make it even faster is always appreciated.
My current solution is the following:
X,Y,Z=a[:,:,:,0],a[:,:,:,1],a[:,:,:,2]
R00X=R[0][0]*X
R10X=R[1][0]*X
R20X=R[2][0]*X
R01Y=R[0][1]*Y
R11Y=R[1][1]*Y
R21Y=R[2][1]*Y
R02Z=R[0][2]*Z
R12Z=R[1][2]*Z
R22Z=R[2][2]*Z
#Rotated Field
Xr=R00X+R01Y+R02Z
Yr=R10X+R11Y+R12Z
Zr=R20X+R21Y+R22Z
b=np.array([Xr,Yr,Zr]).T
I am using matlab's built in function called Procrustes to see the rotation translation and scale between two images. But, I am just using coordinates of the brightest points in the image and rotating these coordinates about the center of the image. Procrustes compares two matrices and gives you the rotation, translation, and scale. However, procrustes only works correctly if the matrices are in the same order for comparison.
I am given an image and a separate comparison coordinate matrix. The end goal is to find how much the image has been rotated, translated, and scaled compared to the coordinate matrix. I can just use Procrustes for this, but I need to correctly order the coordinates found from the image to match the order in the comparison coordinate matrix. My thought was to compare the distance between every possible combination of points in the coordinate matrix and compare it to the coordinates that I find in the picture. I just do not know how to write this code due to the fact if there is n coordinates, there will be n! possible combinations.
Just searching for the shortest distance is not so hard.
A = rand(1E4,2);
B = rand(1E4,2);
tic
idx = nan(1,1E4);
for ct = 1:size(A,1)
d = sum((A(ct,:)-B).^2,2);
idx(ct) = find(d==min(d));
end
toc
plot(A(1:10,1),A(1:10,2),'.r',B(idx(1:10),1),B(idx(1:10),2),'.b')
takes half a second on my PC.
The problems can start when two points in set A are matched to the same location in set B.
length(unique(idx))==length(idx)
This can be solved in several ways. The best (imho) is to determine a probability that point B matches with point A based on the distance (usually something that decreases exponentially), and solve for the most probable situation.
A simpler method (but more error prone) is to remove the matched point from set B.
edit: I decided to split this question into two parts, because it were really two questions: 1. how to make a polar surface plot in MATLAB (this question) and 2. how to put fit polar data points into a coarse (and non-polar) matrix
I have a matrix that contains certain grey values (values between zero and one). These points are stored in a rectangular matrix, but really the data points are acquired by rotating the detector. This means that I actually have polar coordinates (I know the polar coordinates for every single pixel in my starting matrix).
I want to make a polar plot of the data points. I have the example of this below.
Because MATLAB stores images as matrices, the polar coordinates I have do not exactly match the 'bins' of the matrix. Therefore, we currently use an interpolation algorithm to put the polar coordinates into a square matrix. However, this is extremely slow. I see two methods to solve this issue:
let MATLAB directly plot the data points as polar.
calculate once how to convert from the start matrix to the end matrix and let MATLAB do this through matrix multiplication.
Some basic information:
Input matrix size: 512×960
Current output matrix size: 1024×1024
I think there is built in function for polar plot in matlab.
Z = [2+3i 2 -1+4i 3-4i 5+2i -4-2i -2+3i -2 -3i 3i-2i];
polarplot(Z,'*')
this command plots:
plot polar
See this link:
http://www.mathworks.com/help/matlab/ref/polarplot.html
To plot in grayscale, use "pcolor" and specify colormap to "gray"
www.mathworks.com/help/matlab/ref/ pcolor.html
The question was solved (apart from a minor flaw), partially because K.M. Shihab Uddin pointed me in the right direction. Unfortunately, using surf means continuously really plotting the image in a figure, and this is slow as well.
So I have X and Y values both in separate matrices and greyscale values (in a matrix called C) for every X and Y combination.
I found out that pcolor is just surf with a viewpoint from the top. So I used the following code to plot my graph.
surf(X,Y,C*255)
view([0,0,500])
However, this gave me a completely black image. This is because surf (and pcolor) create 960 grid lines radially in my case. The solution is to use:
surf(X,Y,img2*255,'EdgeColor','none')
view([0,0,500])
Now I have an almost perfect image, like I had before. Only, of my 960 radial lines, one is left white, so I still have to solve that. However, I feel this is a technical detail of the function surf, and answering this part does not belong in this question.
The resulting image
I am doing a project on image quality assessment. I converted the image to grayscale & divided the entire image into 8x8 matrices using mat2cell function. I did this for two images, and now I want to calculate covariance between these two images (i.e the covariance between the matrix of image 1 and covariance between the same matrix of image 2). Note that both are the same images: one a pure image without distortions and one with distortions.
First convert your image to matrix:
I = double(imread('photo.jpg'));
then calculate covariance:
x=cov(I);
For single matrix input, C has size [size(A,2) size(A,2)] based on the number of random variables (columns) represented by A. The variances of the columns are along the diagonal. If A is a row or column vector, C is the scalar-valued variance.
For two-vector or two-matrix input, C is the 2-by-2 covariance matrix between the two random variables. The variances are along the diagonal of C.