Sunday, February 21, 2010

Video Spatial Transformation - Interpolation

Problems arise with the resizing of video. By default, imresize uses nearest-neighbor interpolation to determine the values of pixels in the output image, but you can specify other interpolation methods.

Interpolation is the process used to estimate an image value at a location in between image pixels.

For example, if you resize an image so it contains more pixels than it did originally, the toolbox uses interpolation to determine the values for the additional pixels. The imresize and imrotate geometric functions use two-dimensional interpolation as part of the operations they perform. The improfile image analysis function also uses interpolation.

The interpolation methods all work in a fundamentally similar way. In each case, to determine the value for an interpolated pixel, they find the point in the input image that the output pixel corresponds to. They then assign a value to the output pixel by computing a weighted average of some set of pixels in the vicinity of the point. The weightings are based on the distance each pixel is from the point.

The methods differ in the set of pixels that are considered:
  • For nearest-neighbor interpolation, the output pixel is assigned the value of the pixel that the point falls within. No other pixels are considered.
  • For bilinear interpolation, the output pixel value is a weighted average of pixels in the nearest 2-by-2 neighborhood.
  • For bicubic interpolation, the output pixel value is a weighted average of pixels in the nearest 4-by-4 neighborhood.
The number of pixels considered affects the complexity of the computation. Therefore the bilinear method takes longer than nearest-neighbor interpolation, and the bicubic method takes longer than bilinear. However, the greater the number of pixels considered, the more accurate the effect is, so there is a tradeoff between processing time and quality.

Heres the list of argument values you can use:
  1. 'nearest' Nearest-neighbor interpolation (the default)
  2. 'bilinear' Bilinear interpolation
  3. 'bicubic' Bicubic interpolation

Example for Images:

I = imread('circuit.tif');
J = imresize(I ,[100 150],'bilinear');
imshow(I)
figure, imshow(J)


In Videos:

fin = 'rawVideo.avi'; fout = 'test.avi'; fileinfo = aviinfo(fin); nframes = fileinfo.NumFrames; aviobj = avifile(fout, 'compression', 'none', 'fps',fileinfo.FramesPerSecond); for i = 1:20 %Read frames from input video mov_in = aviread(fin,i); im_in = frame2im(mov_in); %Do processing on each frame of the video
%----------------------------------------------------------------------

%In this example - Image Resize
using interpolation
im_out = imresize(im_in,1.5,'nearest'); %----------------------------------------------------------------------
%Write frames to output video
frm = im2frame(im_out); aviobj = addframe(aviobj,frm); %i %Just to display frame number end; %Don't forget to close output file aviobj = close(aviobj); msgbox('DONE'); return;

the Results:


Image 1: Nearest


Image 2: Bilinear


Image 3: Bicubic

No comments:

Post a Comment