5

I have an image size 300x300. And to faster computation time, I use downsampling it by

 I=imresize(originalImage,0.5);

After that, I want to recover it

 outputImage=imresize(I,2);

But I see that output image does not simlilar with original image. Which is best way to up and down sampling in matlab? It will return smallest error between original image and output image (after down and up sampling Thank you so much

user3336190
  • 233
  • 1
  • 6
  • 22

2 Answers2

10

You should not expect to recover the larger image exactly. If this were possible, we could losslessly compress images by storing downsampled images and the upscaling them. However, some information is lost when the image is downsample which simply cannot be recovered. If you need to downsample the image for runtime reasons, make sure to keep a copy of the original image if needed. The matlab imresize function provides a number of ways to perform interpolations. It's possible to test the error resulting from a downsample/upsample operation for a particular kernel:

I = int8(imread('rice.png'));

J = imresize(imresize(I,0.5,'box'),2,'box');
mean(abs(J(:) - I(:)))

Running this code with different kernel types yields:

box
4.132904052734375
triangle
4.843124389648438
cubic
4.094940185546875
lanczos2
4.088195800781250
lanczos3
3.948623657226562

Lanczos3 seems to have the smallest error for this image, which isn't too surprising. It's also likely to be the slowest.

yhenon
  • 4,111
  • 1
  • 18
  • 34
3

If you are focused on the special case of downsampling by half (and up sampling by 2), use impyramid:

I1 = impyramid(I0, 'reduce');
Ix = impyramid(I1, 'expand');
chappjc
  • 30,359
  • 6
  • 75
  • 132
  • It show good result. But check it does not recover same size of orginal image. You can check this code in matlab: >> I0=[1 2 3 4;5 6 6 6; 7 6 5 6; 2 4 6 8];>>I1 = impyramid(I0, 'reduce'); >> Ix = impyramid(I1, 'expand'); Ix size is only 3x3. It does not look like I0 size 4x4. How to fix it? Thank sir – user3336190 Mar 26 '14 at 05:41
  • @user3336190 Honestly, I've only ever used `reduce` so I never noticed in the docs that `expand` gives `(2*M-1)-by-(2*N-1)`. Not sure if there's a workaround, sorry. – chappjc Mar 26 '14 at 05:56
  • Chappjc:No problem. Do you have any idea for down and up sampling with small error? I only know one method that is imresize() function – user3336190 Mar 26 '14 at 05:59
  • @user3336190 `imresize` is a good method, but be aware that anti-aliasing when down-sampling may or may not be beneficial in terms of error. For reference, if you want to do the sampling manually (with `interp2`), see [my `resizeim` function from a previous post](http://stackoverflow.com/a/20009314/2778484). Except for the edges, which get special treatment in `imresize`, the output is equivalent to `imresize` with anti-aliasing disabled. – chappjc Mar 26 '14 at 06:15