Finding transformation that transforms circle into ellipse - coordinates

I'm looking for a way to determine a specific transformation:
There is a circle as an equation like
x^2 + y^2 = z
and an ellipse as
x'^2 + px'y' + qy'^2 + r = 0.
(Notice that both the circle's and the ellipse's center is at 0|0 )
Given the values of z, p, q and r, how can you determine the parameters of the transformation matrix a, b, c and d for a transformation that transforms the circle into the ellipse?
And is it possible to transform the circle into any ellipse that way?

It is certainly possible to transform a circle into an ellipse. For example, using a non-uniform scaling with scaleX=2.0 and scaleY=1.0, you will get (a,b,c,d)=(2,0,0,1) and the ellipse equation will be (x'/2)^2+y'^2=z. However, the transformation matrix between a circle and an ellipse is not unique. For example, using a non-uniform scaling with scaleX=1.0 and scaleY=2.0, followed by a -90 degree rotation, you will get (a,b,c,d)=(0, 2, -1, 0) and the ellipse equation is still (x'/2)^2+y'^2=z.

Related

How to calculate the volume under two intersection surface?

I like to calculate the volume under the two intersection plane. The two plane is draw use this code.
P1 = [575,0,400];
P2 = [287.5,0,662];
P3 = [575,3500,154];
normal = cross(P1-P2, P1-P3)
syms x y z
P = [x, y, z]
ep1=dot(normal, P-P1)
% get the equation
Z = solve(ep1,z)
% draw the first plane
ezsurf(Z,[287.5,575,0,3500])
hold on
% draw the second horizontal plane
[x,y]=meshgrid(0:500:3500)
z = ones(8,8)*440
surf(x, y, z)
So I must calculate the volume under the first plane.
I used this code, but I don't know how to construct matrix Zm used the the symbols equation Z. And how can I use meshgrid and surf not ezsurf draw the first plane.
%f=#(x,y)(interp2(Zm,Xq,Yq))
% I want to calculate volume under the plane ranged by Xmin=2.875, Xmax=575,Ymin=0,Ymax=3500
%volume = quad2d(f,(287.5),575,0,3500)
%volume = integral2(f,287.5,575,0,3500)
Thanks a lot.
As an alternative strategy that might be easier to understand I would suggest, instead of interpolating, using some geometry formulas to calculate the area. You can break down the 3D shape into a simple triangular prism and an irregular tetrahedron. There are well defined generalized formulas for both of these.
http://mathcentral.uregina.ca/QQ/database/QQ.09.03/peter2.html

Matlab, visualize 3D rotational matrix

I have a target coordinate (xyz) within a 3D rectangle. The target coordinate is set assuming the 3D rectangle is in a flat plane with x,y,z. The target coordinate is set to be on one terminal end of the rectangle, with the 'y' component set to be -2 from the top of the 3D square (Refer to image). However, in reality the 3D rectangle is not in a flat plane with x,y,z axes, it is skewed. We calculate the skew by measuring two reference points on the top surface of the rectangle and now want to recalculate the target coordinate. I do not know how to ensure I am computing the skew correctly or (more importantly) plot the old vs new 3D rectangle and target coordinate. Below is code to generate both the matrix and the target within said matrix.
For the two reference points used to calculate the skew of the 3D rectangle, we name one 'bregma' and one 'lamda'.
%% CONVENTIONS:
%1) numbers get lower when
% 1) going to left on x axis
% 2) going down y axis
% 3) going posterior (or towards you) on z axis,
% opposite of convention
%2) coordinate order is AP (anterior posterior), ML
%(medial lateral), DV
%(dorsal ventral)
%Bregma is the anterior reference point and
%Lambda is posterior reference point . The goal variables
%are the point in space I want to reach. I'm hoping the code creates new
%points for me as caused by a change in elevation between point "c" and "f"
%Note, for AP/ML/DV absolute values, the actual integers %are merely
%reference points in space. It is the delta between AP %and AP or ML and ML
%that is meaningful
%values a-f would be acquired from the stereotaxic frame
a= 43; %AP at bregma
b= 20; %ML at bregma
c= 22; %DV at bregma
d= 38.8; %AP at lambda
e= 20; %ML at Lambda
f= 21.5; %DV at lamda This variable causes the change %in which I want the rotation to correct
%Bregma =[b a c];
%Following three points are given relative to bregma, this is convention. So mathematically 'Bregma=[0 0 0];'
%These are also the coordinates I want altered from the rotation matrix
MLgoal=0; %ML to injection
APgoal=0; %AP to injection
DVgoal=-2.0; %DV to injection
DVgoal_zaxis_signconvention_correction = -DVgoal; %we want sign convention flipped for z axis[![enter image description here][1]][1]
A= a-d;
M= b-e;
D= c-f;
%Bregma =[0 0 0];
v = [A M D];
mv = norm(v);
u=[A 0 0]; %theoretical vector [ML,AP,DV] bregma to %lambda
mu = norm(u);
%angle in radians
X=acos(dot(u,v)/(mu*mv));
cu=cross(u,v);
w=cu/norm(cu); %unit vector
%rotate about this unit vector by angle X
wx=w(1);
wy=w(2);
wz=w(3);
%sustitute variables into general rotation for more %readable code
vx = 1-cos(X);
cx = cos(X);
sx = sin(X);
%rotation matrix about arbitrary unit vector [wx; wy; %wz] by angle X
r=[vx*wx*wx + cx, vx*wx*wy - wz*sx, vx*wx*wz + wy*sx;
vx*wx*wy + wz*sx, vx*wy*wy + cx, vx*wy*wz - wx*sx;
vx*wx*wz - wy*sx, vx*wy*wz + wx*sx, vx*wz*wz + cx];
%Newcoordinate output should tell you the change in %targeting coordinate
%after correcting for the output of rotational matrix. %The value for DV
%seems too large based on the subtle increase elevation %of reference point
%Bregma to reference point Lambda
Newcoordinates=r* [APgoal;MLgoal;DVgoal_zaxis_signconvention_correction]; %should give you the new coordinates for the injection %site

How to plot a 3D surface with a circle in it?

I have a rational polynomial function. I find zeros of numerator and denominator of it. Now I want to draw this function and I do it with meshgrid and mesh command in matlab. How can I draw a circle in this shape? I add my result figure at first and second figure is an image that I want to be like that( draw red circle).
Create x and y for your circle:
r = 1;
theta = 0:0.1:2*pi;
x = r*cos(theta);
y = r*sin(theta);
Get the value of your function at the x and y's and plot a line in 3D with the values:
z = f(x,y);
plot3(x,y,z);
The final result may have some artefacts where the line crosses in and out of the surface. If you are not so concerned about the accuracy in the plot add a very small value to z to "lift" it above the surface.

How to get the values of a circle of pixels on an image?

I'm currently using MatLab as part of a Digital Imaging course and trying to get the values of a circle of pixels on an image. The idea is to start with a central pixel (x, y) and then collect all of the pixels with the radius r and return their values.
So far, all I've managed to create is this (where A_grey is an image):
function [localMean] = getLocalMean(x, y, radius)
for x = 1:size(A_grey, 1)
for y = 1:size(A_grey, 2)
<code>
end
end
But I'm not entirely sure what I'm doing and I could really do with some beginner level advice here. Any tips?
I'll throw you a bone. This is actually quite easy. Given the size of the image / viewing window, stored in width and height that you want to examine, generate a meshgrid of points, then search for those (x,y) values that satisfy the equation of the circle. In other words, you want to search for all (x,y) values given a centre of your circle (x0,y0) such that:
(x - x0)^2 + (y - y0)^2 = r^2
r is the radius of your desired circle. As such, it's as simple as:
[x,y] = meshgrid(1:width, 1:height);
p = (x - x0).^2 + (y - y0).^2 == r^2;
pts = [x(p) y(p)];
We first generate a meshgrid of points. Think of a meshgrid as a set of 2D arrays where each unique spatial location in the same spot of these arrays gives you the x and y coordinates at that spatial location in 2D. Take a look at the following example:
[x,y] = meshgrid(1:3, 1:3)
x =
1 2 3
1 2 3
1 2 3
y =
1 1 1
2 2 2
3 3 3
In terms of pixel coordinates, the top-left corner is (x,y) = (1,1). The top right corner is (x,y) = (3,1) and so on. Note that the convention is that x is horizontal while y is vertical, as what most digital image processing literature will give you. If this is not what you want, replace meshgrid with ndgrid to respect the row/column convention.
Once we generate this grid of points, we simply put this through our circle equation and see which values of (x,y) at a given centre (x0,y0) satisfy the equation of the circle. Once we find such locations, we simply index into our meshgrid of points and return those (x,y) values, corresponding to those points that lie along the perimeter of your circle.

Why not spherical plot? How to plot 3D-polar-plot in Matlab?

[r,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
[x,y]=pol2cart(sin(t)*cos(r),sin(t)*sin(r));
%[x,y]=pol2cart(r,t);
surf(x,y);
I played with this addon but trying to find an default function to for this. How can I do the 3D-polar-plot?
I am trying to help this guy to vizualise different integrals here.
There are several problems in your code:
You are already converting spherical coordinates to cartesian coordinates with the sin(theta)*cos(phi) and sin(theta)*sin(phi) bit. Why are you calling pol2cart on this (moreover, we're not working in polar coordinates!)?
As natan points out, there is no third dimension (i.e. z) in your plot. For unity radius, r can be omitted in the spherical domain, where it is completely defined by theta and phi, but in the cartesian domain, you have all three x, y and z. The formula for z is z = cos(theta) (for unit radius).
You didn't read the documentation for surf, which says:
surf(Z,C) plots the height of Z, a single-valued function defined over a geometrically rectangular grid, and uses matrix C, assumed to be the same size as Z, to color the surface.
In other words, your surf(x,y) line merely plots the matrix x and colors it using y as a colormap.
Here's the above code with the mistakes fixed and plotted correctly:
[f,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
x = sin(t)*cos(f);
y = sin(t)*sin(f);
z = cos(t);
surf(x,y,z)