B-spline fitting to 2D discrete data points (pixels of contour image) - matlab

I am trying to fit a B-spline to a set of ordered discrete data points which represent pixels of a contour extracted from an image.
While the below code works fine for some simple shapes, but not for others (please see attached image for examples). Why does this happen, and what would be a better way to approach this problem?
I am quite new to differential geometry, appreciate any insights or inputs. Thanks.
% data contains two columns representing x,y coordinates of pixels
x = data(:, 1);
y = data(:, 2);
plot(x, y, 'bo');
fittedmodel = fit(x, y, 'cubicinterp');
plot(fittedmodel, 'r-');

What went wrong?
You have two sets of numbers x and y with the same number of elements in both sets.
You assume:
a. That there is a function f such that f( x_i ) = y_i for all pairs x_i,y_i in your sets.
b. That the points in the set are ordered: that is, if you follow the curve of f(x) then x_i "comes before" x_{i+1}.
While these assumptions hold for the "correct fit" example you have. They are no longer valid for the "incorrect fit" example.
As you can see for yourself, the input contour on the top cannot be expressed as y = f(x) since there are values of x for which there are two possible corresponding values of y (see the definition of mathematical function). The fit you got is the closest thing to a mathematical function y = f(x) that can be given the pairs x,y you gave (the red curve has the property of each x having only one y value).
What can you do?
In most cases when you try and fit a 2D curve you search for a parametric curve: that is, you introduce an auxilary parameter t such that each point along the curve can be represented as [x(t), y(t)] for some 0<=t<=1.
Now, if assumption b holds (and by looking at your examples, I'm not certain it is), what you can do is
t = linspace( 0, 1, numel(x) ); % define the parameter t
fitX = fit( t, x, 'cubicinterp'); % fit x as a function of parameter t
fitY = fit( t, y, 'cubicinterp'); % fit y as a function of parameter t
plot( fitX, fitY, 'r-' ); % plot the parametric curve

Related

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])

Draw a line with non-Cartesian coordinates in MATLAB

MATLAB's surf command allows you to pass it optional X and Y data that specify non-cartesian x-y components. (they essentially change the basis vectors). I desire to pass similar arguments to a function that will draw a line.
How do I plot a line using a non-cartesian coordinate system?
My apologies if my terminology is a little off. This still might technically be a cartesian space but it wouldn't be square in the sense that one unit in the x-direction is orthogonal to one unit in the y-direction. If you can correct my terminology, I would really appreciate it!
EDIT:
Below better demonstrates what I mean:
The commands:
datA=1:10;
datB=1:10;
X=cosd(8*datA)'*datB;
Y=datA'*log10(datB*3);
Z=ones(size(datA'))*cosd(datB);
XX=X./(1+Z);
YY=Y./(1+Z);
surf(XX,YY,eye(10)); view([0 0 1])
produces the following graph:
Here, the X and Y dimensions are not orthogonal nor equi-spaced. One unit in x could correspond to 5 cm in the x direction but the next one unit in x could correspond to 2 cm in the x direction + 1 cm in the y direction. I desire to replicate this functionality but drawing a line instead of a surf For instance, I'm looking for a function where:
straightLine=[(1:10)' (1:10)'];
my_line(XX,YY,straightLine(:,1),straightLine(:,2))
would produce a line that traced the red squares on the surf graph.
I'm still not certain of what your input data are about, and what you want to plot. However, from how you want to plot it, I can help.
When you call
surf(XX,YY,eye(10)); view([0 0 1]);
and want to get only the "red parts", i.e. the maxima of the function, you are essentially selecting a subset of the XX, YY matrices using the diagonal matrix as indicator. So you could select those points manually, and use plot to plot them as a line:
Xplot = diag(XX);
Yplot = diag(YY);
plot(Xplot,Yplot,'r.-');
The call to diag(XX) will take the diagonal elements of the matrix XX, which is exactly where you'll get the red patches when you use surf with the z data according to eye().
Result:
Also, if you're just trying to do what your example states, then there's no need to use matrices just to take out the diagonal eventually. Here's the same result, using elementwise operations on your input vectors:
datA = 1:10;
datB = 1:10;
X2 = cosd(8*datA).*datB;
Y2 = datA.*log10(datB*3);
Z2 = cosd(datB);
XX2 = X2./(1+Z2);
YY2 = Y2./(1+Z2);
plot(Xplot,Yplot,'rs-',XX2,YY2,'bo--','linewidth',2,'markersize',10);
legend('original','vector')
Result:
Matlab has many built-in function to assist you.
In 2D the easiest way to do this is polar that allows you to make a graph using theta and rho vectors:
theta = linspace(0,2*pi,100);
r = sin(2*theta);
figure(1)
polar(theta, r), grid on
So, you would get this.
There also is pol2cart function that would convert your data into x and y format:
[x,y] = pol2cart(theta,r);
figure(2)
plot(x, y), grid on
This would look slightly different
Then, if we extend this to 3D, you are only left with plot3. So, If you have data like:
theta = linspace(0,10*pi,500);
r = ones(size(theta));
z = linspace(-10,10,500);
you need to use pol2cart with 3 arguments to produce this:
[x,y,z] = pol2cart(theta,r,z);
figure(3)
plot3(x,y,z),grid on
Finally, if you have spherical data, you have sph2cart:
theta = linspace(0,2*pi,100);
phi = linspace(-pi/2,pi/2,100);
rho = sin(2*theta - phi);
[x,y,z] = sph2cart(theta, phi, rho);
figure(4)
plot3(x,y,z),grid on
view([-150 70])
That would look this way

How to integrate over a discrete 2D surface in MATLAB?

I have a function z = f(x, y), where z is the value at point (x, y). How may I integrate z over the x-y plane in MATLAB?
By function above, I actually mean I have something similar to a hash table. That is, given a (x, y) pair, I can look up the table to find the corresponding z value.
The problem would be rather simple, if the points were uniformly distributed over x-y plane, in which case I can simply sum up all the z values, multiply it with the bottom area, and finally divide it by the number of points I have. However, the distribution is not uniform as shown below. So I am actually asking for the computation method that minimises the error.
The currently accepted answer will only work for gridded data. If your data is scattered you can use the following approach instead:
scatteredInterpolant + integral2:
f = scatteredInterpolant(x(:), y(:), z(:), 'linear');
int = integral2(#(x,y) f(x,y), xmin, xmax, ymin, ymax);
This defines the linear interpolant f of the data z(i) = f(x(i),y(i)) and uses it as an argument to integral2. Note that ymin and ymax, instead of doubles, can be function handles depending on x. So usually you will be integrating rectangles, but this could be used for integration regions a bit more complicated.
If your integration area is rather complicated or has holes, you should consider triangulating your data.
DIY using triangulation:
Let's say your integration area is given by the triangulation trep, which for example could be obtained by trep = delaunayTriangulation(x(:), y(:)). If you have your values z corresponding to z(i) = f(trep.Points(i,1), trep.Points(i,2)), you can use the following integration routine. It computes the exact integral of the linear interpolant. This is done by evaluating the areas of all the triangles and then using these areas as weights for the midpoint(mean)-value on each triangle.
function int = integrateTriangulation(trep, z)
P = trep.Points; T = trep.ConnectivityList;
d21 = P(T(:,2),:)-P(T(:,1),:);
d31 = P(T(:,3),:)-P(T(:,1),:);
areas = abs(1/2*(d21(:,1).*d31(:,2)-d21(:,2).*d31(:,1)));
int = areas'*mean(z(T),2);
If you have a discrete dataset for which you have all the x and y values over which z is defined, then just obtain the Zdata matrix corresponding to those (x,y) pairs. Save this matrix, and then you can make it a continuous function using interp2:
function z_interp = fun(x,y)
z_interp = interp2(Xdata,Ydata,Zdata,x,y);
end
Then you can use integral2 to find the integral:
q = integral2(#fun,xmin,xmax,ymin,ymax)
where #fun is your function handle that takes in two inputs.
I had to integrate a biavariate normal distribution recently in MatLab. The idea is very simple. Matlab defines a surface through a meshgrid, so from x, y you need to do this:
x = -10:0.05:10;
y = x;
[X,Y] = meshgrid(x',y');
...for example. Then, let's call FX the function that defines the value at each point of the surface. To calculate the integral you just need to do this:
surfint = zeros(length(X),1);
for a = 1:length(X)
surfint(a,1) = trapz(x,FX(:,a));
end
trapz(x, surfint)
For me, this is the simplest way.

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)

Finding the belonging value of given point on a grid of 3D histogram?

I use 2D dataset like below,
37.0235000000000 18.4548000000000
28.4454000000000 15.7814000000000
34.6958000000000 20.9239000000000
26.0374000000000 17.1070000000000
27.1619000000000 17.6757000000000
28.4101000000000 15.9183000000000
33.7340000000000 17.1615000000000
34.7948000000000 18.2695000000000
34.5622000000000 19.3793000000000
36.2884000000000 18.4551000000000
26.1695000000000 16.8195000000000
26.2090000000000 14.2081000000000
26.0264000000000 21.8923000000000
35.8194000000000 18.4811000000000
to create a 3D histogram.
How can I find the histogram value of a point on a grid? For example, if [34.7948000000000 18.2695000000000] point is given, I would like to find the corresponding value of a histogram for a given point on the grid.
I used this code
point = feat_vec(i,:); // take the point given by the data set
X = centers{1}(1,:); // take center of the bins at one dimension
Y = centers{2}(1,:); // take center of the bins at other dim.
distanceX = abs(X-point(1)); // find distance to all bin centers at one dimension
distanceY = abs(Y-point(2)); // find distance to center points of other dimension
[~,indexX] = min(distanceX); // find the index of minimum distant center point
[~,indexY] = min(distanceY); // find the index of minimum distant center point for other dimension
You could use interp2 to accomplish that!
If X (1-D Vector, length N) and Y (1-D vector, length M) determine discrete coordinate on the axes where your histogram has defined values Z (matrix, size M x N). Getting value for one particular point with coordinates (XI, YI) could be done with:
% generate grid
[XM, YM] = meshgrid(X, Y);
% interpolate desired value
ZI = interp2(XM, YM, Z, XI, YI, 'spline')
In general, this kind of problem is interpolation problem. If you would want to get values for multiple points, you would have to generate grid for them in similar fashion done in code above. You could also use another interpolating method, for example linear (refer to linked documentation!)
I think you mean this:
[N,C] = hist3(X,...) returns the positions of the bin centers in a
1-by-2 cell array of numeric vectors, and does not plot the histogram.
That being said, if you have a 2D point x=[x1, x2], you are only to look up the closest points in C, and take the corresponding value in N.
In Matlab code:
[N, C] = hist3(data); % with your data format...
[~,indX] = min(abs(C{1}-x(1)));
[~,indY] = min(abs(C{2}-x(2)));
result = N(indX,indY);
done. (You can make it into your own function say result = hist_val(data, x).)
EDIT:
I just saw, that my answer in essence is just a more detailed version of #Erogol's answer.