Shade a vertical strip Area in a MATLAB Plot - matlab

I need to shade a vertical strip area in a MATLAB plot between to vertical lines.
I need to shade the part covered enclosed by the BLACK Curve, RED, BLUE & GREEN Lines.
I have tried the example from Here
If the data for the plot is required, please let me know i will upload.

I think this is what you are looking for:
% some arbitrary data
x = -10:0.1:10;
y = abs(x).^0.5;
xleft = 0.5;
xright = 4;
fillStart = find(x>=0.5,1);
fillEnd = find(x>=4,1);
top = 2.5;
% plotting the lines
plot(x,y,'k',...
x,ones(1,length(x))*top,'r',...
ones(1,length(y)).*xleft,y,'g',...
ones(1,length(y)).*xright,y,'b')
hold on
% filling the area
area(x(fillStart:fillEnd),y(fillStart:fillEnd),top, ...
'EdgeColor', 'none', 'FaceColor', [0.5 0.5 0.5],'ShowBaseLine','off')
hold off
Which create this:

While not exactly what you are after, (you need the equations for your respective lines) something like this should work
x = -5:0.1:5;
y = sqrt(abs(x));
figure
hold on
fill([2, 4, 4, 2], [0, 0, 2, 2], 'g')
plot(x,y)
From the fill documentation
fill(X,Y,C) fills the 2-D polygon defined by vectors X and Y
with the color specified by C. The vertices of the polygon
are specified by pairs of components of X and Y. If necessary,
the polygon is closed by connecting the last vertex to the first.

Related

How to implement a 3D boolean operation in MATLAB to make an intersection like in Blender (or any other 3D software)?

I am working on creating 3D images from 3 individual binary images, that were taken with 3 cameras. I have a corresponding calibration and know the setup (see below). Since the image preprocessing is mostly done in MATLAB I would like to implement everything there.
The current idea of my code is to extrude the 2D binary image according to the camera calibration. Here's what a typical binary image looks like:
And an extruded image looks like this in MATLAB:
With all 3 cameras extruded and a binning algorithm, I can create my final 3D-shape. This works fine so far, but takes a long time to compute, since I need to create a lot of extrusion steps to get a good surface.
I was thinking now to speed this up by recreating the process I would do in a 3D-modeling software like Blender. There I also extrude the outline of the binary image and create an intersection easily by just creating a spline for the outline, extrude them and use a boolean operator. Here's a Blender example with 2 extruded images:
I have no idea how to implement something like this in MATLAB. I wanted to create two instances of my binary contour at the top and bottom end of the extrusion "tube" and then define faces between the individual points and create an intersection afterwards. The point creation is no problem but the face definition and the intersection (boolean operator) are. Does anyone have an idea how this could be implemented?
This might not be an easy thing to do in MATLAB, but it's possible. I'll outline one set of steps here, using two intersecting cylinders as an example...
Creating a tetrahedral mesh:
The first step is to create a tetrahedral mesh for your extrusion. If your 2D binary image that you're extruding is convex and has no holes, you could do this using the delaunayTriangulation function:
DT = delaunayTriangulation(P);
Here, P contains the coordinate points of the "end caps" of your extrusion (i.e. the faces on each end of your tube). However, when generating tetrahedral meshes, delaunayTriangulation doesn't allow you to specify constrained edges, and as such it can end up filling in holes or concavities in your extrusion. There may be some better mesh-generation alternatives in other toolboxes, such as the Partial Differential Equations Toolbox, but I don't have access to them and can't speak to their applicability.
If automated mesh-generation options don't work, you'll have to build your tetrahedral mesh yourself and pass that data to triangulation. This could be tricky, but I'll show you some steps for how you can do this for a cylinder, which may help you figure it out for more involved shapes. Below, we build a set of coordinate points P1 and an M-by-4 matrix T1 where each row contains indices into the rows of P1 which define one tetrahedron:
% Create circle coordinates for the end caps:
N = 21;
theta = linspace(0, 2*pi, N).';
x = sin(theta(1:(end-1)));
y = cos(theta(1:(end-1)))+0.5;
z = ones(N-1, 1);
% Build tetrahedrons for first cylinder, aligned along the z axis:
P1 = [0 0.5 -1; ... % Center point of bottom face
x y -z; ... % Edge coordinates of bottom face
0 0.5 1; ... % Center point of top face
x y z]; % Edge coordinates of top face
cBottom = ones(N-1, 1); % Row indices for bottom center coordinate
cEdgeBottom1 = (2:N).'; % Row indices for bottom edge coordinates
cEdgeBottom2 = [3:N 2].'; % Shifted row indices for bottom edge coordinates
cTop = cBottom+N; % Row indices for top center coordinate
cEdgeTop1 = cEdgeBottom1+N; % Row indices for top edge coordinates
cEdgeTop2 = cEdgeBottom2+N; % Shifted row indices for top edge coordinates
% There are 3 tetrahedrons per radial slice of the cylinder: one that includes the
% bottom face and half of the side face (all generated simultaneously by the first row
% below), one that includes the other half of the side face (second row below), and one
% that includes the top face (third row below):
T1 = [cEdgeBottom1 cEdgeBottom2 cEdgeTop1 cBottom; ...
cEdgeBottom2 cEdgeTop1 cEdgeTop2 cBottom; ...
cEdgeTop1 cEdgeTop2 cTop cBottom];
TR1 = triangulation(T1, P1);
To better visualize how the cylinder is being divided into tetrahedrons, here's an animation of an exploded view:
Now we can create a second cylinder, offset and rotated so it aligns with the x axis and intersecting the first:
% Build tetrahedrons for second cylinder:
P2 = [P1(:, 3) -P1(:, 2) P1(:, 1)];
T2 = T1;
TR2 = triangulation(T2, P2);
% Plot cylinders:
tetramesh(TR1, 'FaceColor', 'r', 'FaceAlpha', 0.6);
hold on;
tetramesh(TR2, 'FaceColor', 'g', 'FaceAlpha', 0.6);
axis equal;
xlabel('x');
ylabel('y');
zlabel('z');
And here's the plot to visualize them:
Finding the region of intersection:
Once we have tetrahedral representations of the volumes, we can generate a grid of points covering the region of intersection and use the pointLocation function to determine which points are within both cylinders:
nGrid = 101;
[X, Y, Z] = meshgrid(linspace(-1, 1, nGrid));
QP = [X(:) Y(:) Z(:)];
indexIntersect = (~isnan(pointLocation(TR1, QP))) & ...
(~isnan(pointLocation(TR2, QP)));
mask = double(reshape(indexIntersect, [nGrid nGrid nGrid]));
We now have volume data mask that contains zeroes and ones, with the ones defining the region of intersection. The finer you make your grid (by adjusting nGrid), the more accurately this will represent the true region of intersection between the cylinders.
Generating a 3D surface:
You may want to create a surface from this data, defining the boundary of the intersection region. There are a couple ways to do this. One is to generate the surface with isosurface, which you could then visualize using featureEdges. For example:
[F, V] = isosurface(mask, 0.5);
TR = triangulation(F, V);
FE = featureEdges(TR, pi/6).';
xV = V(:, 1);
yV = V(:, 2);
zV = V(:, 3);
trisurf(TR, 'FaceColor', 'c', 'FaceAlpha', 0.8, 'EdgeColor', 'none');
axis equal;
xlabel('x');
ylabel('y');
zlabel('z');
hold on;
plot3(xV(FE), yV(FE), zV(FE), 'k');
And the resulting plot:
Another option is to create a "voxelated" Minecraft-like surface, as I illustrate here:
[X, Y, Z, C] = build_voxels(permute(mask, [2 1 3]));
hSurface = patch(X, Y, Z, 'c', ...
'AmbientStrength', 0.5, ...
'BackFaceLighting', 'unlit', ...
'EdgeColor', 'none', ...
'FaceLighting', 'flat');
axis equal;
view(-37.5, 30);
set(gca, 'XLim', [0 101], 'YLim', [25 75], 'ZLim', [0 102]);
xlabel('x');
ylabel('y');
zlabel('z');
grid on;
light('Position', get(gca, 'CameraPosition'), 'Style', 'local');
And the resulting plot:

Draw error bars in Matlab

I have created an example scatter plot with five points in MATLAB as follows:
x = linspace(0,pi,5);
y = cos(x);
scatter(x,y);
In my case, the y-value of each point shall be in a predefined range defined as follows:
y_limits = {[0.9 1.1], [0.6 0.8], [-0.1 0.1], [-0.8 -0.6], [-1.1 -0.9]};
So for example, the y value of point 1 at x = 0 shall be in the range [0.9 1.1].
I would somehow like to draw five vertical boundaries nicely in the same plot perhaps by means of
five vertical lines with endpoints at the respective two limits
five filled vertical areas between the respective two limits
something else that may be more appropriate
I would like to get some suggestions or sample code of people who are more experienced than me in such kind of graphical representations.
You can do this in one line by using the errorbar function
% Your example variables
x = linspace(0,pi,5)';
y = cos(x);
y_limits = [0.9, 1.1; 0.6, 0.8; -0.1, 0.1; -0.8, -0.6; -1.1, -0.9];
% Plot
errorbar(x, y, y - y_limits(:,1), y_limits(:,2) - y, 'x');
% Format: (x, y, negative error, positive error, point style)
Result:
Edit, you can set the line properties of the plot as you call errorbar. For example, you could use larger, blue circle markers with thicker, red error bars
using:
errorbar(x, y, y - y_limits(:,1), y_limits(:,2) - y, 'o', 'MarkerSize', 2, 'MarkerFaceColor', 'b', 'MarkerEdgeColor', 'b', 'Color', 'r', 'LineWidth', 1);
Note for these images I'm using grid on to add the grid. Second result:
Creating a lines is done with the line command.
for i = 1:length(x)
line([x(i) x(i)], [y_limits{i}]);
end
Filled areas can be done with patch or fill. Some reordering of the limits is necessary so that the order given follows a path around the area to be filled. One nice trick is to use the alpha command on those filled areas to create transparency.
hold on
y_corners = reshape([y_limits{:}], 2, length(x)).'; %make an array
y_corners = [y_corners(:,1); flipud(y_corners(:,2))]; %corners follow a path around the shape
fill([x fliplr(x)], y_corners, 'blue');
alpha(0.5);

Filling in rectangles drawn with the patch function

I am wanting to draw rectangles on a figure that I am creating. I also would like to be able to fill that rectangle with a transparent color. So I am using 'Patch' instead of 'Rectangle' which will allow me to do so.
I am able to get the rectangles in the location and the size that I want, but I cannot seem to get the filling in with transparency the way that I think it should work?
In the documentation, it is of the form:
p=patch(X,Y, 'c')
When I enter this in, it never takes into account that Color denoted by 'c'/cyan, and will just plot a standard black rectangle. However, I can set the color with the dot index notation of:
p.EdgeColor='c';
My actual script is different as the X's and Y's are variable, but I am attaching a very simple script here to simulate my issue of my vectorized plotting issue. It simply creates Xs vertices at every 10th location, with a width of 2, and a height of 10.
Am I misusing the patch function to get it to fill with a transparent color with FaceAlpha?
clear all;
% Generate some simple X value data, where each X is the X value starting from the
% lower left of the rectangle, in a clockwise fashion around the edges of
% rectangle.
X1(:,1) = 0:10:100;
X2(:,1) = 0:10:100;
X3(:,1) = X1+2;
X4(:,1) = X1+2;
% Rotate the X vectors into rows.
X1 = X1(:)';
X2 = X2(:)';
X3 = X3(:)';
X4 = X4(:)';
% Here I am stacking each X on top of each other, and padding the bottom
% row with NaNs. Adding a similar Y set of values, which will simply be a
% height of 0-10
X =[X1; X2; X3; X4;
nan(size(X1)) ];
Y =[zeros(size(X1)); 10+zeros(size(X2)); ...
10+zeros(size(X3)); zeros(size(X4));
nan(size(X1)) ];
% Rotate vectors so that the last value in the X/Y coordinates is a NaN,
% and Matlab will ignore and plot the next value
X=X(:);
Y=Y(:);
% This Plots the outline of the rectangles I am looking for, with a width
% of 2, height of 10, starting at each 10th value of X
p=patch( X ,Y,'c');
p.EdgeColor = 'b'; %Comment this out to see that the Patch function is not working?
p.FaceColor = 'blue';
p.FaceAlpha = 0.5;
% But for some reason it is not FILLING in the rectangles/patches I have
% drawn?
If you want to create multiple rectangles, you don't want to put the NaN values to separate them, you instead want to put each rectangle's coordinates in a separate column.
X = [X1; X2; X3; X4];
Y =[zeros(size(X1)); 10+zeros(size(X2)); ...
10+zeros(size(X3)); zeros(size(X4))];
patch(X, Y, 'c')
Also, the third input to patch specifies the color of the faces (FaceColor) but the default EdgeColor is going to be black.
You can specify the parameter/value pairs as additional inputs to patch to specify the desired value for any other parameters (such as EdgeColor).
patch(X, Y, 'c', 'EdgeColor', 'c', 'FaceAlpha', 0.5)
Your problem is not with the color but with X and Y, they should be a 4-by-n matrix. Here is a way to do this:
x = [0:10:100; (0:10:100)+2];
X = reshape(repelem(x(:),2),4,[]);
Y = repmat([0; 10; 10; 0],1,size(X,2));
p = patch(X,Y,'b','EdgeColor','c', 'FaceAlpha', 0.5);

How do I create a polar plot with concentric colored rings corresponding to single values in Matlab?

I am trying to create a plot that looks like this with rings of constant values (colors) extending from 0 to 100 in 10 unit increments.
Rings of single values extending outward from center
However, my code is not producing this, and I do not know where it has gone wrong.
% values representing the colors that each ring should be
% starting from the center and moving outwards in 10 unit increments.
values = [364,358,354,348,339,335,330,325,320,310];
xCoord = linspace(0,2*pi,10);
yCoord = linspace(0,100,10);
[TH,R] = meshgrid(xCoord,yCoord);
[X,Y] = pol2cart(TH,R);
[Z] = meshgrid(values);
contour_ticks = 300:5:375;
figure
hold on
contourf(X,Y,Z,contour_ticks);
a=gca; cb=colorbar; colormap('jet'); caxis([300 375]);
This produces a plot resembling this:
Incorrect plot
Any ideas what I'm doing wrong? Any help is greatly appreciated. Thanks.
If you just want to plot circles, you can use the following approach:
radii = 100:-10:10; %// descending order, so that bigger circles don't cover small ones
colors = parula(numel(radii)); %// or use some other colormap
for n = 1:numel(radii)
r = radii(n);
rectangle('Position', [-r -r 2*r 2*r], 'Curvature', [1 1], 'FaceColor', colors(n,:),...
'EdgeColor', 'none') %// plot each circle using sequential colors, no edge
hold on
end
axis equal
axis([-1 1 -1 1]*max(radii))

Rotating a Plot About the Y Axis

I have a vector of values that I want to plot as brightness on a circle through the radius of it (I.e. If it was 0 3 1 5 I'd want a circle that was dark at the centre, then a bright ring around it, then a slightly darker ring, then a brighter ring).
To do this I've attempted to rotate my radial vector (E) around the y axis, as such
[X,Y,Z] = cylinder(E);
h = surf(X,Y,Z),
However I'm clearly not doing it right, as this appears to be rotating my curve around the x axis. I've tried just swapping X and Y, but it still rotates it around the x axis. Any help would be greatly appreciated.
One way would be to rotate your vector and create a surface. The Z data of the surface (your rotated vector) will be color coded according to the colormap you choose, if you display the surface from the top you get your circles at the different brightness.
If you are really only interested from the "top view" of this surface, then no need to create a full surface, a simple pcolor will do the job.
example:
%% // input data (and assumptions)
E=[0 3 1 5 2 7];
nBrightness = 10 ; %// number of brightness levels
r = (0:numel(E)) ; %// radius step=1 by default for consecutive circles
%// otherwise define different thickness for each circle
So if I use stairs([E 0]) you get your different brightness levels:
I had to add a last 0 to the vector to "close" the last level, we'll have to do that again in the solution below.
Now to rotate/replicate that around Y, color code the height, and look at it from the top:
%% // replicate profile around axis
ntt = 50 ; %// define how many angular division for the plot
theta = linspace(0,2*pi,ntt) ; %// create all the angular divisions
[rr,tt]=meshgrid(r,theta) ; %// generate a grid
z = repmat( [E 0] , ntt , 1 ) ; %// replicate our "E" vector to match the grid
[xx,yy,zz] = pol2cart(tt,rr,z) ; %// convert everything to cartesian coordinates
pcolor(xx,yy,zz) %// plot everything
colormap(gray(nBrightness)) %// make sure we use only "nBrightness" colors (Shades of gray)
caxis([0 nBrightness])
shading flat ; axis equal %// refine the view (axis ratio and "spokes" not visible) etc...
colorbar
axis off
will yield the following :
Note that your problem was not fully defined, I had to take assumptions on:
What radius each brightness circle should have ? (I made them all the same but you can modify that)
How many brightness levels you want ? (You can also modify that easily though).
Have you tried the rotate function?
direction = [0 1 0];
rotate(h,direction,90);
In this example a 90 degree rotation is performed around the y axis.
Using this library http://www.mathworks.com/matlabcentral/fileexchange/45952-circle-plotter
%http://www.mathworks.com/matlabcentral/fileexchange/45952-circle-plotter
x0 = 0;
y0 = 0;
colors = [0 3 1 5];
maxC = max(colors);
sz = numel(colors);
for i=fliplr(1:sz)
c = colors(i);
circles(x0,y0,i,'facecolor',[c/maxC c/maxC 0]) % http://au.mathworks.com/help/matlab/ref/colorspec.html
end