Image downsampling and upsampling using bilinear interpolation - image-manipulation

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.

Related

create the pixel size for Dicom_Picture in matlab

I have pictures in .dcm format. From Dicominfo I learned that the pixel spacing is [0.9,0.9] mm and the slice thickness is 1.98 mm.
My task: I should get the picture size in real (world) coordinates and then display the pictures in all three projections in matlab.
I had an idea that I would create a matrix in matlab, but it is difficult for me to create the pixel size spacing.
I mean that the pixel in the matrix is like a square and is 0.9mm * 0.9mm.
I don't know if my approach is correct and if there is an easy way to solve the problem.
Thank you very much for every answer
several plotting functions allow you to specify x/y/z positions of each pixel/voxel, including imagesc, pcolor, here is an example using imagesc.
% vol stores your dicom volume
vol=rand(40,50,30);
dx=[0.9,0.9,1.98];
imagesc((0:size(vol,1)-1)*dx(1), (0:size(vol,2)-1)*dx(2), vol(:,:,1))

Finding Sub-Pixel Accurate Maxima in a 3D Image

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.

Qualitative and Quantitative analysis of filtered back projection / iradon in matlab

I was wondering if anyone encountered this issue.
I can reconstruct images from matlab that resembles the original image, however, the actual values are always different.
For example, original image have values in the matrix ranging from 0 to 1, while my reconstructed image ranges from -0.2 to 0.4 for example.
The reconstructed image look similar to the original image though, just that the data in the image are of different scales.
this is a sample code of what i mean.
p=phantom(64);
theta=0:1:179;
r=radon(p,theta);
ir=iradon(r,theta);
figure
subplot(1,2,1);imagesc(p)
subplot(1,2,2);imagesc(ir)
Those results aren't quite what I found.
>> min(min(ir))
-0.0583
>> max(max(ir))
0.9658
Remember that the Inverse Radon Transform can only approximate the reconstruction of the original image. With only 180 views, there's bound to be some differences.
The Radon transform inherently causes some information to be lost because pixels have to be projected onto a new coordinate system and re-binned - both during projection and back projection. This causes the reconstructed image to be degraded slightly. The Radon transform is not identically invertible like the Fourier Transform.
For better results, try using a larger image size and more viewing angles.
p=phantom(256);
theta=0:0.01:179;
And also try using a different filter (the F in F.B.P.) such as the Shepp-Logan, which reduces high frequencies and lessens overshoot.
ir=iradon(r,theta,'linear','Shepp-Logan');

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.

How to make "well" a ridge-shape from a given 2d line? (gaussian, matlab)

My goal is to make a ridge(mountain)-like shape from the given line. For that purpose, I applied the gaussian filter to the given line. In this example below, one line is vertical and one has some slope. (here, background values are 0, line pixel values are 1.)
Given line:
Ridge shape:
When I applied gaussian filter, the peak heights are different. I guess this results from the rasterization problem. The image matrix itself is discrete integer space. The gaussian filter is actually not exactly circular (s by s matrix). Two lines also suffer from rasterization.
How can I get two same-peak-height nice-looking ridges(mountains)?
Is there more appropriate way to apply the filter?
Should I make a larger canvas(image matrix) and then reduce the canvas by interpolation? Is it a good way?
Moreover, I appreciate if you can suggest a way to make ridges with a certain peak height. When using gaussian filter, what we can do is deciding the size and sigma of the filter. Based on those parameters, the peak height varies.
For information, image matrix size is 250x250 here.
You can give a try to distance transform. Your image is a binary image (having only two type of values, 0 and 1). Therefore, you can generate similar effects with distance transform.
%Create an image similar to yours
img=false(250,250);
img(sub2ind(size(img),180:220,linspace(20,100,41)))=1;
img(1:200,150)=1;
%Distance transform
distImg=bwdist(img);
distImg(distImg>5)=0; %5 is set manually to achieve similar results to yours
distImg=5-distImg; %Get high values for the pixels inside the tube as shown
%in your figure
distImg(distImg==5)=0; %Making background pixels zero
%Plotting
surf(1:size(img,2),1:size(img,1),double(distImg));
To get images with certain peak height, you can change the threshold of 5 to a different value. If you set it to 10, you can get peaks with height equal to the next largest value present in the distance transform matrix. In case of 5 and 10, I found it to be around 3.5 and 8.
Again, if you want to be exact 5 and 10, then you may multiply the distance transform matrix with the normalization factor as follows.
normalizationFactor=(newValue-minValue)/(maxValue-minValue) %self-explanatory
Only disadvantage I see is, I don't get a smooth graph as you have. I tried with Gaussian filter too, but did not get a smooth graph.
My result: