{"id":200,"date":"2009-12-23T15:44:23","date_gmt":"2009-12-23T22:44:23","guid":{"rendered":"http:\/\/www.giassa.net\/?page_id=200"},"modified":"2025-08-24T08:03:52","modified_gmt":"2025-08-24T15:03:52","slug":"2-upsampling-interpolation","status":"publish","type":"page","link":"https:\/\/www.giassa.net\/?page_id=200","title":{"rendered":"2 &#8211; Upsampling &#038; Interpolation"},"content":{"rendered":"<p>Well, now that we can downsample an image and reduce its dimensions, it would be nice to be able to increase the dimensions of an image. When downsampling, we have a very simple task &#8211; throw away data (according to strict calculations of course).<\/p>\n<p>When we want to increase image dimensions, we are basically expanding an image, and filling in &#8220;gaps&#8221; in rows and columns of the original image. For example, say we want to increase the width and height of an image by a factor of 4. We could simply &#8220;repeat\/redraw&#8221; each row and column of the original image 4 times. Sample code and sample images are shown below.<\/p>\n<pre class=\"lang:matlab decode:true \">%Import my original picture file\r\n\r\nI = imread('trees_small.jpg','jpg');\r\n\r\n%Convert image to grayscale (intensity) values for simplicity (for now)\r\n\r\nI = rgb2gray(I);\r\n\r\n%Determine the dimensions of the source image\r\n\r\n%Note that we will have three values - width, height, and the number\r\n\r\n%of color vectors, 3\r\n\r\n[j k] = size(I)\r\n\r\n%Determine how much larger we want the new image (should be an integer)\r\n\r\nscale = 4;\r\n\r\n%Declare and initialize an output image buffer\r\n\r\nM = zeros(scale.*j,scale.*k);\r\n\r\n%Generator the output image\r\n\r\nfor count1 = 1:scale:j.*scale\r\n\r\n for count2 = 1:scale:k.*scale\r\n\r\n for count3 = 1:scale\r\n\r\n for count4 = 1:scale\r\n\r\n M(count1+count3,count2+count4) = I(ceil(count1.\/scale),ceil(count2.\/scale));\r\n\r\n end\r\n\r\n end\r\n\r\n end\r\n\r\nend\r\n\r\n%Display the two images side-by-side for a few seconds, then close\r\n\r\nsubplot(1,2,1); imagesc(I);colormap gray; axis tight;\r\n\r\nsubplot(1,2,2); imagesc(M);colormap gray; axis tight;\r\n\r\npause(4);\r\n\r\nclose all;<\/pre>\n<p>&nbsp;<\/p>\n<div id=\"attachment_202\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloned.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-202\" class=\"size-medium wp-image-202\" title=\"cloned\" src=\"https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloned-300x265.png\" alt=\"Upsampling by cloning rows and columns\" width=\"300\" height=\"265\" srcset=\"https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloned-300x265.png 300w, https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloned.png 568w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-202\" class=\"wp-caption-text\">Upsampled image after cloning columns and rows<\/p><\/div>\n<p style=\"text-align: left;\">Interestingly enough, when both images are scaled side-by-side, they look identical. By duplicating entire rows and columns, we have not created any &#8220;new&#8221; data. The extra rows and columns are completely redundant, and do not provide any real improvement, nor do they provide data that we did not already have, leaving this approach completely useless for increasing the dimensions of images (at least so far as these tutorials are concerned). An example of increasing the width and height of an image by a factor of 3 is shown below to give a graphical demonstration of exactly what is happening.<\/p>\n<div id=\"attachment_210\" style=\"width: 508px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloning.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-210\" class=\"size-full wp-image-210\" title=\"cloning\" src=\"https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloning.png\" alt=\"\" width=\"498\" height=\"259\" srcset=\"https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloning.png 498w, https:\/\/www.giassa.net\/wp-content\/uploads\/2009\/12\/cloning-300x156.png 300w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/a><p id=\"caption-attachment-210\" class=\"wp-caption-text\">Basic Upsampling by a Factor of 3<\/p><\/div>\n<p title=\"I \u2013 Nearest Neighbour Interpolation\">Another problem with this approach is that it only works when we want to increase the image dimensions by an integral factor (ie: 2, 4, 10), as it cannot deal with the case of image scaling by a fractional number (2.5, 10\/3, etc). If we want to increase the dimensions of the image AND be able to overcome the shortcomings of the previous example, we need to interpolate image data &#8211; using mathematical produces to provide a reasonably accurate intermediate value between pixels. In the following subsections I will cover some of the most common approaches to increasing the dimensions of an image by interpolation.<\/p>\n<p title=\"I \u2013 Nearest Neighbour Interpolation\"><div id=\"shailan-subpages-200-1\"><ul class=\"subpages\"><li class=\"page_item page-item-207\"><a href=\"https:\/\/www.giassa.net\/?page_id=207\" title=\"I &#8211; Nearest Neighbour Interpolation\" rel=\"\">I - Nearest Neighbour Interpolation<\/a><\/li>\n<li class=\"page_item page-item-240\"><a href=\"https:\/\/www.giassa.net\/?page_id=240\" title=\"II \u2013 Bilinear Interpolation\" rel=\"\">II \u2013 Bilinear Interpolation<\/a><\/li>\n<li class=\"page_item page-item-274\"><a href=\"https:\/\/www.giassa.net\/?page_id=274\" title=\"III &#8211; Bicubic Spline Interpolation\" rel=\"\">III - Bicubic Spline Interpolation<\/a><\/li>\n<li class=\"page_item page-item-371\"><a href=\"https:\/\/www.giassa.net\/?page_id=371\" title=\"IV &#8211; Generalized Bicubic Interpolation\" rel=\"\">IV - Generalized Bicubic Interpolation<\/a><\/li>\n<\/ul><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, now that we can downsample an image and reduce its dimensions, it would be nice to be able to increase the dimensions of an image. When downsampling, we have a very simple task &#8211; throw away data (according to &hellip; <a href=\"https:\/\/www.giassa.net\/?page_id=200\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":118,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-200","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.giassa.net\/index.php?rest_route=\/wp\/v2\/pages\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.giassa.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.giassa.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.giassa.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.giassa.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":16,"href":"https:\/\/www.giassa.net\/index.php?rest_route=\/wp\/v2\/pages\/200\/revisions"}],"predecessor-version":[{"id":1092,"href":"https:\/\/www.giassa.net\/index.php?rest_route=\/wp\/v2\/pages\/200\/revisions\/1092"}],"up":[{"embeddable":true,"href":"https:\/\/www.giassa.net\/index.php?rest_route=\/wp\/v2\/pages\/118"}],"wp:attachment":[{"href":"https:\/\/www.giassa.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}