Resize

We often encounter an image of some size that we would like to convert to an image of some other size. We may want to upsize (zoom in) or downsize (zoom out) the image; we can accomplish either task by using cvResize(). This function will fit the source image exactly to the destination image size. If the ROI is set in the source image then that ROI will be resized to fit in the destination image. Likewise, if an ROI is set in the destination image then the source will be resized to fit into the ROI.

void cvResize(
   const CvArr* src,
   CvArr*       dst,
   int          interpolation = CV_INTER_LINEAR
);

The last argument is the interpolation method, which defaults to linear interpolation. The other available options are shown in Table 5-4.

Table 5-4. cvResize() interpolation options

Interpolation

Meaning

CV_INTER_NN

Nearest neighbor

CV_INTER_LINEAR

Bilinear

CV_INTER_AREA

Pixel area re-sampling

CV_INTER_CUBIC

Bicubic interpolation

In general, we would like the mapping from the source image to the resized destination image to be as smooth as possible. The argument interpolation controls exactly how this will be handled. Interpolation arises when we are shrinking an image and a pixel in the destination image falls in between pixels in the source image. It can also occur when we are expanding an image and need to compute values of pixels that do not directly correspond to any pixel in the source image. In either case, there are several options for computing the values of such pixels. The easiest approach is to take the resized pixel's value from its closest pixel in the source image; this is the effect of choosing the interpolation value CV_INTER_NN. Alternatively, we can linearly weight the 2-by-2 surrounding source pixel values according to how close they are to the destination pixel, which is what CV_INTER_LINEAR does. We can also virtually place the new resized pixel over the old pixels and then average the covered pixel values, as done with CV_INTER_AREA.[57] Finally, we have the option of fitting a cubic spline between the 4-by-4 surrounding pixels in the source image and then reading off the corresponding destination value from the fitted spline; this is the result of choosing the CV_INTER_CUBIC interpolation method.



[57] At least that's what happens when cvResize() shrinks an image. When it expands an image, CV_INTER_AREA amounts to the same thing as CV_INTER_NN.