Thilina's Blog

I might be wrong, but…

Digital Image Zooming – Sample Codes on MATLAB

Image zooming or image scaling is another basic operation in computer graphics. In this article I am trying to describe how to implement the image zooming or scaling from our own.  Image resize algorithms are try to interpolate the suitable image intensity values for the pixels of resized image which does not directly mapped to its original image. There are three major algorithms for image resizing,

1. Nearest Neighbour Interpolation

2. Bi-linear interpolation

3. Bi-cubic interpolation

clip_image002You can read more about these methods in Wikipedia and from this web site and also by typing ‘doc imresize’ on MATLAB command prompt which is also the default operation for image resizing. I’ll try to briefly describe what’s going on in the each algorithm and a way to implement it on MATLAB.

Nearest Neighbour Interpolation

In the nearest neighbour algorithm, the intensity value for the point v(x,y is assigned to the nearest neighbouring pixel intensity f(x,y)which is the mapped pixel of the original image. The logic behind the approximation is as the equation below.

clip_image004

Sample code for this implementation is as below.


function imzoom = nnbr(image,zoom);
[r c d] = size(image);      % dimensions of image data
%% zoom
rn = floor(zoom*r);
cn = floor(zoom*c);
s = zoom;
im_zoom = zeros(rn,cn,d);
%% nearest neighbour
for i = 1:rn;
    x = i/s;
    near_i = cast(round(x),'uint16');
if near_i == 0
        near_i = 1;
    end
    for j = 1:cn;
        y = j/s;
near_j = cast(round(y),'uint16');
        if near_j == 0
            near_j = 1;
        end
        im_zoom(i,j,:) = image(near_i,near_j,:);
    end
end
imzoom = im_zoom;

Bilinear Interpolation

clip_image006In the Bi-linear interpolation, intensity of the zoomed image pixel ‘P’ is defined by the weighted sum of the mapped 4 neighbouring pixels. If the zooming factor is ‘s’, then the mapped pixel point in the original image is given by ‘r’ and ‘c’ as follows.

clip_image008

And the distance from the point of interest to the mapped pixels can be obtain as follows,

clip_image010

So the neighbouring 4 pixels can be defined as,

clip_image012

By using these values we can approximate the intensity of the pixel in interest as follows.

clip_image014

Sample code for this implementation is as below.


function image_zoom = blnrm2(image, zoom)
[r c d] = size(image);
rn = floor(zoom*r);
cn = floor(zoom*c);
s = zoom;
im_zoom = zeros(rn,cn,d);
for i = 1:rn;
    x1 = cast(floor(i/s),'uint32');
    x2 = cast(ceil(i/s),'uint32');
    if x1 == 0
        x1 = 1;
    end
    x = rem(i/s,1);
    for j = 1:cn;
        y1 = cast(floor(j/s),'uint32');
        y2 = cast(ceil(j/s),'uint32');
        if y1 == 0
            y1 = 1;
        end
        ctl = image(x1,y1,:);
        cbl = image(x2,y1,:);
        ctr = image(x1,y2,:);
        cbr = image(x2,y2,:);
        y = rem(j/s,1);
        tr = (ctr*y)+(ctl*(1-y));
        br = (cbr*y)+(cbl*(1-y));
        im_zoom(i,j,:) = (br*x)+(tr*(1-x));
    end
end
image_zoom = cast(im_zoom,'uint8');

Bi-cubic Interpolation

When using the bi-cubic interpolation zoomed image internsity v(x,y) is defined using the weighted sum of mapped 16 neighbouring pixels of the original image. As in above method, let the zooming factor is ‘s’ and the mapped pixel point in the original image is given by ‘r’ and ‘c’. Then the neighbour-hood matrix can be defined as,

clip_image016

Using the bi-cubic algorithm;

clip_image018

The coefficients aij can be find using the La-grange equation.

clip_image020

clip_image022

clip_image024

When implementing this algorithm I made a mask for defining ai and bj using matrix and then I applied it to the matrix containing the selected 16 points, due to reduce the complexity of the algorithm in-order to reduce the calculation time. Zero padding is added to surround the original image to remove the zero reference error occurred.

clip_image026

Sample code for this implementation is as below.


function im_zoom = bicubic_m2(image,zoom);
[r c d] = size(image);
rn = floor(zoom*r);
cn = floor(zoom*c);
s = zoom;
im_zoom = cast(zeros(rn,cn,d),'uint8');
im_pad = zeros(r+4,c+4,d);
im_pad(2:r+1,2:c+1,:) = image;
im_pad = cast(im_pad,'double');
for m = 1:rn
    x1 = ceil(m/s); x2 = x1+1; x3 = x2+1;
    p = cast(x1,'uint16');
    if(s>1)
       m1 = ceil(s*(x1-1));
       m2 = ceil(s*(x1));
       m3 = ceil(s*(x2));
       m4 = ceil(s*(x3));
    else
       m1 = (s*(x1-1));
       m2 = (s*(x1));
       m3 = (s*(x2));
       m4 = (s*(x3));
    end
    X = [ (m-m2)*(m-m3)*(m-m4)/((m1-m2)*(m1-m3)*(m1-m4)) ...
          (m-m1)*(m-m3)*(m-m4)/((m2-m1)*(m2-m3)*(m2-m4)) ...
          (m-m1)*(m-m2)*(m-m4)/((m3-m1)*(m3-m2)*(m3-m4)) ...
          (m-m1)*(m-m2)*(m-m3)/((m4-m1)*(m4-m2)*(m4-m3))];
    for n = 1:cn
        y1 = ceil(n/s); y2 = y1+1; y3 = y2+1;
        if (s>1)
           n1 = ceil(s*(y1-1));
           n2 = ceil(s*(y1));
           n3 = ceil(s*(y2));
           n4 = ceil(s*(y3));
        else
           n1 = (s*(y1-1));
           n2 = (s*(y1));
           n3 = (s*(y2));
           n4 = (s*(y3));
        end
        Y = [ (n-n2)*(n-n3)*(n-n4)/((n1-n2)*(n1-n3)*(n1-n4));...
              (n-n1)*(n-n3)*(n-n4)/((n2-n1)*(n2-n3)*(n2-n4));...
              (n-n1)*(n-n2)*(n-n4)/((n3-n1)*(n3-n2)*(n3-n4));...
              (n-n1)*(n-n2)*(n-n3)/((n4-n1)*(n4-n2)*(n4-n3))];
        q = cast(y1,'uint16');
        sample = im_pad(p:p+3,q:q+3,:);
        im_zoom(m,n,1) = X*sample(:,:,1)*Y;
        if(d~=1)
              im_zoom(m,n,2) = X*sample(:,:,2)*Y;
              im_zoom(m,n,3) = X*sample(:,:,3)*Y;
        end
    end
end
im_zoom = cast(im_zoom,'uint8');

Results obtained from these three methods are as below.

Original Image Nearest-neighbour x1.6

clip_image028 clip_image029

Bi-linear interpolation x1.6 Bi-cubic interpolation x0.6

clip_image030 clip_image031

Calculation times for each method with zooming factor 1.6 and with 160x120x3 RGB image are as follows.

>> tic;imshow(nnbr(im,2.5));toc

Elapsed time is 1.774069 seconds.

>> tic;imshow(blnr(im,2.5));toc

Elapsed time is 4.052437 seconds.

>> tic;imshow(bicubic_m2(im,2.5));toc

Elapsed time is 3.772757 seconds.

2010 December 24 - Posted by | Image Processing, MATLAB

79 Comments »

  1. ela. keep it up kolla. 🙂

    Comment by Gosala | 2010 December 26 | Reply

  2. Hi,

    the bilinear and bicubic ran into an error:

    ??? Index exceeds matrix dimensions.

    Error in ==> bilinear_ at 22
    cbl = image(x1,y2,:);

    Please advice.
    Thank you.

    Comment by Michael | 2011 January 22 | Reply

    • Hi,
      Please replace line 10 of bilinear by
      x2 = cast(ceil(i/s),’uint16′);
      and line 17 of bilinear by
      y2 = cast(ceil(j/s),’uint16′);
      Code will work, sorry for the computational mistake I did, thanks for comment..!

      Comment by thilinamees | 2011 January 22 | Reply

      • Hi,

        the bicubic ran into an error:

        Error using ==> mtimes
        Inner matrix dimensions must agree.

        Error in ==> bicubic_m2 at 93
        im_zoom(m,n,1) = X*sample(:,:,1)*Y;

        Comment by Bims | 2012 April 15 | Reply

        • Hi,
          I tested code again, its working properly.

          Comment by Thilina S. | 2012 April 19 | Reply

  3. Hi,

    I am also getting an error for

    ??? Assignment has fewer non-singleton rhs
    dimensions than non-singleton
    subscripts

    Error in ==> bicubic_ at 8
    im_pad(2:r+1,2:c+1,:) = image;

    Thank you for your help.

    Comment by Michael | 2011 January 22 | Reply

    • Hi,
      I designed the code for zoom RGB images, I assume you tried my code with a gray-scale image,
      I got update the code again to also work with RGB as well as gray scale images. Try the code now,
      Thank you for the comment..!

      Comment by thilinamees | 2011 January 22 | Reply

  4. Hi Thilina,

    I really like your site, it’s really helpful, and I think you are doing a really great work.

    1 more question. I tested your version with the matlab inbuilt, besides ‘nearest’. The other 2 spatial-based interpolators are not giving the same performance. In fact, in the following case, bicubic deteriorates. In some cases, bicubic is better. Nonetheless, it differs from matlab inbuilts.

    [SBI – Inbuilt]
    PSNR (interpolatedImage: nearest) = +35.58328 dB
    PSNR (interpolatedImage: bilinear) = +37.89995 dB
    PSNR (interpolatedImage: bicubic) = +38.22953 dB

    [SBI – Self construct]
    PSNR (interpolatedImage: nearest) = +35.58328 dB
    Error rate: 0.00000 %
    PSNR (interpolatedImage: bilinear) = +42.84948 dB
    Error rate: 13.05949 %
    PSNR (interpolatedImage: bicubic) = +42.01865 dB
    Error rate: 9.91151 %

    Comment by Michael | 2011 January 22 | Reply

    • Hi Michel,
      Thank you very much for your kind responses.., As you see nearest interpolation does not deal any kind of data type conversion in spatial intensities(uint8 is kept in whole operation)..
      I think it can be the reason for this chance. I’ll test the code again for using im2double() instead of cast(im,’double’) and let you know if it succeeded, Can you be kind enough to reply me the way you calculated PSNR,
      Thanks again for your comment..!

      Comment by Thilina S. | 2011 January 22 | Reply

  5. Hi Thilina,

    The PSNR code can be found here in the following link. Please note that there may be a slight error in that version. Do ensure that the numerator is 255^2 (highestPossibleValueForIntensity^2) instead of 256^2.

    http://www.mathworks.com/matlabcentral/fileexchange/29500-image-error-measurements

    Comment by Michael | 2011 January 23 | Reply

    • Hi Michael,
      Thank you very much for reply and help..!!

      Comment by Thilina S. | 2011 January 23 | Reply

      • Hi Thilina,

        Thank you. Your effort makes a grand difference. You have made a very good tutorial.

        Comment by Michael | 2011 January 23 | Reply

    • Hi Michael,
      I tried with several modifications to the code, but no change in the result 😦 , However I also noticed that image result of self-constructed code has a high sharpness than the same result with the MATLAB built-in code. I think maybe they also use a smoothing kernel also with interpolation mechanism. I’ll update this back when I got much similar results to MATLAB biult-in.
      Thank you..!

      Comment by Thilina S. | 2011 January 23 | Reply

      • Hi Thilina,

        I am not too well versed with the interpolation in the interpolation theory. However, when we downsized it and recover to a original size, we hope to get back the same image, including it’s original noise. This is the main theme of image restoration.

        The better quality is what human may perceive and desire to have sometimes. This is another area known as image enhancement.

        Depending on the applications, the 2 themes may not match to be the same. For example a single dot on an image for NASA may indicate a possibility of a star, another solar system, or possible black hole. Thus, such restoration is important. Enhancement in another sense is more applicable to aid human vision where a human operator is required to make certain decisions, such as a possible infected blood vessel or cranial nerve.

        Thank you for your great efforts. You may not know how much your contribution can even change the next step of human progress.

        Comment by Michael | 2011 January 23 | Reply

    • Hi Michael,
      Thank you very much for your kind comment, by keeping your advice on image restoration I tested the code as follows,

      1. zoomed test image 2x by my code, and again zoomed out it 0.5x by my code and checked PSNR.
      2. zoomed test image 2x by built-in, and again zoomed out it by 0.5x by built-in and then checked PSNR.

      This is the result I got,

      >> im = rgb2gray(imread(‘temple.jpg’));
      >> immz = blnrm2(im,2);
      >> immr = blnrm2(immz,0.5);
      >> psnr_m = PSNR(im,immr)
      Images are identical: PSNR has infinite value

      psnr_m =

      Inf

      >> imbz = imresize(im,2,’bilinear’);
      >> imbr = imresize(imbz,.5,’bilinear’);
      >> psnr_b = PSNR(im,imbr)

      psnr_b =

      32.9920

      Thank you again for the comment..!

      Comment by Thilina S. | 2011 January 23 | Reply

      • Hi Thilina,

        I tested the other way round for your bicubic and bilinear.

        The image was 1st down-converted 2x, than upsized 2x. Did you get the same performance for self-construct and inbuilts?

        What did you change to the codes to make it right?

        Comment by Michael | 2011 January 23 | Reply

    • Hi Michael,
      I did a small modification on interpolation in lines 24 to 27, I’ll update the code now. However zoom .5x to zoom 2x has given me a different result but yet its PSNR is higher than the built-in code.
      Thank you for the reply..!!

      Comment by Thilina S. | 2011 January 23 | Reply

      • Hi Thilina,

        if you downsized with ‘nearest’ and upsized with your bilinear and bicubic. The results does not give infinite PSNR.

        could you verify this?
        thanks.

        Comment by Michael | 2011 January 23 | Reply

      • Hi Thilina,

        Here’s an example of the test for inbuilts.
        % interpolation method used
        methodUsed = {‘nearest’, ‘bilinear’, ‘bicubic’};

        schemeTitle = ‘SBI – Inbuilt’;
        fprintf(‘\n[%s] \n’, schemeTitle);
        for methodChoice = 1:length(methodUsed)
        methodChosen = char(methodUsed(methodChoice));

        % reducedSizeImage = imresize(originalImageInGray, (originalDimensions/resizingFactor), ‘nearest’); % shrink image
        reducedSizeImage = imresize(originalImageInGray, 1/resizingFactor, ‘nearest’); % shrink image
        % interpolatedImage = imresize(reducedSizeImage, originalDimensions, methodChosen); % recover image with original sizing
        interpolatedImage = imresize(reducedSizeImage, resizingFactor, methodChosen); % recover image with original sizing

        %% PSNR
        psnr_Value = PSNR(originalImageInGray, interpolatedImage);
        fprintf(‘PSNR (interpolatedImage: %s) = +%5.5f dB \n’, methodChosen, psnr_Value);

        collect = [collect psnr_Value]; % accumulate the values for comparison later
        end

        Comment by Michael | 2011 January 23 | Reply

    • Hi Micheal,
      I think when ever we zoom down an image more of details of it dissapears, assume we got 3 pixel image;
      >> immm = [0 10 20;
      30 40 50;
      60 70 80];
      >> iimm = nnbr(immm,2/3)

      iimm =

      40 50
      70 80
      Which is not the proper representation of our original image, Due to this reason we can not restore our original image again, but on the other hand; when we zoomed up;

      immm = [0 10 20;
      30 40 50;
      60 70 80];
      iimm = nnbr(immm,4/3)

      iimm =

      0 10 10 20
      30 40 40 50
      30 40 40 50
      60 70 70 80
      All image details are in our image, so we can easily reconstruct our original image back..!!
      iimmm = nnbr(iimm,3/4)

      iimmm =

      0 10 20
      30 40 50
      60 70 80

      Thank you for the comment..!

      Comment by Thilina S. | 2011 January 23 | Reply

      • Hi Thilina,

        I have considered this point. However, how I meant was.

        1. We zoom down the image and zoom up image with inbuilts.
        2. Compare with the original to check the corruption (details lost), ie. PSNR (original, interpolated_inbuilt)

        do likewise for self-construct, ie. check PSNR (original, interpolated_selfConstruct)

        The key point here is to check which version has the higher sense of corruption.

        Comment by Michael | 2011 January 23 | Reply

    • Hi Michael,
      I tested what you said,

      im = rgb2gray(imread(‘temple.jpg’));

      immz = blnrm2(im,.5);
      immr = blnrm2(immz,2);
      psnr_m = PSNR(im,immr);
      disp([‘PSNR original with self construct = ‘ num2str(psnr_m) ‘ dB’]);

      imbz = imresize(im,.5,’bilinear’);
      imbr = imresize(imbz,2,’bilinear’);
      psnr_b = PSNR(im,imbr);
      disp([‘PSNR original with buit-in = ‘ num2str(psnr_b) ‘ dB’]);

      PSNR original with self construct = 28.4131 dB
      PSNR original with buit-in = 27.7067 dB

      What i got is that PSNR of self constructed is bit higher than built-in..,
      Thank you for the comment..!

      Comment by Thilina S. | 2011 January 23 | Reply

      • Hi Thilina,

        I am packaging a benchmark module for you.
        In this package, I have renamed your functions as nearest_, bilinear_, and bicubic_. Along with it, I am also including some test images.

        Please ignore some commented lines, as I am also benchmarking other SBI versions.

        Please also comment off the ‘Bicubic spline’ section.
        The main execution is
        trial_compareInbuiltAndSelfConstructed_SBIs.m

        The link may have a pop-up. There should be a link stating SBI.rar:

        http://www.sendspace.com/file/erqj62

        I am using rar file as a zip.
        http://www.rarlab.com/

        The benchmark is giving a different outcome.

        Comment by Michael | 2011 January 24 | Reply

      • Hi Thilina,

        A comment I posted is not appearing. I posted a link for the benchmark used for the functions previously.

        http://www.sendspace.com/file/erqj62

        Comment by Michael | 2011 January 24 | Reply

      • The main executing usage is :
        The main execution is
        trial_compareInbuiltAndSelfConstructed_SBIs.m

        Please comment out the bicubic spline section.

        Thank you.

        Comment by Michael | 2011 January 24 | Reply

    • Hi Michael,
      Wordpress spam blocker is blocking comments which have more than 2 urls, Thats what happened to your previous comment. I unlocked it out. I’ll test my codes with the bench mark files downloaded from your link.
      Thank you so much for your kind responses..!!

      Comment by Thilina S. | 2011 January 24 | Reply

      • Thank you, Thilina. You are a passionate engineer. I wished my juniors were all like you.

        Comment by Michael | 2011 January 24 | Reply

    • Hi Micheal,
      I tried to run your code..,
      There was an error in line 107
      xx=csi(x1,y1);
      MATLAB states that csi() function is unknown. What did you mean by using this function, and what are the expected outcome of it? Please reply,
      Thank you..!

      Comment by Thilina S. | 2011 January 24 | Reply

      • Hi Thilina,

        that’s the cubic spline, ie. the section I mentioned to comment off.

        ps. I voted your page up. 😉

        Comment by Michael | 2011 January 24 | Reply

    • Hi Michael,
      Thank you very much for your kind appreciation 🙂 . Your motivation helped me a lot to go further more to it. I found a link about cubic spline on MATLAB central.
      http://www.mathworks.com/help/techdoc/ref/spline.html
      I’ll update after having some good results from the code.
      Thank you so much for the motivation and kind responses..!!

      Comment by Thilina S. | 2011 January 24 | Reply

    • Hi Micheal,
      I went through your code, Well, I was unable to run the bi-cubic spline section, If you can kindly describe me what you wanted to perform on that section, I think I can code it for me by myself 🙂 🙂
      And also in trial_compareInbuiltAndSelfConstructed_SBIs.m file I noted that for the test for in-built SPI test was done using down sizing the images using nearest interpolation for all 3 cases, i think it will be much better if the down size is done as same as the up sizing method. i.e nearest—> neaset, bilinear–> bilinear etc.
      Also when it used in Self built SPI, code has done down-sizing using the in-build with corresponding method, I also think that it will be much better the down-sizing is also done by the Self build codes for this case.

      Anf Finally, in PSNR feed back, in line 90
      if (abs(tolerance/collect(methodChoice))*100 < 6);
      i think it must not take the abs value, since i personally think if the PSNR of a self-built code is greater than in-built, the self built code must be better than the in-built.

      Please be kind enough for reply and add a comment for cubic spline section..!
      Thank you..!!

      Comment by Thilina S. | 2011 January 24 | Reply

      • Hi Thilina,

        The bi-cubic spline section is to be commented off, as we are not using this in your case. You should comment off that section, as stated during 1 of our discussions.

        The abs() case is only an error check. There is another portion without abs(). I am using ‘nearest’ for downsizing, because it produces the highest corruption. As the inbuilt is also using the same, the self-construct should follow the control cases.

        It should still output the comparison. Did you see that the PSNR was lower?

        Comment by Michael | 2011 January 24 | Reply

    • Hi Michael,
      Thank you very much for you kind responses.
      So what i have to check is, down-size the image in nearest and check the psnr values for in-built and self built in all 3 cases with zooming with respect the original image and compare the differecne ?
      Please advice, Thank you..!

      Comment by Thilina S. | 2011 January 24 | Reply

    • Hi Michael,
      I comment off the spline section, and also commented the line 37,

      reducedSizeImage = imresize(originalImageInGray, 1/resizingFactor, methodChosen); % shrink image
      because with this for each test image is zoomed out with mapped in-built for the self-construct and i used the reducedSizeImage which used to test in-built code and the results I obtained as follows,

      [SBI – Inbuilt]
      PSNR (interpolatedImage: nearest) = +35.58328 dB
      PSNR (interpolatedImage: bilinear) = +37.89995 dB
      PSNR (interpolatedImage: bicubic) = +38.22953 dB

      [SBI – Self construct]
      PSNR (interpolatedImage: nearest) = +35.58328 dB
      SAME PERFORMANCE.
      The self-construct is reasonably serviceable.
      Error rate: 0.00000 %
      PSNR (interpolatedImage: bilinear) = +43.37534 dB
      The self-construct outperforms the inbuilt.
      The self-construct is NOT serviceable.
      Error rate: 14.44696 %
      PSNR (interpolatedImage: bicubic) = +42.01865 dB
      The self-construct outperforms the inbuilt.
      The self-construct is NOT serviceable.
      Error rate: 9.91151 %

      I also noted, the PSNR is higher than the build-in code..!
      Thank you..!!

      Comment by Thilina S. | 2011 January 24 | Reply

    • Hi Michael,
      What did you meant by the output normalization?
      Is it;
      normalized image = 255*image/max(image);
      If its so I haven’t done it, and how do i calculate average power of image?
      Is it, sum(image^2)/size(image) ?
      How do I use regional/ over-all statistics ??
      Please reply,
      Thank you..!

      Comment by Thilina S. | 2011 January 24 | Reply

      • Hi Thilina,

        you can use mean as the average.
        For regional/ over-all statistics, the link provides an usage demo.

        It is important to check as PSNR can be misleading when .

        psnr (o, x1) < psnr (o, x2)

        where x2 = 1/2*x1.

        It gives the illusion that x2 is better, where in the case, x2 is just 1/2 the intensity of x1. In content, they are the same.

        Comment by Michael | 2011 January 25 | Reply

    • Hi Michael,
      Thank you very much for you kind responses..!
      I’ll check the statistics as you mentioned. 🙂

      Comment by Thilina S. | 2011 January 25 | Reply

      • You are very dynamic!

        Comment by Michael | 2011 January 25 | Reply

    • Hi Michael,
      Sorry for the late reply ..,
      I made some over all statistics as you mentioned above, Results are as follows,

      Loaded temple.jpg
      Zoomed x0.5 original with self construct
      Zoomed x2 original with self construct
      Zoomed x0.5 original with built-in
      Zoomed x2 original with built-in
      Original Image
      | MEAN | MEDIAN | STANDARD DEV. | VARIANCE |
      | 118.07151 | 114.00000 | 5.68571 | 217358.11018 |
      Self Constructed Image
      | MEAN | MEDIAN | STANDARD DEV. | VARIANCE |
      | 118.60968 | 115.00000 | 5.75251 | 209764.08595 |
      In-built Image
      | MEAN | MEDIAN | STANDARD DEV. | VARIANCE |
      | 118.33362 | 115.00000 | 5.72312 | 201931.11484 |

      For the regional statistics, I’m planing to use some 25×25 size windows (some 25×25 kernels to convolve with images) to obtain regional statistics. I’ll update those results soon. Thank you very much for your motivation 🙂 🙂

      Comment by Thilina S. | 2011 January 28 | Reply

  6. Hi,

    Wonderful codes, and all working. Possible to upload some code or just book title about image zooming with spline interpolation?

    Thank you,
    Best Regards,
    Szabó Hunor

    Comment by Szabo Hunor | 2011 April 17 | Reply

    • Hi,
      I haven’t worked with the spline interpolation before. I’ll do my best to upload some codes for image zooming on spline interpolation and some good resources on it as soon as possible. But I think MTLAB central itself may contain details on spline interpollation too.
      Thank you very much for the comment..

      Comment by Thilina S. | 2011 April 17 | Reply

  7. Hi,

    Speed answer, thank you. Yes the matlab contain spline interpolation function, but i can not use this functions in my application.I would be grateful if you upload a few resources when you have time.

    Thank you very much,
    Szabo Hunor

    Comment by Szabo Hunor | 2011 April 17 | Reply

    • Hi,
      I’ll do my best to study and upload a code as soon as possible,
      Thank you very much for the comment 🙂

      Comment by Thilina S. | 2011 April 17 | Reply

      • Thank you.

        Comment by Szabo Hunor | 2011 April 17 | Reply

      • Hi,

        How is it going?
        I can’t find very useful source on the internet, only your blog.

        Best Regards,
        Szabó Hunor

        Comment by Szabó Hunor | 2011 April 27 | Reply

    • Hi,
      I got some submissions at the university in these days, I’ll study and post about spline interpolation as soon as possible, sorry for any delay..
      Thank you very much for the comment.. 🙂

      Comment by Thilina S. | 2011 April 27 | Reply

  8. Thank you very much. You are very helpful.

    Comment by Szabó Hunor | 2011 April 27 | Reply

  9. Thanks for the interesting write-up about “image zooming” using different ‘interpolation’ schemes. I am fairly new to the programming, and MATLAB environment. I was wondering how I could interpolate along the 2-D image slices to increase the size of 3-D volume. For example, I have array of 2-D image slices. I have concatenated them to make a 3-D volume. I have to interpolate along the image slices to increase the size along the direction of the image slices (in MATLAB’s jargon, it is along the pages).

    Comment by Srinidhi | 2011 August 3 | Reply

    • Hi..,
      Can you update more information about your project. How many slices you use, and what is your final target..
      Thank you very much for sharing your ideas 🙂

      Comment by Thilina S. | 2011 August 3 | Reply

      • Hi Sameera, thanks for the reply.

        I have a set of uniformly discretized, 100 2-D TIFF images of resolution 480*488 pixels. I have used MATLAB to stack them to create a volumetric data. I want to visualize this volume from different orientations. After stacking, the resolution of the volume is X*Y*Z: 480*488*100. The z-axis resolution is significantly smaller compared to other two axes. To compensate for this, I need to artificially increase the z-axis resolution to make it comparable to the x- and y-axes. I am literally confused with how to accomplish this.

        Comment by Srinidhi | 2011 August 4 | Reply

  10. Hi

    What is the rest of this part? please help .

    X = [ (m-m2)*(m-m3)*(m-m4)/((m1-m2)*(m1-m3)*(m1-m4)) …
    (m-m1)*(m-m3)*(m-m4)/((m2-m1)*(m2-m3)*(m2-m4)) …
    (m-m1)*(m-m2)*(m-m4)/((m3-m1)*(m3-m2)*(m3-m4)) …
    (m-m1)*(m-m2)*(m-m3)/((m4-m1)*(m4-m2)*(m4-m3))];

    Thank you for your sharing.

    Comment by Merve | 2011 October 13 | Reply

    • Hi..,
      What do you mean by rest of this part..,
      Above equation is complete..!!

      Comment by Thilina S. | 2011 October 13 | Reply

  11. Hi. I think there is a problem with your bilinear interpolation function. If we are using integer downsizing factor, say, we reduce the image size to its 1/2, or 1/3, then your xd and yd will be zero, and the algorithm reduces to nearest neighbor. I thought about it myself but could not think of a way to solve this. Any idea? Thanks.

    Comment by puff1n | 2011 October 31 | Reply

    • Hi..,
      I’ll check that as soon as possible., thank you

      Comment by Thilina S. | 2011 November 1 | Reply

      • Hi, Thilina. I talked to a professor here, and he said this was okay. The bilinear or bicubic or … algorithm DOES reduces to simple subsampling in these cases. So there is nothing wrong with your code (that’s how I will code it), and there is nothing wrong with the principle of bilinear/bicubic interpolation, it’s that these algorithm is not terribly good/designed for downsizing images.

        By the way, I am doing a project regarding downsizing here. Do you by any chance know of any algorithm that is particularly designed for downsizing images/creating thumbnails? I will search (in fact I already have) on tomorrow, but better to know more, and sooner, if possible. 🙂

        Comment by puff1n | 2011 November 1 | Reply

    • Hi..,
      Thank you very much for your concern 🙂
      I have no idea about making thumbnail images, but i think MATLAB file exchange may help you..

      Comment by Thilina S. | 2011 November 1 | Reply

  12. i am getting “Input argument “zoom” is undefined.”

    Comment by Ravdeep | 2012 October 17 | Reply

  13. Hi,
    Nearest Neighbour Interpolation , can u explain the code more , it’s complicated and i have to understand it very well , please explain it step by step , and explain the logic of the algorithm ,,,
    i’m trying to do shrinking using Nearest Neighbour Interpolation but i really do not understand the algorithm and the code ,,,
    need help please ,,,

    Comment by Sara | 2012 October 30 | Reply

  14. Hi,
    I’d like to thank u for ur great efforts.
    I have some questions about the bi-linear function. Is the input (zoom) means the scale or the resolution? Why did u use (cast) to change into uint32?
    could u kindly explain the code like Sara asked step by step? that would be a great help..

    thanks in advance

    Comment by SbS | 2012 November 3 | Reply

  15. Do you have a spam issue on this blog; I also am a blogger, and I was wondering your situation; we have created some
    nice methods and we are looking to exchange solutions with others, why not shoot
    me an email if interested.

    Comment by Randi | 2012 December 15 | Reply

  16. I like the valuable information you provide in your articles.
    I’ll bookmark your weblog and check again here regularly. I am quite certain I’ll learn a
    lot of new stuff right here! Good luck for the next!

    Comment by Russell | 2013 February 19 | Reply

  17. Is it possible to zoom an image in frequency domain?

    Comment by Richa | 2013 May 8 | Reply

  18. Thank you very much ayye,I had to correct some places in the code,because there were some errors when I run it on my PC.Anyway it was really helpful to do Ranga Sir’s Assignment.I’m really love to do image processing.Thank you again ayye.

    Comment by Supun | 2013 July 2 | Reply

  19. hi sir u are doing really great job

    I am also trying ot improve image resolution but by using multiple low resolution frame can u please help me out its not working for rgb images

    Comment by chirayu | 2013 November 28 | Reply

  20. this is the code
    frst main function

    im=double(imread(‘limage’));

    figure,imshow(uint8(im)),title(‘original HR image’);
    shifts=[ 0, 0;
    4.1, 2.68;
    -3.7, 7.8;
    -1.1, -6.5];

    factor=4;

    im1=create_low(im,shifts(1,1),shifts(1,2),factor);
    im2=create_low(im,shifts(2,1),shifts(2,2),factor);
    im3=create_low(im,shifts(3,1),shifts(3,2),factor);
    im4=create_low(im,shifts(4,1),shifts(4,2),factor);

    LR_images={im1,im2,im3,im4};

    estimated_image = interpolate(LR_images,shifts,factor);
    figure,imshow(uint8(estimated_image)),title(‘reconstructed image’);

    now downsampling ie create_low

    function [ low ] = create_low(im,x_shift,y_shift,factor)

    low = shift(im,x_shift,y_shift);

    low=downsample(low,factor);
    low=low’;
    low = downsample(low,factor);
    low=low’;

    end

    shift function

    function C = shift(im,x_shift,y_shift)

    [M N]=size(im); %Assuming square matrix.
    [xx yy]=meshgrid(1:N,1:M); %xx,yy are both outputs of meshgrid (so called plaid matrices).
    C = interp2(xx,yy,im,xx+x_shift,yy+y_shift);

    interpolation

    function rec = interpolate(s,shifts,factor)

    n=length(s);
    ss = size(s{1});
    if (length(ss)==2) ss=[ss 1]; end

    % compute the coordinates of the pixels from the N images.
    for k=1:ss(3) % for each color channel
    for i=1:n % for each image
    s_c{i}=s{i}(:,:,k);
    s_c{i} = s_c{i}(:);
    r{i} = [1:factor:factor*ss(1)]’*ones(1,ss(2)); % create matrix with row indices
    c{i} = ones(ss(1),1)*[1:factor:factor*ss(2)]; % create matrix with column indices
    r{i} = r{i}+factor*shifts(i,2);
    c{i} = c{i}+factor*shifts(i,1);
    rn{i} = r{i}((r{i}>0)&(r{i}0)&(c{i}0)&(r{i}0)&(c{i}0)&(r{i}0)&(c{i}<=factor*ss(2)));
    end

    s_ = []; r_ = []; c_ = []; sr_ = []; rr_ = []; cr_ = [];
    for i=1:n % for each image
    s_ = [s_; sn{i}];
    r_ = [r_; rn{i}];
    c_ = [c_; cn{i}];
    end
    clear s_c r c coord rn cn sn

    % interpolate the high resolution pixels using cubic interpolation
    rec_col = griddata(c_,r_,s_,[1:ss(2)*factor],[1:ss(1)*factor]','cubic');
    rec(:,:,k) = reshape(rec_col,ss(1)*factor,ss(2)*factor);
    end
    rec(isnan(rec))=0;

    take black n white img and of same dimesions

    Comment by chirayu | 2013 November 28 | Reply

  21. Hi,
    I need to interpolate M by N RGB tiff images using bilinear interpolation the teacher asked for manhattan interpolation. I have to enlarge an image 10 times then shrink it. I need help please on how to do it?
    Thank you,
    You are mazing!
    Suzie,

    Comment by Suzie | 2013 December 3 | Reply

  22. hi i m getting error in all of three codes it says that “not enough input arguments at line 2”?please help me

    Comment by shershahkhan | 2014 February 17 | Reply

  23. can you please solve this question. coz i have to submit my assignment to my professor by tcoming monday
    Lab1-1: Zooming and Shrinking Images by Bilinear Interpolation
    (a) Write a computer program capable of zooming and shrinking an image by bilinear interpolation. The input to
    your program is the desired resolution (in dpi) of the resulting image.
    (b) Download Fig. 2.20(a) from the book web site or the link as follows, and use your program to shrink this from
    1250 dpi to 100 dpi.
    https://dl.dropboxusercontent.com/u/51640593/temp/Fig0220a_chronometer_3692_2812_2pt25_inch_1250dpi.tif
    (c) Use your program to zoom the image in (b) back to 1250 dpi. Explain the reasons for their differences.

    Comment by Amit Chauhan | 2014 October 18 | Reply

  24. in bicubic code if set zoom 1 or 2 , it wont change the size , plz check

    Comment by milad | 2014 October 31 | Reply

  25. can anyone post simple matlab code for bicubic interpolation to enhance the low resolution image?? plzz post the code .i need it soon.

    Comment by karthick | 2015 February 18 | Reply

  26. Gracious! Thanks bunches Thilina aiya. This blog is really really helpful and awesomely awesome too. Keep it up!

    Comment by Sayuru | 2015 June 10 | Reply

    • Thank you malli, so you are doing image processing!! O:) 😉

      Comment by Thilina S. | 2015 June 28 | Reply

  27. Hi
    When im passing the values in functiin execution like nnrb(‘image name’, zooming scale )
    It gives me the error below
    This statement is not inside any function.
    Im really new to this please help me with this.
    Thank you in advance

    Comment by sayad | 2015 July 30 | Reply

  28. hi

    for bilinear code which picture do you use?

    Comment by amir | 2015 August 15 | Reply

  29. hi sir
    I do not know run script as a function. Can you help me, please?

    Comment by fff | 2016 March 1 | Reply

  30. please give the algorithm to perform bicubic interpolation of matrix values to the higher size, please include calculations so that the program can be written using that in matlab

    Comment by akshaya | 2016 December 29 | Reply


Leave a reply to Michael Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.