How to group the green contour and the yellow contour? - matlab

I'm using imcontour in MATLAB to get the contour of some image. The resulting image is as follow. Is there any way that I can pick out the green contour together as a group, and the yellow contour together as a group?
fig.1

The good news is that your data are already grouped. Assuming you have an image/matrix I and you are doing something like imcontour(I, 2), you have just to use [C, h] = imcontour(I, 2).
C is a ContourMatrix, which contains (type help clabel) the "contour line definitions, returned as a two-row matrix. Each contour line in the plot has an associated definition. If there are a total of N contour lines in the plot, then the contour matrix consists of N definitions (N=2 in your example):
C = [C(1) C(2)...C(k)...C(N)]
Each contour line definition follows this pattern:
C(k) = [level x(1) x(2) ... x(numxy)
numxy y(1) y(2) ... y(numxy) ]

Related

Generate a 3D scatter plot from 3D Matrix - Matlab [duplicate]

This question already has an answer here:
Plotting volumetric data in MATLAB
(1 answer)
Closed 4 years ago.
I have given a 300x300x300 Matrix.
The first subscript represents the x-value, the second the y-value and the third the z-value.
To access a value of a specific point in the matrix i use:
matrix(x-val, y-val, z-val)
I want to create a 3D scatter plot where the color of the dots in the plot changes based on the values of the points in the matrix. All values are >=0
As i am pretty new to Matlab i have no idea where to start.
MathWorks has a page that summarizes types of MATLAB plots. I've referenced it on multiple occasions. The function you are looking for is scatter3(X,Y,Z,S,C). Walk through the function's example, it should help you out.
I'm not sure how you can have a 300x300x300 matrix for a point cloud in 3-D. I will assume you have a 300x300x3 matrix, i.e.:
x = matrix(:,:,1);
y = matrix(:,:,2);
z = matrix(:,:,3);
First of all, you probably want to rearrange points into a 2D matrix:
m = reshape(matrix, numel(matrix(:,:,1), 3);
n = size(m,1);
Your matrix is now arranged as a n-by-3 matrix, coloumn 1, 2 and 3 representing x-, y- and z-axis respectively, i.e.:
m = [ x1 y1 z1]
[ x2 y2 z2]
[ ... ]
[ xn yn zn]
Then you can create a basic 3D scatter plot:
scatter3(m(:,1), m(:,2), m(:,3))
However, this is not what you want, because points are in the same colour. To add colour based on your colouring logic, you should firstly create a color matrix using one of the MATLAB's built-in color maps. Here I use jet:
myc = jet(n);
You can also create your own colour map ofc. Elements in the colour matrix are simply normalised rgb values.
Now you will have to weight each points using your own logic:
weighting = myWeightingLogic(m);
weighting will be a n-by-1 vector and it should be normalised if it is not yet.
weighting = weighting/max(weighting);
Now you can colour your scatter plot:
scatter3(m(:,1), m(:,2), m(:,3)), [], myc(round(weighting*n),:));
The full code:
m = reshape(matrix, numel(matrix(:,:,1), 3);
n = size(m,1);
myc = jet(n);
weighting = myWeightingLogic(m);
weighting = weighting/max(weighting);
scatter3(m(:,1), m(:,2), m(:,3)), [], myc(round(weighting*n),:));

Contours in Matlab/Octave

I am having some trouble understanding contours.
What I have understood so far is that contours are a way to represent a 3d figure in a 2d plane. It does so by plotting a function of 2 variables as curves along which the function has same value.
Now if I do:
z=[1 4; 10 7];
contour(z);
I get this:
I read the documentation and it says:
contour(Z) draws a contour plot of matrix Z, where Z is interpreted as
heights with respect to the x-y plane. Z must be at least a 2-by-2
matrix that contains at least two different values. The x values
correspond to the column indices of Z and the y values correspond to
the row indices of Z. The contour levels are chosen automatically.
Thus for x=1,y=1: z=1, x=2,y=1: z=4 and so on. However I can't understand how to interpret this as the contour plot shown above.
And if I write:
contour(X1, X2, vals, [0.5 0.5], 'b'); where X1, X2 and vals are equal sized matrices and vals is a matrix of only 0s and 1s. I can't understand what does the argument [0.5 0.5] do. I read the documentation which states:
contour(Z,v) draws a contour plot of matrix Z with contour lines at
the data values specified in the monotonically increasing vector v. To
display a single contour line at a particular value, define v as a
two-element vector with both elements equal to the desired contour
level.
and I am unable to understand this statement.
The problem of the first contour is that there are just 4 values. Try something like
x = 0:0.1:10;
y = 0:0.1:10;
z = sin(x') * cos(y);
contour(z)
For the second thing, this means that if you want to see just particular contours, input them as vector v. In the example above:
contour(z, [0.1, 0.2, 0.3])
will show contour lines of 0.1, 0.2 and 0.3.
To have a single contour line, you can't have just (z, 0) but require (z, [0,0])

What does the vector input in MATLAB fnplt do?

The documentation of fnplt says
A vector of the form [a,b], to indicate the interval over which to
plot the univariate function in f.
So I thought vector [a, b] means plotting the part of spline curve s that falls into the interval [a, b]. But the following experiments don't seem to support my this interpretation.
Running
x = [0.16;0.15;0.25;0.48;0.67];
y = [0.77;0.55;0.39;0.22;0.21];
spcv = cscvn([x, y].');
fnplt(spcv); % plot the full curve
hold on;
fnplt(spcv,[0.3,0.4],'r'); % plot the "[0.3, 0.4] part" in RED
fnplt(spcv,[1,2],'g'); % plot the "[1, 2] part" in GREEN
gives me this
How do I make sense of the results?
The spline, although constructed from known x and y points, is no longer a function of x and y. Instead, it is a function of the arc length along the spline. So the vector that you provide as the second input to fnplt is the range of that parameter and not x and y.
If you want to determine the correct ranges to pass to fnplt such that each piece of the spline (between successive input points) is a different color, you can use the output of cscvn to determine the values of the parameter at each input point.
spcv = cscvn([x, y].');
% form: 'pp'
% breaks: [0 0.4693 0.9037 1.4385 1.8746] <---- These are the parameter values
% coefs: [8x4 double]
% pieces: 4
% order: 4
% dim: 2
Specifically, we can use the breaks field to determine the parameter value corresponding to each input point and we can loop through these values to plot each section independently with fnplt.
colors = 'rgbc'
for k = 1:(numel(x) - 1)
fnplt(spcv, spcv.breaks([k k+1]), colors(k));
hold on
end

Customizing multiple rootlocus plot colors (scale of grey) Matlab

I would like to customize the color of my rootlocus plot.
I use a for cycle to plot 10 rootlocus (with slightly different systems in the loop) and I would like every of them to be of a different shade of grey. I thought to use the gray command to obtain a matrix to store the RGB data and then use this matrix in the rlocus(sys,K,'style') command (choosing the i-th line at the i-th iteration of my cycle). Unfortunately the command requires the style to be a cell (for example 'g' or 'b') and not a vector of numbers.
This is a sample of my code:
figure()
hold on
L = [sys1, sys2, ..., sys10];
colors = gray(10);
for i = 0:9
rlocus (L(i+1), 'Color', colors(i+1, :));
end
The rlocus() function is not as powerful as the plot() function and only has limited support for setting colours with rlocus(sys, 'b') as you've noticed. However, we can combine it with the plot() function to make use of its power.
Here I use [R, K] = rlocus(sys) to return the values of the root locus, R. Each row of R represents a different trajectory. We can plot 1 trajectory of the root locus with plot(R(m, :)) and utilise the strength of plot() to change the colour however we wish.
L = [sys1, sys2, sys3, sys4, sys5, sys6, sys7, sys8, sys9, sys10];
C = gray(numel(L) + 1); % Extra 1 because the last value will be
% white and plotting white on white does
% not look well :P
figure;
hold on
for n = 1:numel(L)
[R, K] = rlocus(L(n));
for m = 1:numel(R)/length(R)
plot(R(m, :), 'Color', C(n, :));
end
end
hold off

Matlab contourf

If I'm asked to plot a function S with "level lines" abs(S) = 0:0.1:1, how do I do that?
I looked up the solution:
[X,Y] = meshgrid(-15:0.1:15);
Z = X + i*Y;
contourf(X,Y,abs(S),[1 1]);
where they pass in the fourth argument [1 1] but I've no idea what it's doing. Why do they pass in [1 1] if they ask me for lines between 0 and 1?
Any help is much appreciated!
They're making a mistake.
The help to contourf states
To draw a single contour of level i, use contour(Z,[i i])
So they're drawing a single contour line at 1.
You want to write
contourf(X,Y,abs(S),0:0.1:1);
because the help says
contourf(Z,v) draws a filled contour
plot of matrix Z with contour lines at
the data values specified in the
monotonically increasing vector v. The
number of contour levels is equal to
length(v)