Convert correlation matrix to an RGB image matlab - matlab

I have a correlation Matrix outputed by the corr(X) function.
I need to display it in an RGB image format with the following specs.
Negative correlation should be red and possitive correlation should be green.
The values of the correlation matrix are in the range of [ -1 , 1 ].
1 -0,0286473845495979 0,185190317331816
-0,0286473845495979 1 -0,309327144422681
0,185190317331816 -0,309327144422681 1
I convert the matrix to the range of [ -255 , 255 ] and I need to display the negative values in red and the possitive values in green, with the corresponding color intensity ...
0 -262 208
-262 0 -334
208 -334 0
Any help would be great !

red = [1,0,0];
green = [0,1,0];
R = linspace(red(1),green(1),256);
G = linspace(red(2),green(2),256);
B = linspace(red(3),green(3),256);
map = [R', G', B'];
colormap(map)
colorbar
See How to create a custom colormap programmatically? for an explanation. Also you'll notice it goes a yellowy-brown in the middle. If you don't want this then I suggest either making the middle black or white, the answer I linked to should explain how to achieve that.

Related

Matlab: How to color a region

I am wishing to use Matlab to achieve the following goal:
I have a function that takes in two inputs and gives a real number.
function T = SS(x,y)
%%% some calculation %%%
T = returnval
I want the point (x,y) in the x-y plane to be coloured blue if the return value is equal to 1, green if the return value is equal to 0.5 etc.
I don't know how to approach this.
(the calculation is complicated so it is not obvious what's the relationship between x and y with respect to z. Thus can't write an equation to divide the region then colour accordingly.)
Thanks for helping.
If you can group all your T's into a vector, you can use scatter to make the plot. Then just setup your own colormap. Here's an example:
%// Sample data
[x,y]=meshgrid(0:.1:2);
x=x(:);y=y(:);
T=rand(size(x));
%// Define the colormap
MAP=[1 0 0; %// red
0 1 0; %// green
0 0 1]; %// blue
colormap(MAP) %// apply the colormap
scatter(x,y,[],T) %// make the plot
MAP is a matrix, and each row defines a colour. In this case, there are three rows, so elements with values between 0 and 1/3 will be red, elements with values between 1/3 and 2/3 will be green, and elements between 2/3 and 1 will be blue. In general, the range of T values (i.e. max(T)-min(T)) is evenly divided into each colour defined by MAP.

intensity to RGB color convertor, matlab

I have color vector =[0.....1]. I want to convert it to RGB code such that
color_vector =[0.....1] % o for blue, .5 for green and 1 for red
R=255,0,0
G=0,255,0
B=0,0,255
Is there any matlab command(which I could not found) to do it or code.
Another issue is that I want to make my own range for green color (.45-.55) all should be green color.
Basically what you describe is a colormap - but you need to index your color vector for
that.
Incidentally hsv2rgb produces a similar color mapping. But starting from red:
As the hue varies from 0 to 1, the resulting color varies from
red, through yellow, green, cyan, blue and magenta, back to red.
Do you want to linearly interpolate between the colors, for intensity values that are not exactly 0, 0.45-0.55, or 1? If so, you can use real2rgb (on the MATLAB File Exchange), as follows:
I = rand(100, 100); % Input data
cmap = [1 0 0 45; 0 1 0 10; 0 1 0 45; 0 0 1 0]; % Colormap defining the transformation
RGB = real2rgb(I, cmap); % Do the conversion
I use CC and it works well.I can interpolate the colors.

conversion between RGB and HSV space

I am currently trying to convert two objects from RGB space to HSV space using the rgb2hsv function, one being an image, the other being a matrix of RGB values, but my results for the value part of the matrix are not consistent.
The results for value in matrix I are between 1 and 255, but in the second matrix D, they are between 0 and 1. Does anybody know why this is the case?
I = imread('00000001.jpg');
I = rgb2hsv(I);
D = [221 12 26; 30 68 76; 40 47 27; 165 87 25; 37 59 26; 148 125 91];
D = rgb2hsv(D);
When you call rgb2hsv with a N-by-3 matrix, it is interpreted as a colormap, not an image or even image intensities. A colormap has values on [0,1], while a uint8 image has values on [0,255]. This is what rgb2hsv expects.
The colormap syntax explained by the help page for rgb2hsv:
H = rgb2hsv(M) converts an RGB color map to an HSV color map.
Each map is a matrix with any number of rows, exactly three columns,
and elements in the interval 0 to 1.
When you run D = rgb2hsv(D); it is running it with the above syntax, rather than treating the input as an image.
That said, you can just divide the third column of the output D by 255 since the resulting bizarro colormap seems to simply have scaled value elements.
I simply multiplied by 255 on the resulting matrix from running rgb2hsv on an image. I verified that the hue, saturation, and value were then the correct value and it was. This solved my problem. Be sure to verify. Another weird thing was that those values are a percentage, so 1 means 100% and 0.5 means 50%. This also threw me off.

Getting the average grey scale of each channel (R,G,B) of a pic [MATLAB]

STILL NEED HELP, ANSERWS WERE NOT THE THING I WAS LOOKING FOR!
I need a function that gets me the average grey scale of each channel (R,G,B) of an image
ave = getAverageGreyScale(image)
ave must return a vector of 3 elements. Each one with the average grey scale of one channel (R,G,B):
[ a b c ] a = average greyscale of R b = average greyscale of
G c = average greyscale of B
Still ned help! Can anyone give me a help with this?
Thanks in advance!
I think you are looking for
function [Ravg, Gavg, Bavg] = getAverageRGBValues(I)
Ravg = mean(mean(I(:, :, 1)));
Gavg = mean(mean(I(:, :, 2)));
Bavg = mean(mean(I(:, :, 3)));
Assuming I is a rows x cols x 3 matrix (which is what you get from, e.g., imread).
Edit after clarification:
You need the average along dimensions 1 & 2, use:
ave = mean(mean(image,2),1);
Original answer:
If (as it is usage) your image is a 3 dimension matrix with last dimension length of 3, you simply can use:
ave=squeeze(mean(image,3)); %#compute the average value for each pixel along the third dimension
The average is a rough estimate of luminance but a better approach would be to first multiply each color plane by a different coefficient since red, green and blue do not amount equaly to luminance. Standard coefficients are:
0.3 for red
0.59 for green
0.11 for blue
your code therefore would look like this :
ave=image(:,:,1)*0.3 + image(:,:,2)*0.59 + image(:,:,3)*0.11

Matlab - attach scalar value to RGB colours

When I read the following image into Matlab I am obtaining a 3D matrix which basically contains the values of RGB colour samples which compose every pixel within the image.
Is there any Matlab function which I can use to assign a scalar value between lets say [-10, 10] to every pixel within the image based on the RGB values? Pure red should be like 10, yellow should be 5, green should be 0, blue should be like -8 and cyan should be like -10.
Have a look at RGB2IND: http://www.mathworks.com/help/techdoc/ref/rgb2ind.html
You could then replace the Nx3 index output with your own custom N element index vector.
As you can see from running
colorlist=[1 0 0;1 1 0;0 1 0; 0 0 1; 0 1 1];
valuelist=[10 5 0 -8 -10];
figure;
hold all;
for i=1:10
bar(i,i,'FaceColor',colorlist(i,:))
end;
the colorlist defined above corresponds to the colors you are interested in.
To solve your question, for each pixel in your image, you would have to determine which RGB values are exactly zero and which ones are exactly one to determine between which pair you want to interpolate. For example, assuming size(image)==[100,100,3] and image=im2double(imread(myfilename)) i.e. max(image(:))==1:
if ((image(x,y,:)==0)==[0 0 1]) && ((image(x,y,:)==1)==[1 0 0])
%# interpolate between red and yellow
result(x,y)=10 - 5*image(x,y,2); %# pure red gives 10, pure yellow gives 5
elseif ((image(x,y,:)==0)==[0 0 1]) && ((image(x,y,:)==1)==[0 1 0])
%# interpolate between yellow and green
result(x,y)=5*image(x,y,1); %# pure yellow gives 5, pure green gives 0
elseif
%# ...
end
This solution is not vectorized, but it should get you on the right track for a doable implementation. Of course, if you could avoid saving the data with a multicolor lookup-table as RGB but save the raw values instead, you could save yourself some hassle...