How to create a colormap based on value? - matlab

I have two vectors as follows:
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20]
Where x denotes the distance on the x-axis and sir_dB the SNR. For this, I need to generate a color map for a grid of 50 x 60m something similar to this:
based on the value of sir_dB.
I tried the following:
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
radius = 5;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set, test_pt, radius );
neighborhood = candidate_set(idx{1}, :);
Once I have the neighbors at a radius of 5m, I need to color that part of the grid based on the sir_dB value for a corresponding x value.
I need to have the plot in such a way that for all values of sir_dB greater than 15, the grid should be colored green, yellow for y greater than 0 and red for y greater than -20.
Could someone provide me inputs of how to do this best?

Im not sure exactly what you want, but this should get you started with contourf. I increased the granularity of xrange and yrange to make the radius more smooth but you can change it back if you want.
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:0.1:50;
yrange = -30:0.1:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
r = sqrt((test_pt(1)-X(:)).^2 + (test_pt(2)-Y(:)).^2);
idx = r>5;
snr = nan(size(X));
snr(idx) = interp1(x,sir_dB,X(idx),'linear');
% Some red, yellow, green colors
cmap = [0.8500 0.3250 0.0980;
0.9290 0.6940 0.1250;
0 0.7470 0.1245];
figure();
colormap(cmap);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
Plotting the the contour plot alongside the original sir_dB we see that it lines up (assuming you want linear interpolation). If you don't want linear interpolation use 'prev' or 'next' for the interp1 method.
figure();
colormap(cmap);
subplot(2,1,1);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
subplot(2,1,2);
plot([0,50],[-20,-20],'-r',[0,50],[0,0],'-y',[0,50],[15,15],'-g',x,sir_dB);

Here is another suggestion, to use imagesc for that. I nothed the changes in the code below with % ->:
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
% -> create a map for plotting
Signal_map = nan(size(Y));
candidate_set = [X(:), Y(:)];
test_pt = [10 20];
radius = 35;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set,test_pt,radius);
neighborhood = candidate_set(idx{1}, :);
% -> calculate the distance form the test point:
D = pdist2(test_pt,neighborhood);
% -> convert the values to SNR color:
x_level = sum(x<D.',2);
x_level(x_level==0)=1;
ColorCode = sir_dB(x_level);
% -> apply the values to the map:
Signal_map(idx{1}) = ColorCode;
% -> plot the map:
imagesc(xrange,yrange,rot90(Signal_map,2))
axis xy
% -> apply custom color map for g-y-r:
cmap = [1 1 1 % white
1 0 0 % red
1 1 0 % yellow
0 1 0];% green
colormap(repelem(cmap,[1 20 15 35],1))
c = colorbar;
% -> scale the colorbar axis:
caxis([-21 50]);
c.Limits = [-20 50];
c.Label.String = 'SNR';
The result:

Related

How to control colorbar color ranges in Matlab plots?

I have the following code:
[X,Y,Z] = peaks(30);
crange = 1.5;
[maxval dummy] = max(Z(:));
[minval dummy] = min(Z(:));
% green, yellow, red
cmap = [0 1 0; 1 1 0; 1 0 0];
figure
colormap(cmap);
surf(X,Y,Z);
caxis([30 55]);
cbh=colorbar;
set(cbh,'Ytick',[30 32 38 55]);
My goal is to set the limits of the color bar so that the colors are like this:
green from 30 to 32
yellow from 32 to 38
red from 38 to 55
I believe I should somehow change the CData variable, so I used these lines of code without success:
i = findobj(cbh,'type','image');
set(i,'cdata',[30 32 38]','YData',[30 55]);
Your custom colorbar consists of (32-30 = ) 2 + (38-32 = ) 6 + (55-38 = ) 17 = 25 "units" of color. So a simple trick would be replicating each color the required number of "units":
function q58097577
[X,Y,Z] = peaks(30); Z = (Z - min(Z(:)))*5;
% green, yellow, red
nG = 32-30; nY = 38-32; nR = 55-38;
cmap = [ repmat([0 1 0], [nG 1]); repmat([1 1 0], [nY,1]); repmat([1 0 0], [nR,1]) ];
figure()
colormap(cmap);
surf(X,Y,Z);
caxis([30 55]);
cbh=colorbar;
set(cbh,'Ytick',[30 32 38 55]);
Resulting in:

Values do not match data in matlab geographic plot

I am plotting trajectories in Matlab using contourf. I am having an issue with the colors matching the data. I have posted my current image below. All areas that do not have data should be white as they are zero which I specifically specified in the script(they are currently blue-ish which is the the 0.1 to 1 range). In addition, values that are yellow, should be in the blue range(<1). Any suggestions?
Here is the part of my script where I do the plotting:
axesm('mercator', 'MapLatLim', latlim, 'MapLonLim', lonlim,...
'Frame', 'on', 'Grid', 'on', 'MeridianLabel', 'on', 'ParallelLabel', 'on')
setm(gca,'mlabelparallel',-20)
load coastlines
Contours = [0.001 0.01 0.1 1 10 100];
[c,h] = contourfm(latlim, lonlim, u, log(Contours));
colorbar('YTick', log(Contours), 'YTickLabel', Contours);
myColorMap = jet(256).^.3;
myColorMap(1,:) = [1];
colormap(myColorMap)
colorbar
caxis(log([Contours(1) Contours(length(Contours))]));
colorbar('FontSize', 12, 'YTick', log(Contours), 'YTickLabel', Contours);
geoshow(coastlat, coastlon,'Color', 'k')
Contour level, V, in contourfm(lat,lon,Z, V) does not scale your data or colour. It works in a different way than what you thought.
Let's see one example first:
u = rand(8)+0.1; u(1:2,:) = 0; u(5:6,:) = 10; u(7:8,:) = 100;
V = [0,1,40,100];
contourfm([0,1], [0,1], u, V);
mycm = jet(256).^.3; mycm(1,:) = 1;
colormap(mycm)
contourcbar('FontSize', 12, 'YTick', V, 'YTickLabel', V);
where u is
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0.1947 0.6616 0.2413 0.6511 0.4403 0.9112 1.0016 0.5654
0.8422 0.3159 0.5695 0.6478 0.9933 0.1686 0.8387 0.5362
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100
As you can see, for V = [0,1,40,100] all values from 0 to 1 are white, values from 1 to 40 are cyan and above are red.
Therefore, you must scale your u then assign appropriate contour level. Use contourcbar instead of colorbar to check the colours first.
Apart from the problem with contour level, I suspect the u parameter contains negative values. The colour at the bottom of the colour bar is always assigned to the minimum z value. You must ensure 0 is the minimum value in u, i.e. remove the negative values.

How to patch in a for loop

i want to patch points to obtain a square and do it for all other points on the axis, but i want to do it in a for loop.. Thereafter, i would apply some transformation properties to the patched points. This is what i have done so far. Any help will be well appreciated.
whitebg('g')
axis on
% first patch point on the axis
pts2 = [0 0 1 1 0;0 1 1 0 0];
% last patch point on the axis
pts3 = [29 29 30 30 29;29 30 30 29 29];
[n m] = size(pts2);
[o p] = size(pts3);
axis([0 30 0 30])
shg
theta=0;
dx=0;
dy=0;
d=patch(pts1(1,1:end),pts1(2,1:end),'b*-');
for pts1 = pts2:pts3
if (d==patch(pts1(1,1:end),pts1(2,1:end),'b*-'))
delete(d)
end
%function to process
rot = [cosd(theta) sind(theta);-sind(theta) cosd(theta)];
trans = [1 0 dx;0 1 dy; 0 0 1];
homogeneous_rot = eye(3);
homogeneous_rot(1:2,1:2) = rot;
homogeneous_pts1 = [pts1; ones(1,5)];
trans_pts1 = trans*homogeneous_rot*homogeneous_pts1;
hold off
f=patch(trans_pts1(1,1:end),trans_pts1(2,1:end),'r*-');
draw now
end

Matching Colormap and Colorbar

I'm running the following code
% x_start y_start x_end y_end concentration
A = [0 0 1 1 10
0 1 3 3 0.6
3 1 6 2 1.2];
cmap = jet(256);
con_min = 0;
con_max = 10;
ind_c = round((size(cmap,1)-1)*A(:,5)/(con_max-con_min))+1
figure;
set(gca,'ColorOrder',cmap(ind_c,:),'NextPlot','replacechildren');
plot([A(:,1) A(:,3)]',[A(:,2) A(:,4)]');
colorbar
However, my colorbar is not matching up with my colormap; it's using the default colorbar, and I'm running a custom colormap. How can I make it so that my colorbar shows the right values for my colormap?

Getting all pixel coordinates of a vector inside a image

I have an intensity/greyscale image, and I have chosen a pixel inside this image. I want to send vectors starting from this pixel in all directions/angles, and I want to sum all the intensities of the pixels touching one vector, for all vectors.
After this step I would like to plot a histogram with the intensities on one axis and the angle on the other axis. I think I can do this last step on my own, but I don't know how to create these vectors inside my greyscale image and how to get the coordinates of the pixels a vector touches.
I previously did this in C++, which required a lot of code. I am sure this can be done with less effort in MATLAB, but I am quite new to MATLAB, so any help would be appreciated, since I haven't found anything helpful in the documentation.
It might not be the best way to solve it, but you can do it using a bit of algebra, heres how...
We know the Point-Slope formula of a line passing through point (a,b) with angle theta is:
y = tan(theta) * (x-a) + b
Therefore a simple idea is to compute the intersection of this line with y=const for all const, and read the intensity values at the intersection. You would repeat this for all angles...
A sample code to illustrate the concept:
%% input
point = [128 128]; % pixel location
I = imread('cameraman.tif'); % sample grayscale image
%% calculations
[r c] = size(I);
angles = linspace(0, 2*pi, 4) + rand;
angles(end) = [];
clr = lines( length(angles) ); % get some colors
figure(1), imshow(I), hold on
figure(2), hold on
for i=1:length(angles)
% line equation
f = #(x) tan(angles(i))*(x-point(1)) + point(2);
% get intensities along line
x = 1:c;
y = round(f(x));
idx = ( y<1 | y>r ); % indices of outside intersections
vals = diag(I(x(~idx), y(~idx)));
figure(1), plot(x, y, 'Color', clr(i,:)) % plot line
figure(2), plot(vals, 'Color', clr(i,:)) % plot profile
end
hold off
This example will be similar to Amro's, but it is a slightly different implementation that should work for an arbitrary coordinate system assigned to the image...
Let's assume that you have matrices of regularly-spaced x and y coordinates that are the same size as your image, such that the coordinates of pixel (i,j) are given by (x(i,j),y(i,j)). As an example, I'll create a sample 5-by-5 set of integer coordinates using MESHGRID:
>> [xGrid,yGrid] = meshgrid(1:5)
xGrid =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
yGrid =
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
Next we can define a line y = m*(x - a) + b passing through the coordinate system by selecting some values for the constants and computing y using the x coordinates of the grid:
>> a = 0;
>> b = 1;
>> m = rand
m =
0.5469
>> y = m.*(xGrid(1,:)-a)+b
y =
1.5469 2.0938 2.6406 3.1875 3.7344
Finally, we find the y points in the grid that differ from the points computed above by less than the grid size:
>> index = abs(yGrid-repmat(y,size(yGrid,1),1)) <= yGrid(2,1)-yGrid(1,1)
index =
1 0 0 0 0
1 1 1 0 0
0 1 1 1 1
0 0 0 1 1
0 0 0 0 0
and use this index matrix to get the x and y coordinates for the pixels crossed by the line:
>> xCrossed = xGrid(index);
>> yCrossed = yGrid(index);