Using cv.remap (mexopencv) instead of interp2 (MATLAB) - matlab

I am experimenting with the mexopencv project that allows using the
OpenCV library from MATLAB .m files in order to compare the performance
of the native MATLAB functions with the OpenCV functions
I would like to substitute a call to the MATLAB interp2 function:
Vq = interp2(X,Y,V,Xq,Yq) returns interpolated values of a function of two variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X and Y contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq and Yq contain the coordinates of the query points.
The substitute shall be a call to the cv.remap function.
Applies a generic geometrical transformation to an image
dst = cv.remap(src, map1, map2)
dst = cv.remap(src, map1)
dst = cv.remap(..., 'OptionName',optionValue, ...)
The three SO questions Similar OpenCV Api for interp2 in Matlab,
How to do grid interpolation interp2 in OpenCV
and cv::remap (in opencv) and interp2 (matlab)
state that the OpenCV function remap can be used instead of the native MATLAB
function interp2, but I have no idea how-to correctly interpret/transform the
arguments (I have no experience regarding MATLAB and computer vision).
How can I use the mexopencv function cv.remap to get the same effect as if one
would call Vq = interp2(X,Y,V,Xq,Yq)?

Related

How do I construct a piecewise polynomial (cubic spline) for 4D data in MATLAB?

I have a problem in which I have to interpolate 4D data d = f(a, b, c) often, because the interpolation happens within an optimisation routine. Now, at first I programmed this using Matlab's interpn function. However, the program obviously became very slow, because the cubic splines had to be constructed upon each iteration within the optimisation.
I have read about 2D spline interpolation and I am basically looking for its 4D equivalent: pp = spline(a,b,c,d). Also, I found the scatteredInterpolant function (I have a non-uniform grid), but this function only gives me options for 'linear', 'nearest', or 'natural' and not the 'spline' option I'm looking for.
I could imagine that Matlab would have the function that is underneath the interpn function available, but I can't seem to find it. Does anyone know such a function that returns the piecewise polynomial or some other form of a spline function for a 4D interpolant, preferably Matlab-original?
P.s. I have also looked into a workaround; typing edit interpn, I have tried copying the Matlab function interpn, naming it differently and editing it such that it returns F instead of Vq, the interpolating function. However, doing this it says it doesn't recognise the methodandextrapval function, the first nested Matlab built-in it encounters.

Matlab - Interpolation of non-monotonic scattered 3d - data using griddata

My question is about using griddata as a substitute for interp2 in matlab as my data is not monotonic. I have extracted data for x_measured, y_measured, z_measured and would like to interpolate z at specified Points (x,y).
This code works:
z = griddata(x_measured,y_measured, z_measured,x,y,'linear')
This obviously doesn't as the data is non monotonic:
z = interp2(x_measured,y_measured, z_measured,x,y,'linear')
I'm wondering why griddata works. griddata returns a value using linear Interpolation as specified in the function call. Is there a fundamental difference between the two algorithms?

Transform a solution vector of PDE into a piecewise linear function - MATLAB

I know that when I use the PDE toolbox in Matlab to solve a PDE the result is a vector, which represents the values of the function in each vertex of the mesh.
Is there a command in the PDE toolbox such that we could transform the vector solution into a piecewise linear function on the domain of definition, so that we could be able to use it like u(x,y) to find directly the approximate value in (x,y)?
I don't know about such function. But your solution is defined on a structured rectangular grid. If you simply need to interpolate data on a 2D rectangular grid, you can use interp2 for that. If your grid is made of triangles, use TriScatteredInterp. If you want to use different interpolation (e.g., FEM), you will have to implement it yourself.

Expectation values in MATLAB

I have a 3D data set representing a grid of photosensors which unfortunately were not steady during use. I have estimated the pdf for the motion of the detector and I want to find the expectation value for each sensor. As I don't want to reflect my pdf (i.e. I want f(t) not f(t-tau)) I don't think I can use the matlab convolve function. Is there a function that will do what I want or should I just get on with it? I've found movavg but it seems to do something else or maybe I just haven't followed it correctly.
Also, as I'm faily new to matlab, from C, I tend to use for loops which I'm aware is not the way to use Matlab. Is there an easy way to convert my 1-D pdf to a 3D matrix that will only operate in one axis (z in this case). Something like repmat(i, j, pdf).
Thanks
The expected value of a pdf is the integral over the product of the grid values and the pdf values at the corresponding grid points.
To integrate over a 3D-Grid, use the Matlab function triplequad (doc). As an example, let us compute the expected x value:
expectedx = triplequad(pdfxfun,xmin,xmax,ymin,ymax,zmin,zmax)
pdfxfun in this call must be a function handle which for any (x,y,z) point it receives should return the product of the pdf value at that point and x. In your case, this could probably be achieved by interpolation over your grid data. I don't know a 3D-interpolation function in Matlab, so probably you have to work on this for yourself. Roughly, what you would do is:
function pdfvalue = pdfinterpolation(x,y,z,Xgrid,Ygrid,Zgrid,pdfdata)
% compute interpolated pdfvalue at (x,y,z) from griddata and pdfdata
Then, pdfxfun above can be defined as anonymous function in the call to triplequad:
pdfxfun = #(x,y,z) x*pdfinterpolation(x,y,z,myxgrid,myygrid,myzgrid,pdfdata)
expectedx = triplequad(pdfxfun,xmin,xmax,ymin,ymax,zmin,zmax)

How to draw the transfer function of an RC circuit using MATLAB?

If I have an RC circuit with transfer function 1/(1+sRC) how do I draw the transfer function using MATLAB?
Num2=[1];
Den2=[R*C 1];
RCcirc=tf(Num2,Den2);
How do I declare the R and the C so that there are no errors?
tf is the wrong tool for plotting the transfer function. Try these instead:
Use linspace to generate a range of values for s. Give R and C reasonable values of your choice.
Read up on arithmetic operations in MATLAB, especially ./
Look at how to use plot and familiarize yourself with the command using some simple examples from the docs.
With these you should be able to plot the transfer function in MATLAB :)
First of all you need to understand what transfer function you want. Without defined values of R and C you won't get any transfer function. Compare it to this, you want to plot a sine wave: x = sin(w*t), I hope you can agree with me that you cannot plot such a function (including axes) unless I specifically say e.g. t is the time, ranging from 0 seconds to 10 seconds and w is a pulsation of 1 rad/s. It's exactly the same with your RC network: without any values, it is impossible for numerical software such as MATLAB to come up with a plot.
If you fill in those values, you can use th tf function to display the transfer function in whatever way you like (e.g. a bode plot).
On the other hand, if you just want the expression 1/(1+s*R*C), take a look at the symbolic toolbox, you can do such things there. But to make a plot, you will still have to fill in the R and C value (and even a value for your Laplace variable in this case).