Finding Sub-Pixel Accurate Maxima in a 3D Image - matlab

I am using a 3D cross correlation technqiue to track a particle in 3D. It is very robust but my z dimension is 4x times lower resolution than my x and y. The cross correlation produces a 3D image with a single maximum. I would like to localise this point with sub-pixel accuracy using interpolation of some sort I expect.
Any help welcome!
Craig

You could use bicubic (tricubic in 3D?) or similar interpolation around the peak, as used for image scaling, to better localize the peak. This is commonly done in image processing, for example when localizing peaks in difference-of-gaussian stacks for blob detection, by performing a cubic approximation in each dimension, with the respective neighbouring pixels.

Related

Disparity calculation of two similar images in matlab

I have two images(both are exactly same images) and I am trying to calculate the disparity between them using sum of squared distances and reconstruct disparity in 3D space.
Do I need to rectify the image before calculating disparity?
The following are the steps that I have done so far for disparity map computation(I have tried with rectification and without rectification but both are returning all zeroes disparity matrix).
For each pixel in the left image X,
Take the pixels in the same row in the right image.
Separate the row in right image to windows.
For each window,
Calculate the disparity for each pixel in that window with X
Select the pixel in the window which gives minimum SSD with X
Find the pixel with minimum disparity among all windows as the best match to X
Am I doing it correctly?
How can I visualise the 3D reconstruction of the disparity as scatter plot in matlab?
Rectification guarantees that matches are to be found in the same row (for horizontally separated cameras). If you have doubts about rectification of your images you can try to compare rows by drawing horizontal lines between horizontally separated images. If the lines hit the same features you are fine, see the picture below where images are NOT rectified. The fact that they are distorted means there was a lens distortion correction as well as attempted (but not actually performed correctly) rectification.
Now, let’s see what you meant by the same images. Did you mean the images of the same object that were taken from different viewpoints? Note that if the images are literally the same (the same viewpoints) the disparity will be zero as was noted in another answer. The definition of disparity (for horizontally separated cameras) is a value of shift (in the same row) between matching features. The disparity is related to depth (if optical axes of cameras are parallel) as disparity d=f*B/z, where z - depth, B - baseline or separation between cameras and f is a focal length. You can transform the formula above into disparity/B=f/z which basically says that disparity related to camera separation as focal length is related to distance. In other words, the ratios of horizontal and distance measures are equal.
If your images are taken with the cameras shifted horizontally the disparity (in a simple correlation algorithm) is typically calculated in 5-embedded loops:
loop over image1 y
loop over image1 x
loop over disparity d
loop over correlation window y
loop over correlation window x
Disparity, or D_best, gives you the best matching window between image1 and image2 across all possible values of d. Finally, scatterplots are for 3D point clouds while disparity can be rather visualized as a heat color map. If you need to visualize 3D reconstruction or simply saying a 3D point cloud calculate X, Y, Z as:
Z=fB/D, X=uZ/f, Y=v*Z/f, where u and v are related to column and row of wxh image as
u=col-w/2 and v=h/2-row, that is u, v form an image centered coordinate system.
If your two images are exactly the same, then the disparity would be 0 for every pixel. You either have to use two separate cameras to take the images, or take them with a single camera from two different locations. The best way to do 3D reconstruction is to use a calibrated stereo pair of cameras. Here is an example of how to do that using the Computer Vision System Toolbox for MATLAB.

Artifacts in image after super resolution using delaunay triangulation in MATLAB

i have to do super resolution of two low resolution images to obtain a high resolution image.
2nd image is taken as base image and the first image is registered with respect to it . i used SURF algorithm for image registration . A Delaunay triangulation is constructed over the points using a built-in MATLAB delaunay function . The HR grid of size is constructed for a prespecified resolution enhancement factor R Then HR algorithm for interpolating the pixel values on the HR grid is summarized next.
HR Algorithm Steps: 1. Construct the Delaunay triangulation over the set of scattered vertices in the irregularly sampled raster formed from the LR frames.
Estimate the gradient vector at each vertex of the triangulation by calculating the unit normal vector of neighbouring vector using cross product method.Sum of the unit normal vector of each triangle multiplied by its area is divided by summation of area of all neighbouring triangles to get the vertex normal.
Approximate each triangle patch in the triangulation by a continuous and, possibly, a continuously differentiable surface, subject to some smoothness constraint. Bivariate polynomials or splines could be the approximants as explained below.
Set the resolution enhancement factor along the horizontal and vertical directions and then calculate the pixel value at each regularly spaced HR grid point to construct the initial HR image
now i have the results shown below
now for one kind of data set i get this result that has a few pixels black and wite in a random manner for the other type i get thin parallel lines all over image after super resolution the results are attached
any one can tell me the reason, i have figured out may be its demosaicing but i am not sure,because i dnt have much understanding of it , moreover can it be a bug in my code but it behaves different for different images, i have increased the size by super resolution twice.

Stereo matching

I am using Camera Calibration Toolbox for Matlab. After calibration I have intrinsic and extrinsic parameters of stereo camera system. Next, I would like to determine the distance between the camera system and the object. To get this information, I used the function stereo_triangulation which is included in the Toolbox. Input are two matrixes including pixel coordinates of correspondences in the left and right image.
I tried to get coordinates of correspondences with using of Basic Block Matching method which is described in Matlab's help for Stereo Vision.
Resolution of my pictures is 1280x960 pixels. I know that the biggest disparity is around 520 pixels. I set the maximum of disparity range to 520. But then determine the coordinates takes ages. It is not possible use in practice. Calculating of disparity map is much faster with using of Matlab's function disparity(). But I want the step before - coordinates of correspondences.
Please can you suggest how can I effectively get the coordinates with Matlab?
Disparity and 3D are related by simple formulas (see below) so the time for calculating 3D data and disparity map should be the same. The notation is
f - focal length in pixels,
B - separation between cameras,
u, v - row and column in the system centered on the middle of the image,
d-disparity,
x, y, z - 3D coordinates.
z=f*B/d;
x=z*u/f;
y=z*v/f;
1280x960 is too large resolution for any correlation stereo to work in real time. Think about it: you have to loop over a 2d image, over 2d correlation window and over the range of disparities. This means 5 embedded loops! I don't work with Matlab anymore but I know that it is quite slow.

Image downsampling and upsampling using bilinear interpolation

I am trying to understand how exactly the upsampling and downsampling of a 2D image I have, would happen using Bilinear interpolation. Now I am aware of how bilinear interpolation works using a 2x2 neighbourhood values to interpolate the data point inside this 2x2 area using weights. But what I am not aware of, is asked below. My objectives and specific queries are -
1.To start with I have a 2D image of values(size MxN). The width(M) and height(N) of this image is not fixed, but will change from case to case. This 2D image needs to be down-sampled using bilinear interpolation to a grid of size PxQ (P and Q are to be configured as input parameters) e.g. lets take PxQ is 8x8. And assume input 2D array image is of size 200x100. i.e 200 columns, 100 rows.
Now how while performing downsampling using bilinear interpolation of this 200x100 image, should I first obtain a downsampled image of size 100x50 (downsampling by 2 in both dimensions using bilinear interpolation); then a 50x25 image(again by doing downsampling by 2 in both dimensions), then a 25x12 image, then a 12x12(this time doing downsampling by linear(not bilinear!) interpolation only along the rows, and finally drop some pixels to get 8x8.
Any pointers to exact algorithm or different ways to achieve this, are appreciated.
2.Above question raises another one - how to downsample using bilinear interpolation by a non-integer scale factor, e.g. how to go from a say 8x8 image array to a 6x2 image wherein resampling/scaling factors in both dimensions are not integers.
3.Then when I get a 8x8 sized image I need to upsample it by bilinear interpolation to the same original size I started with- MxN. If I need to go from 8x8 to say 20x20. How would it interpolate in between points in a row and would it interpolate a full row by some means. Again in case of non-integer scale factors how would bilinear interpolation for upsampling happen. Exact steps.
And finally I need to implement this in C.
I tried visualizing these particular questions by taking different examples, but not got a clear picture of how this bilinear interpolation would happen while downsampling and upsampling. All I have is plenty of paper sheets having'dots and crossed' pictures on my desk, but still no clear solution!
Any detailed reading material, books appreciated.

iPhone - creating the smoothest curve

I have this iPhone app that has an array containing around 50 to 100 points. How do I calculate the smoothest curve that will fit the points? It can be bezier, cubic, quadratic, whatever. It just have to look smooth and fit as much as possible all points (obviously, as I did in my drawing, to create a smooth curve, some points have to be created out of the original set... no problem).
See picture:
Maybe you are looking for a Cubic Spline
Cubic Spline
These are the functions with continous second derivative that interpolate your nodes with the smallest curvature so they oscillate less. And there are lots of examples and algorithms to find these.