Related
Trisurf(f,x,y,z) function can draw a patch object. How can I add datatip to this object at the known vertices index with code?
I tried to use the following codes in 2017b
cursorMode = datacursormode(gcf);
hdtip = cursorMode.createDatatip(h_surf); %h_surf is the handle of trisurf
hdtip.Cursor.Position=pos; %this commond seems cannot find the exact vertex and depends on the view angle, where pos is the vertex coordinates.
hdtip.Cursor.DataIndex=idx; %try to use this command to assign the vertex index, but not successful.DataIndex seems not the vertex index.
Accordint to #Woffie's suggestions, I tuned a little bit to reproduce the error with different view angle in 2017b and 2019b.
First to enable view([180,0]); line. Run the code to get the correct result. And then without closing the figure,comment this line, only remain view([0,0]); to get the wrong result. But if add Close all to the front of the script, should get the correct result each time. (But still don't know the reason, whether it's a bug or not and how to avoid it smartly.)
It might be good to note, in 2D plots, this usually is not a problem since one doesn't usually change the view.
ngrid=1000;%actualy doesnot matter.
x=linspace(-3,3,ngrid+1);
y=linspace(-3,3,ngrid+1);
[x,y] = meshgrid(x,y);
tri = delaunay(x,y);
z = peaks(x,y);
x=(x+3)/6;y=(y+3)/6;
h_surf = trisurf(tri,x,y,z,'EdgeAlpha',0.1);
x0=0.25;y0=0.44;%the above normlization are not necessary but easier to setup the query node.
z0=z(round(y0*ngrid)+1,round(x0*ngrid)+1);
pos=[x0,y0,z0];
view([0,0]);
view([180,0]); %toggle between these two view angles to see the difference.
cursorMode = datacursormode(gcf);
hdtip = cursorMode.createDatatip(h_surf);
hdtip.Cursor.Position = pos;
hdtip.Cursor.Position
BTW, I also figured out how to use hdtip.Cursor.DataIndex=idx. Basically, DataIndex here is the linear index for h_surf.XData(or YData or ZData), while the XData, YData, ZData are mxn matrice as the node/vertex coordinates of all faces in h_surf, where m is the number of nodes in each face and n is the number of the faces. So it will be straightforward to convert to node index into DataIndex.
I'll use the trisurf example from the docs to create a mesh:
[x,y] = meshgrid(1:15,1:15);
tri = delaunay(x,y);
z = peaks(15);
h_surf = trisurf(tri,x,y,z);
Now the tri matrix contains the indices within x, y, and z for each vertex in the mesh. We can choose a vertex index idx, and the corresponding value from each of the x/y/z arrays to use as the .Position property of your data cursor. Contrary to your comment, the Position property does not depend on the view angle, it is the position relative to the axes (and therefore your data).
So
idx = 123; % intex within triangulation "tri"
xcurs = x(tri(idx)); % = 5 in this example
ycurs = y(tri(idx)); % = 6 in this example
zcurs = z(tri(idx)); % = 0.65233 in this example
cursorMode = datacursormode(gcf);
hdtip = cursorMode.createDatatip(h_surf);
hdtip.Cursor.Position = [xcurs,ycurs,zcurs];
You could determine idx however you want, then use a common idx to extract the aligned x, y, and z coordinates.
Two different views to show the data tip stays in place:
This example was run using MATLAB R2017b.
I try to interpolate curves in the gaps of some already existing curves using matlab (and the interp1-function).
Start of edit 1
I already have the data of 5 Torque over rpm curves that I obtained with a number of simulations for each curve. Since simulation time is precious, I would like to save time with the interpolation of curves that "fill the gap" between the already existing ones.
I'm looking to form the following:
Further thoughts of me down below.
End of edit 1
I tried to follow the steps from the question in the thread Interpolation between two curves (matlab) but it does not seem to work with my code. I am not sure if the code is actually applicable since the curves might overlap...
I tried to edit the code from the link above like the following:
% Save the data in one array each. The original data is stored in the
% arrays "x/yOriginal" row-wise.
curve1_data = [xOriginal(:,1) yOriginal(:,1)];
curve2_data = [xOriginal(:,2) yOriginal(:,2)];
% This was to try if the code from the link works
yy = [0:1:60];
xx1 = interp1(curve1_data(:,2),curve1_data(:,1),yy,'spline');
xx2 = interp1(curve2_data(:,2),curve2_data(:,1),yy,'spline');
m = 3; % Curve_Offset
mm(:,m) = xx1 + (xx2-xx1)*(m/(8750-5000));
% With the following I tried to interpolate over x
xx = (xOriginal(1,1):1:xOriginal(1,2));
yy1 = interp1(curve1_data(:,1),curve1_data(:,2),xx,'spline');
yy2 = interp1(curve2_data(:,1),curve2_data(:,2),xx,'spline');
m = 3; % Curve_Offset
% From the original code:
mm(:,m) = xx1 + (xx2-xx1)*(m/(8750-5000));
% Interpolation over x
yINT = yy1 + (yy2-yy1)*(m/8750-5000);
None of the interpolation-techniques worked, the y-values are either 90% negative (with the code from the link) or way too high (10e8 with the interpolation over x).
What I expected was that it creates a curve a little less steep than the curve "to its left" and a bit steeper than the curve "to its right".
My further thoughts:
The existing curves are the product of big 3-dimensional arrays. I.e. it could be that it is more the way to go to interpolate the arrays and then "read out" the Torque-over-rpm-Curves. On the other hand, I don't see a way to interpolate between two 1001-by-7001-by-5 arrays...
Moreover, for the next steps with the programm, the curve-interpolation needs to be quite fine (it is necessary to have way more than 1 interpolated curve between two existing curves) which makes the problem even more difficult.
If I understand right, the plot is generated with something like this:
plot(xOriginal(:,1),yOriginal(:,1))
plot(xOriginal(:,2),yOriginal(:,2))
% ...
If so, you can plot an intermediate curve with
plot((xOriginal(:,1) + xOriginal(:,2))/2, (yOriginal(:,1) + yOriginal(:,2))/2)
That is, the average between each pair of coordinates forms a curve that is exactly half-way between the two original curves.
Use weighted averages to generate more of these curves. This is linear interpolation.
d = 0.2;
plot(d*xOriginal(:,1) + (1-d)*xOriginal(:,2), d*yOriginal(:,1) + (1-d)*yOriginal(:,2))
Setting d = 0.5 we go back to the half-way case above.
Example:
xOriginal(:,1) = linspace(0.2,0.5,100);
yOriginal(:,1) = 3 * cos(xOriginal(:,1)*10-2.5) + 3;
xOriginal(:,2) = linspace(0.6,0.8,100);
yOriginal(:,2) = cos(xOriginal(:,2)*20-13.5) + 1;
clf; hold on
plot(xOriginal(:,1),yOriginal(:,1))
plot(xOriginal(:,2),yOriginal(:,2))
plot((xOriginal(:,1)+xOriginal(:,2))/2,(yOriginal(:,1)+yOriginal(:,2))/2)
(the interpolated line is in orange)
I am trying to transcribe one of my scripts into App Designer in Matlab so that I easily distribute it to students. The idea is to show forces and moments acting in 6 degrees of freedom on a vehicle in real time (based on user input through a joystick). I am using a very simplistic animation made of multiple 3D arrow plots at the moment (which refreshes every 0.1 s), but it does the job. What I would like to get is shown in the first figure, whereas the second figure shows what I actually get in App Designer.
The relevant lines of code in App Designer which I use for the plotting are as follows:
function plot_ROV(app)
% Plot the forces and moments acting on the ROV in the correct graph:
quiver3(app.UIAxes,0,0,0,1,0,0);
hold(app.UIAxes);
quiver3(app.UIAxes,0,0,0,0,1,0);
hold(app.UIAxes);
quiver3(app.UIAxes,0,0,0,0,0,1);
hold(app.UIAxes);
quiver3(app.UIAxes,0,0,0,app.Surge,0,0,'LineWidth',5,'Color',app.Colors(1,:));
hold(app.UIAxes);
quiver3(app.UIAxes,0,0,0,0,app.Sway,0,'LineWidth',5,'Color',app.Colors(2,:));
hold(app.UIAxes);
quiver3(app.UIAxes,0,0,0,0,0,app.Heave,'LineWidth',5,'Color',app.Colors(3,:));
hold(app.UIAxes);
app.circular_arrow3([1,0,0],app.Roll,0.2,app.Colors(4,:));
hold(app.UIAxes);
app.circular_arrow3([0,1,0],app.Pitch,0.2,app.Colors(5,:));
hold(app.UIAxes);
app.circular_arrow3([0,0,1],app.Yaw,0.2,app.Colors(6,:));
hold(app.UIAxes);
legend(app.UIAxes,'x-axis','y-axis','z-axis','surge','sway','heave','roll','roll',...
'pitch','pitch','yaw','yaw','Location','BestOutside');
end
function circular_arrow3(app,axis,angle,radius,color)
% Generate the data for the circle in 2D space:
np = 10; % no. points
a = linspace(0,angle*pi,np);
p = [radius.*cos(a);radius.*sin(a);zeros(1,np)];
% Select the correct rotation matrix depending on the axis:
if sum((axis-[1,0,0]).^2)==0
R = [0,0,1;0,1,0;-1,0,0];
elseif sum((axis-[0,1,0]).^2)==0
R = [1,0,0;0,0,-1;0,1,0];
p(2,:) = - p(2,:);
elseif sum((axis-[0,0,1]).^2)==0
R = eye(3);
else
error('Only rotations about the x-, y- and z-axes are supported');
end
% Rotate the points:
pr = zeros(size(p));
for i=1:np
pr(:,i) = R*p(:,i);
end
% Calculate the difference between the last two points:
x = pr(1,end);
y = pr(2,end);
z = pr(3,end);
u = pr(1,end)-pr(1,end-1);
v = pr(2,end)-pr(2,end-1);
w = pr(3,end)-pr(3,end-1);
% Plot the points:
plot3(app.UIAxes,pr(1,:),pr(2,:),pr(3,:),'LineWidth',4,'Color',color);
hold(app.UIAxes);
quiver3(app.UIAxes,x,y,z,u,v,w,'LineWidth',6,'Color',color);
end
Now, by comparing the two figures, I think my problem is that the hold command is not working, at least not the way I intend it to: only the points from the last circular arrow are shown. Since I do not have much experience with App Designer, my feeling is I must have done a basic mistake.
Thanks in advance for the help!
In the plot_ROV function you call hold several times providing only one input parameter (namely the handle of the axes) that is without specifying the on property (hold(app.UIAxes);.
If the hold function is called in such a way, its effect is to toggle the property on / off in each call.
You can fix the issue either:
removing all the calls to hold butg the first one: just on call is enough to "add" subsequents items in the axes through the plotting functions
you can keep all the calls, but you have to specify the on property
hold(app.UIAxes,'on');
For more information about the hold function you can refer to the hold on-line documentation
I have been reading up quite a bit on Hough circles and how it can be used to detect the center and radius of a circular object. Although I understood how the transform works I am still not able to to understand how to get the center of a circle. The code snippet below is customized purely for coins.png in MATLAB. I tried it with a slightly more complex picture and it fails miserably. I am fixing the radius and then getting the accumulator matrix.
Code:
temp = accum;
temp(find(temp<40))=0; %Thresholding
imshow(temp,[0 max(temp(:))])
temp = imdilate(temp, ones(6,6)); %Dilating
imshow(temp,[0 max(max(temp))]);
temp = imerode(temp,ones(3,3)); %Eroding
imshow(temp,[0 max(max(temp))]);
s = regionprops(im2bw(temp),'centroid'); %Computing centroid
centroids = cat(1, s.Centroid);
I wanted to test the code out on a different picture and found this one on google. The Hough Transform produced a favorable result, but it's more overlapping than the previous case and my method fails.
Can someone suggest what the best method is to compute the centroid of the hough circle?
Original image:
Full Code:
%%Read and find edges using a filter
i = imread('coins4.jpg');
i = rgb2gray(i);
i = imresize(i,0.125);
i_f = edge(i, 'canny',[0.01 0.45]);
imshow(i_f)
%%
[x y] = find(i_f>0); % Finds where the edges are and stores the x and y coordinates
edge_index = size(x);
radius = 30; %Fixed radius value
theta = 0:0.01:2*pi;
accum = zeros(size(i,1), size(i,2)); %Create an accumulator matrix.
r_co = radius*cos(theta);
r_si = radius*sin(theta);
x1 = repmat(x, 1, length(r_co));
y1 = repmat(y, 1, length(r_si));
x_r_co = repmat(r_co, length(x),1);
y_r_si = repmat(r_si, length(y),1);
%% Filling the accumulator
a = x1 - x_r_co;
b = y1 - y_r_si;
for cnt = 1:numel(a)
a_cnt = round(a(cnt));
b_cnt = round(b(cnt));
if(a_cnt>0 && b_cnt>0 && a_cnt<=size(accum,1) && b_cnt<=size(accum,2))
accum(a_cnt,b_cnt) = accum(a_cnt,b_cnt) + 1;
end
end
imshow(accum,[0 max(max(accum))]);
%% Thresholding and get the center of the circle.
close all;
temp = accum;
temp(find(temp<40))=0;
imshow(temp,[0 max(temp(:))])
temp = imdilate(temp, ones(6,6));
imshow(temp,[0 max(max(temp))]);
temp = imerode(temp,ones(3,3));
imshow(temp,[0 max(max(temp))]);
s = regionprops(im2bw(temp),'centroid');
centroids = cat(1, s.Centroid);
imshow(i);
hold on;
plot(centroids(:,1), centroids(:,2),'*b')
If you want to stick with the 2D image you have now, this is a simple algorithm that might work for your first image since all the coins are about the same size and it isn't too cluttered:
find the global maximum of your accumulator array using [~,max_idx] = max(accum(:)); [i,j] = ind2sub(size(accum), max_idx];
Take a small neighbourhood around this pixel (maybe a square with a 10 pixel radius) and calculate its center of mass and get its index (this basically finds the middle of the bright rings you see)
add the center of mass pixel index to a list of circle centers
set all pixels in the small neighbourhood to zero (to prevent double-detection of the same pixel center)
repeat from step 1. until you reach approximately 70% or so of the original global max
If that doesn't work, I'd suggest taking the 3D accumulator approach, which is much better for coins of variable size.
The reason you are getting "donut" shapes for some of the coins rather than circles with intense bright points at the centers is because the radius you are assuming is a little off. If you were to explore the 3rd dimension of the accumulator (the radius dimension), you would find that these "donuts" taper to a point, which you could then detect as a local maximum.
This does require a lot more time and memory but it would lead to the most accurate result and isn't a big adjustment code-wise. You can detect the maxima using a method similar to the one I mentioned above, but in 3D and without the center-of-mass trick in step 2.
So, there is a function in matlab that does exactly what you want, called imfindcircles. You give an image and a range of possible radii, it outputs the computed radii and the centers of the circles. Some other parameters allow to tweak the computation (and in my experience, using them is necessary for good results).
Now, if you want to find the centers and radii yourself (not using matlab) then you want to use a maximum finder on the accumulation matrix. The concept of the Hough transform is that the maxima you find on the accumulation matrix each correspond to a circle (parametrized usually in (radius, x_center, y_center). The tough part here is that you have a gigantic searchspace and many many many many maxima that are not "real" circles but artefacts. I do not know how to reliably differentiate these fake circles of the real ones -- and I expect it's a rather complicated check to do.
Hope this helps!
I have 3D flow data of the velocity of a fluid through a tube. I know the diameter of the tube and have looked at the velocity field and found the centre of the field for an xy plane at both ends of the tube. So I essentially have a line through the centre axis of the tube. I want to NaN all data points that are outside of the diameter. For this I am using an equation that gives the distance to a point from a line in 3D which I found here mathworld.wolfram.com/Point-LineDistance3-Dimensional.html. I then created an if statement which states points smaller than diameter will be NaN.
I am new to matlab so I don't know how I would now plot this.
%%
diff_axis = end_axis-start_axis;
diff_axis_mag = (diff_axis(1)^2 + diff_axis(2)^2 + diff_axis(3)^2)^0.5;
[rw col pl] = size(X);
for j = 1:col
for i = 1:rw
for k = 1:pl
x_curr = X(i,j,k);
y_curr = Y(i,j,k);
z_curr= Z(i,j,k);
x0 = [x_curr y_curr z_curr]
t = - dot((start_axis-x0),(diff_axis))./(diff_axis_mag)^2;
d = sqrt(((start_axis(1) - x0(1)) + (end_axis(1) - start_end(1))*t)^2 + ((start_axis(2)-x0(2))+(end_axis(2)-start_end(2))*t)^2+((start_axis(3)-x0(3))+(end_axis(3)-start_end(3))*t)^2);
if (d > D)
x_curr=NaN
y_curr=NaN
z_curr=NaN
end
end
end
end
It were nice to have explanatory names for your X, Y, and Z. I am guessing they are flow components, and diff_axis are axis coordinates? It is a very cumbersome notation.
what you do in your loops is you take point values (X,Y,Z), copy them to temporary constants and then set them to NaN if they fall out. But the problem is that usually you do not plot point-by-point in MATLAB. So these temorary guys like x_curr will be lost.
Also, the most optimal way to do things in MATLAB is to avoid loops whenever possible.
What you can do is to create first a mask
%// remember to put a dot like in `.^` for entrywise array operations
diff_axis_mag = sqrt(diff_axis(1).^2 + diff_axis(2).^2 + diff_axis(3).^2);
%// are you sure you need to include the third axis?
%// then it is a ball, not a tube
%// create a binary mask
mask = diff_axis_mag < tube_radius
X(~mask) = NaN;
Y(~mask) = NaN;
Z(~mask) = NaN;
Then you can plot your data with quiver3 or
stream3