image thumbnail

Aspect ratio conversion in images

version 1.0.1 (436 KB) by prathmesh bhosale
aspect ratio conversion by using bilinear interpolation or nearest neighbor interpolation techniques

4 Downloads

Updated 21 Nov 2021

View License

Image interpolation occurs in all digital photos at some stage. It happens anytime you resize or remap (distort) your image from one pixel grid to another.It is basic requirment for many image processing task like gaming , medical image processing ,computer graphics.To interpolate a image every pixel in the new image must be mapped back to a location in the old image in order to calculate a value of new pixel.There are many allgorithms to determine new value of the pixel.like Nearest-neighbor, Bilinear, Bicubic, Bicubic B-spline, Catmull-Rom .
here in this project I have done 2 of the this few techiniques that is nearest neighbor and bilinear interpolation .Nearest neighbor is the most basic and requires the least processing time of all the interpolation algorithms because it only considers one pixel -the closest one to the interpolated point.. This has the effect of simply making each pixel bigger.Bilinear interpolation considers the closest 2x2 neighborhood of known pixel values surrounding the unknown pixel. It has contribution of all 4 pixels to arrive at its final interpolated value. This results in bilinear interpolation is much smoother looking than nearest neighbor interpolation.
by using this code we can convert sd to hd and vise versa and also can convert any size into this standard density (SD) with sizes of 320x240 or 640x480 or 1024x768 or of high density (HD) with sizes of 720x540 or 1080x720 or 1620x1080 or can convert any size to any other size that we want .
% % Example (just uncomment this example in the matlab to see the output of the program)
% % -----------------------------------------------------------------------------------------------------------------------------------------------------
% img = imread("cameraman.tif");
%
% %fun calling% this function takes the img height and width and gives the interpolated output
% nearest_neighour_interpolated_img = nearest_neighour_interpolation(img,input("mention height of expected image (eg if you want output image(from nearest neighour techinique) as 1080 * 720 so 720 is the height (standard))) "),input("please mention the width of the output/expected image "));
% figure;
% subplot 211 ,imshow(img),title("input image");subplot 212 ,imshow(nearest_neighour_interpolated_img),title("output nearest neighour interpolated img with expected given output ");
% figure,imshow(nearest_neighour_interpolated_img),title("output nearest neighour interpolated img");% for seperate full scale view
% % -----------------------------------------------------------------------------------------------------------------------------------------------------
% % Example (just uncomment this example in the matlab to see the output of the program)
% % -----------------------------------------------------------------------------------------------------------------------------------------------------
% img = imread("cameraman.tif");
% % fun calling
% bilinear_interpolated_img = bilinear_interpolation(img,input("please mention the height of the output/expected image (for eg if you want output image(from bilinear interpolation techinique) as 1080 * 720 so 720 is the height (standard)) )"),input("please mention the width of the output/expected image"));
% figure;
% subplot 211 ,imshow(img),title("input image");subplot 212 ,imshow(bilinear_interpolated_img),title("output bilinear interpolated image");
% figure,imshow(bilinear_interpolated_img),title("output bilinear interpolated image");% for seperate full scale view
% % -----------------------------------------------------------------------------------------------------------------------------------------------------
Details of the theory for is detailed in Nearest-neighbor, Bilinear interpolation are discussed in the following refrence
Rederences
1.A Comparative Analysis of Image Interpolation Algorithms[researchgate.net]
2.interpolation techniques by prabir k. biswas sir [nptel]

Cite As

prathmesh bhosale (2022). Aspect ratio conversion in images (https://www.mathworks.com/matlabcentral/fileexchange/102459-aspect-ratio-conversion-in-images), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2021b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!