Generating a 3D plot by revolution of a curve - matlab

I'm trying to revolve a 2D curve to generate a 3D surface plot.
I've tried using
[X,Z,Y] = cylinder(u);
surf(X,Y,Z), axis square
this, however, revolves my curve around the wrong axis. How do I go about changing the axis?
Thanks alot.

To rotate the axis of the cylinder, you can simply change the order of X, Y, and Z.
[X,Y,Z] = cylinder(u);
surf(X,Y,Z) %# rotation around Z
surf(Z,X,Y) %# rotation around X
surf(Y,Z,X) %# rotation around Y
EDIT
To change the axis of rotation of your curve, you have to calculate the surface. For example, to rotate y = sin(alpha) with alpha = 0:0.1:pi around the y-axis, you can write
r = 0:0.1:pi;
z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(#times,r',cos(theta));
yy = bsxfun(#times,r',sin(theta));
zz = repmat(z',1,length(theta));
dfig,surf(xx,yy,zz)
axis equal

Related

Rotate image around world x axis

Having this coordinate system:
And this dominant vertical vanishing point:
I would like to rotate the image around x axis so the vanishing point is at infinity. That means that all vertical lines are parallel.
I am using matlab. I find the line segmentes using LSD and the vanishing point using homogeneous coordinates. I would like to use angle-axis representation, then convert it to a rotation matrix and pass this to imwarp and get the rotated image. Also would be good to know how to rotate the segments. The segments are as (x1,y1,x2,y2).
Image above example:
Vanishin point in homogenous coordinates:
(x,y,z) = 1.0e+05 * [0.4992 -2.2012 0.0026]
Vanishin point in cartesian coordinates (what you see in the image):
(x,y) = [190.1335 -838.3577]
Question: With this vanishing point how do I compute the rotation matrix in the world x axis as explained above?
If all you're doing is rotating the image so that the vector from the origin to the vanishing point, is instead pointing directly vertical, here's an example.
I = imread('cameraman.tif');
figure;imagesc(I);set(gcf,'colormap',gray);
vp=-[190.1335 -838.3577,0]; %3d version,just for cross-product use,-ve ?
y=[0,1,0]; %The vertical axis on the plot
u = cross(vp,y); %you know it's going to be the z-axis
theta = -acos(dot(vp/norm(vp),y)); %-ve ?
rotMat = vrrotvec2mat([u, theta]);
J=imwarp(I,affine2d (rotMat));
figure;imagesc(J);set(gcf,'colormap',gray); %tilted image
You can play with the negatives, and plotting, since I'm not sure about those parts applying to your situation. The negatives may come from plotting upside down, or from rotation of the world vs. camera coordinate system, but I don't have time to think about it right now.
EDIT
If you want to rotation about the X-axis, this might work (adapted from https://www.mathworks.com/matlabcentral/answers/113074-how-to-rotate-an-image-along-y-axis), or check out: Rotate image over X, Y and Z axis in Matlab
[rows, columns, numberOfColorChannels] = size(I);
newRows = rows * cos(theta);
rotatedImage = imresize(I, [newRows, columns]);

Estimating an ellipse with a multi-variate Gaussian

In MATLAB, say I have the parameters for an ellipse:
(x,y) center
Minor axis radius
Major axis radius
Angle of rotation
Now, I want to generate random points that lie within that ellipse, approximated from a 2D gaussian.
My attempt thus far is this:
num_samps = 100;
data = [randn(num_samps, 1)+x_center randn(num_samps, 1)+y_center];
This gives me a cluster of data that's approximately centered at the center, however if I draw the ellipse over the top some of the points might still be outside.
How do I enforce the axis rules and the rotation?
Thanks.
my assumptions
x_center = h
y_center = k
Minor Axis Radius = b
Major Axis Raduis = a
rotation angle = alpha
h=0;
k=0;
b=5;
a=10;
alpha=30;
num_samps = 100;
data = [randn(num_samps, 1)+h randn(num_samps, 1)+k];
chk=(((((data(:,1)-h).*cos(alpha)+(data(:,2)-k).*sin(alpha))./a).^2) +...
(((data(:,1)-h).*sin(alpha)+(data(:,2)-k).*cos(alpha))./b).^2)<=1;
idx=find(chk==0);
if ~isempty(idx)
data(idx,:)=data(idx,:)-.5*ones(length(idx),2);
end

Matlab: 2D smooth curve coordinates and animated

I need to plot a 2D smooth curve in Matlab. I have found this
npts = 10;
xy = [randn(1,npts); randn(1,npts)];
plot(xy(1,:),xy(2,:),'ro','LineWidth',2);
text(xy(1,:), xy(2,:),[repmat(' ',npts,1), num2str((1:npts)')])
ax = gca;
ax.XTick = [];
ax.YTick = [];
hold on
fnplt(cscvn(xy),'r',2)
hold off
This is the kind of curve I am looking for. But I need the exact coordinates of the curve (as many as possible x,y points) and to show the curve in a animated way,controlling the speed. Also, I need to plot on the line a circle or any other object following the trajectory (I need to control the speed the circle follows the line).
thanks

How translate a point on a circle?

I am trying to create a circle from the given radius and translate the circle.
edia = 10; %diameter
theta=linspace(0,2*pi, 100); %100 evenly spaced points.
radius = edia./2;
x = radius.*cos(theta);
y = radius.*sin(theta);
plot(x,y, 'k')
axis equal
axis([-edia, edia, -edia, edia]);
After creating a circle using the code, I have to translate it but I have no idea how to do it.
This is the circle that I have
and this is what I am suppose to get after translating
Thank you.
for this simple case, just add the shift directly.
close all
edia = 10; %diameter
theta=linspace(0,2*pi, 100); %100 evenly spaced points.
radius = edia./2;
x = radius.*cos(theta);
y = radius.*sin(theta);
plot(x,y, 'k')
axis equal
axis([-edia, edia, -edia, edia]);
newX=3; newY=4;
hold on;
plot(x+newX,y+newY, '-.')

How to draw a circle with perspective transformation

I want to draw a circle with perspective tansformation in Matlab. Here I have used sample of code to draw a circle. If I rotate this circle using perspective tansform this must be ellipse. I want to draw both axis rotation(X & Y) of a circle. Now I am struggled to draw a perspetive rotation of this circle.
I need to use radious of the circle, x axis roation angle and y axis roation angle as input parameters.
Rimg = zeros(600,600);
ri = 100;
xcentre = 300;
ycentre = 300;
for r = 0:ri
for rad = 0:(pi/720):(2*pi)
xi = round(xcentre+(r*cos(rad)));
yi = round(ycentre+(r*sin(rad)));
Rimg(xi,yi) = 1;
end
end
imshow(Rimg,[]);
The first image shows my circle and the second image shows the expected result. This is only rotated in x axis. But I need both axis rotation in a single image.