Downsample number of colors - matlab

I'd like to plot a 3D dataset with colors. That is, each point has an associated rgb color. When I use scatter3 for this task, the plotting process is veeeeery slow. I have searched for alternative options and came up with the function plot3k from FileExchange:
This function however is only able to plot each point's color by referring to some colormap via an index and does not take the rgb values directly. Also, it repeatedly uses plot3 to do its plotting which also gets very slow when the colormap is too large.
So, I was wondering:
Is there a function to downsample the number of colors? I.e., I pass a N x 3 RGB-Array to the function and the function returns indices and a new array A, where size(A,1) < N and A(indices,:) are the new approximated colors.

Yes, there is such a function in core Matlab: rgb2ind. Its purpose is to approximate truecolor images by indexed images, so we have to fiddle around a bit to make it work.
I assume that xyz is an N x 3 array of coordinates and rgb is an N x 3 array of colors. If all the colors are different,
scatter3(xyz(:,1), xyz(:,2), xyz(:,3), 4, rgb)
takes about 21 seconds for N = 100000 (on my machine).
If ncol is the number of different colors used for approximation, then this does the trick:
% convert colors from 0–1 double format to 0–255 uint8 format
rgb = uint8(floor(rgb * (256 - eps)));
% give the color array the form of a truecolor image (N x 1 x 3)
rgb = permute(rgb, [1 3 2]);
% reduce the number of colors
[ind, map] = rgb2ind(rgb, ncol, 'nodither');
The result is a sequence of integer color indices ind into the color map map. The nodither option is necessary because our rgb is not really an image and therefore spatial error diffusion doesn't make sense here. The data can now be plotted using
scatter3(xyz(:,1), xyz(:,2), xyz(:,3), 4, ind)
colormap(map)
caxis([0 ncol] - 0.5) % ensure the correct 1:1 mapping
For ncols = 100, color conversion and plotting together take about 1.4 seconds, a speed-up by a factor 15!
rgb2ind does minimum variance quantization in RGB-space, meaning that it only takes into account numeric similarity in its approximation, but not visual similarity. It should be possible to improve results by using another color space for approximation, for example CIE L*a*b*.

Related

Matlab: 1D array to RGB triplets with colormap

I'm trying to draw a set of rectangles, each with a fill color representing some value between 0 and 1. Ideally, I would like to use any standard colormap.
Note that the rectangles are not placed in a nice grid, so using imagesc, surf, or similar seems unpractical. Also, the scatter function does not seem to allow me to assign a custom marker shape. Hence, I'm stuck to plotting a bunch of Rectangles in a for-loop and assigning a FillColor by hand.
What's the most efficient way to compute RGB triplets from the scalar values? I've been unable to find a function along the lines of [r,g,b] = val2rgb(value,colormap). Right now, I've built a function which computes 'jet' values, after inspecting rgbplot(jet). This seems a bit silly. I could, of course, obtain values from an arbitrary colormap by interpolation, but this would be slow for large datasets.
So, what would an efficient [r,g,b] = val2rgb(value,colormap) look like?
You have another way to handle it: Draw your rectangles using patch or fill specifying the color scale value, C, as the third parameter. Then you can add and adjust the colorbar:
x = [1,3,3,1,1];
y = [1,1,2,2,1];
figure
for ii = 1:10
patch(x + 4 * rand(1), y + 2 * rand(1), rand(1), 'EdgeColor', 'none')
end
colorbar
With this output:
I think erfan's patch solution is much more elegant and flexible than my rectangle approach.
Anyway, for those who seek to convert scalars to RGB triplets, I'll add my final thoughts on the issue. My approach to the problem was wrong: colors should be drawn from the closest match in the colormap without interpolation. The solution becomes trivial; I've added some code for those who stumble upon this issue in the future.
% generate some data
x = randn(1,1000);
% pick a range of values that should map to full color scale
c_range = [-1 1];
% pick a colormap
colormap('jet');
% get colormap data
cmap = colormap;
% get the number of rows in the colormap
cmap_size = size(cmap,1);
% translate x values to colormap indices
x_index = ceil( (x - c_range(1)) .* cmap_size ./ (c_range(2) - c_range(1)) );
% limit indices to array bounds
x_index = max(x_index,1);
x_index = min(x_index,cmap_size);
% read rgb values from colormap
x_rgb = cmap(x_index,:);
% plot rgb breakdown of x values; this should fall onto rgbplot(colormap)
hold on;
plot(x,x_rgb(:,1),'ro');
plot(x,x_rgb(:,2),'go');
plot(x,x_rgb(:,3),'bo');
axis([c_range 0 1]);
xlabel('Value');
ylabel('RGB component');
With the following result:

Produce a 3D stem plot with a custom colormap in MATLAB

I have a matrix (200 x 4) where first 3 values are X, Y and Z data. I want use the fourth column to display each (X,Y,Z) triplet so that it maps to a color.
The fourth column contains values from 0.0 to 1.0 (200 values). I want to map these values with colormap manually and linearly. The smallest value should have blue color and the largest value may have red color.
I know that it is possible with scatter3. However, I want to do using stem3 where I can specify the color manually from colormap.
Is there a way to do this in MATLAB?
That's pretty simple to do. kkuilla posted a very insightful link. To get something started, if you want to have a colour map that varies from blue to red, you know that an image is decomposed into three colours: Red, green and blue.
Therefore, all you would have to do is vary the red and blue channels. Start with a pure blue colour, which is RGB = (0,0,255) where this is mapped to the initial weight of w = 0 and vary this to the end where RGB = (255,0,0) with w = 1. You can very easily do that by linspace. However, colours in a colour map for plotting in MATLAB are normalized so that they're between [0,1], not [0,255]. Also, because a colour map in MATLAB is a matrix of N x 3, where N is the total number of colours you want, all you have to do is:
num_colours = 10;
colourmap = [linspace(0,1,num_colours).' zeros(num_colours,1) linspace(1,0,num_colours).'];
weights = linspace(0,1,num_colours);
num_colours is the total number of colours you would like displayed. I set it to 10 to get you started. weights is something we will need for later, so don't worry about that righ tnow. Essentially, colormap would be the colour map you apply to your data.
However, what is going to be difficult now is that the data that you're plotting has no correlation to the weight of the data itself (or the fourth column of your data). This means that you can't simply use the (X,Y,Z) data to determine what the colour of each plot in your stem is going to look like. Usually for colour maps in MATLAB, the height of the stem is proportional to the colour that is displayed. For the largest Z value in your data, this would naturally be assigned to the colour at the end of your colour map, or red. The smallestZ value in your data would naturally get assigned at the beginning of your colour map, or blue.
If this was the case, you would only need to make one stem call and specify the colour map as the attribute for Color. Because there is no correlation between the height of the Z value and the weight that you're assigning for each data point, you have no choice but to loop through each of your points and determine the closest value between the weight for a point with every weight and ultimately every colour in your colour map, then apply this closest colour to each point in your stem respectively.
We determine the closest point by using the weights vector that was generated above. We can consider each colour as having a mapping from [0,1], and each weight corresponds to the colour in colourmap. Therefore, a weight of 0 is the first colour in the colour map, and that's in the first row. The next weight after this is the second colour, and that's in the second row and so on.... so we simply need to determine where each weight that's in the fourth column of your matrix is closest to for the above weights vector. This will determine which colour we need to select from the colour map to plot the point.
Given that your matrix of 200 x 4 is stored in data, you must specifically do this:
%// Spawn a new figure
figure;
%// Determine the number of points in the dataset
num_points = size(data,1);
%// For each point in the data set
for idx = 1 : num_points
%// Get 4th column element and determine closest colour
w = data(idx,4);
[~,ind] = min(abs(weights-w));
color = colourmap(ind,:);
%// Plot a stem at this point and change the colour of the stem
%// as well as the marker edge colour and face colour
stem3(data(idx,1), data(idx,2), data(idx,3), 'Color', color, ...
'MarkerEdgeColor', color, 'MarkerFaceColor', color);
%// Make sure multiple calls to stem don't clear the plot
hold on;
end
%// Display colour bar to show colours
colormap(colourmap(1:end-1,:));
colorbar('YTickLabel', colourmap);
The last two lines are a bit hackish, but we basically show a colour bar to the right of the plot that tells you how each weight maps to each colour.
Let's test this on some data. I'm going to generate a random 200 x 4 matrix of points and we will use the above code and plot it using stem3:
rng(123123); %// Set seed for reproducibility
data = rand(200,4);
num_colours = 10;
I set the total number of unique colours to 10. Once I have this above data, when I run through the code above, this is the plot I get:
You can use HSV as well. The Z values would correspond to your fourth column. Low Z values are blue and high Z values are red.
I used the site http://colorizer.org/ to work out that blue is H=0.65 and red is H=1. S and V stay the same.
From http://colorizer.org/, I got that a blue colour is H=236, S=100, V=100. Then the H value for blue is H = 235/360 = 0.65 and H=1, S=1, V=1 for red.
num_elem = 200;
c = linspace(0,1,num_elem)'; % // Replace this with the values from your fourth column
% // The equation gives blue (H=0.65) for c=0 and red (H=1) for c = 1
H = 0.65 + ((1-0.65).* c);
S = ones(size(c,1),1);
V = ones(size(c,1),1);
% // You have to convert it to RGB to be compatible with stem3
colourmap = hsv2rgb([H,S,V]);
% // Generate some sample data
theta = linspace(0,2*pi,num_elem)';
X = cos(theta);
Y = sin(theta);
Z = theta;
% // Plot the sample data with the colourmap
figure;
hold on;
for idx=1:num_elem
stem3(X(idx),Y(idx),Z(idx),':*','Color',colourmap(idx,:) ...
,'MarkerEdgeColor',colourmap(idx,:) ...
,'MarkerFaceColor',colourmap(idx,:) ...
,'LineWidth',4 ...
);
end
hold off;
set(gca,'FontSize',36');

How to make 2D scatter plot in Matlab with colors depending on values?

I have three vectors of the same lenght: x, y, and cls. I want to make a 2D plot of x and y but each point should have a color corresponding to the value of cls.
I thought about using the scatter function but you can chage the color of the whole plot, not of particular elements. Any ideas?
I would like to get something like in this example, when cls has elements of three values:
From the help of scatter:
scatter(x,y,a,c) specifies the circle colors. To plot all circles with the same color, specify c as a single color string or an RGB triplet. To use varying color, specify c as a vector or a three-column matrix of RGB triplets.
you can construct c as
c=zeros(size(x),3);
c(cls==1,:)=[1 0 0]; % 1 is red
% ...
scatter(x,y,1,c)
However, I dont know how to do the background. Did you apply some Machine learning algorithm to clasify the data? maybe you can get the equations to plot the background from there, but it depends on the method.
If you have the Statistics Toolbox, there is an easy way of doing this, it's called gscatter.
It takes similar inputs to scatter, but the third input is the group:
gscatter(x,y,cls)
You can add colours and markers - this plots with red, then green, then blue (order determined by the contents of cls, all markers circles.
gscatter(x,y,cls,'rgb','o')
Here's another solution splitting your data in three using logical indexing:
% Some random data
x = rand(100,1);
y = rand(100,1);
cls = round(2*rand(100,1));
% Split the data in three groups depending on the value in cls
x_red = x(cls==0);
y_red = y(cls==0);
x_green = x(cls==1);
y_green = y(cls==1);
x_blue = x(cls==2);
y_blue = y(cls==2);
% plot the data
scatter(x_red,y_red,1,'r')
hold on
scatter(x_green,y_green,1,'g')
scatter(x_blue,y_blue,1,'b')
hold off
One very simple solution with c being the color vector:
scatter3(X,Y,zeros(size(X,1)),4,c);
view(0,90);

Get matrix with colors of colomap figure

I have generated a heatmap from a matrix of values and applied a colormap on it. Is there a way for me to create an m-by-3 matrix of the colors of each square segment?
My understanding is that you have a matrix of values (m-by-n) which you colour using a colour map and you want to get the 3D (m-by-n-by-3) matrix that is the RBG image of this coloured matrix. In that case try:
%//C is your data
C = randn(m,n);
%//d is the number of discrete colour in your colormap and map is your colormap itself.
d=4;
map = jet(d);
%//Normalize C to index map, i.e. make values of C range from 1 to d
C_norm = (C - min(C(:)))./(max(C(:)) - min(C(:))); %//Normalize to btw [0,1]
C_discrete = ceil(C_norm*(d-1)+1); %//Normalize to btw [1,d]
%//Index in map using linearized (i.e. m*n-by-1 vector) version of C
C_mapped = map(C_discrete(:),:);
%//Reshape to m-by-n-by-3
C_RGBreshape(permute(C_mapped, [1, 3, 2]), m, n, 3);
You mean to create a matrix with the colormap? Normally you do it the other way around. Try
cmap = jet(64);
colormap(cmap);
This assigns the colormap to cmap as a matrix and then assigns it to th axes. I also think that the colormap is an axes property and can be accessed through the set and get functions. Also you may not want to use the jet colormap but any colormap can of course be used

Create a tailored colorbar in matlab

I have to create a map to show how far o how close some values are from a range and give them colors in consequence. Meanwhile, values that are within that range should have another different color.
For example: only the results that are within [-2 2] can be considered valid. For the other values, colors must show how far are from these limits (-3 lighter than -5, darker)
I've tried with colorbar but I'm not able to set up a self-defined color scale.
Any idea??
Thanks in advance!
You need to define a colormap for the range of values you have.
The colormap is N*3 matrix, defining the RGB values of each color.
See the example below, for a range -10:10 and valid values v1,v2:
v1=-3;
v2=+3;
a = -10:10;
graylevels=[...
linspace(0,1,abs(-10-v1)+1) , ...
ones(1, v2-v1-1) , ...
linspace(1,0,abs(10-v2)+1)];
c=repmat(graylevels , [3 1])';
figure;
imagesc(a);
colormap(c);
Here is some code that I just put together to demonstrate a simple means of creating your own lookup table and assigning values from it to the image that you're working with. I'm assuming that your results are in a 2D array and I just used randomly assigned values, but the concept is the same.
I mention the potentila use of HSV as a coloring scheme. Just note that, that requires you to have a m by n by 3 matrix. The top layer is the H - hue, 2nd being the S - saturation and the 3rd being the V or value (light/dark). Simply set the H and S to whatever values you want for the color and vary the V in a similar manner as shown below and you can get the varied light and dark color you want.
% This is just assuming a -10:10 range and randomly generating the values.
randmat = randi(20, 100);
randmat = randmat - 10;
% This should just be a black and white image. Black is negative and white is positive.
figure, imshow(randmat)
% Create your lookup table. This will illustrate having a non-uniform
% acceptable range, for funsies.
vMin = -3;
vMax = 2;
% I'm adding 10 here to account for the negative values since matlab
% doesn't like the negative indicies...
map = zeros(1,20); % initialized
map(vMin+10:vMax+10) = 1; % This will give the light color you want.
%linspace just simply returns a linearly spaced vector between the values
%you give it. The third term is just telling it how many values to return.
map(1:vMin+10) = linspace(0,1,length(map(1:vMin+10)));
map(vMax+10:end) = linspace(1,0,length(map(vMax+10:end)));
% The idea here is to incriment through each position in your results and
% assign the appropriate colorvalue from the map for visualization. You
% can certainly save it to a new matrix if you want to preserve the
% results!
for X = 1:size(randmat,1)
for Y = 1:size(randmat,2)
randmat(X,Y) = map(randmat(X,Y)+10);
end
end
figure, imshow(randmat)