Best way to resize an image

What’s the best way to resize an image? I need to do barcode reading and the images from the camera do have a fairly high resolution. I suspect using lower images will lead to shorter reading times… :clock:

Hi!

The answer to your question very strongly depends on the criteria by which you judge whether a method is the best method. Speed? (Perceived) image quality? Price? An application-specific tradeoff between those three?

A (probably incomplete) list of options:

Hardware

Quite a few pieces of image acquisition equipment today have built-in processing capabilities, some of which may be useful for implementing resizing of an image in hardware. If you happen to work with one of these (for example Silicon Software frame grabbers with Visual Applets support) this is easily the fastest way to rescale your images at decent image quality and no CPU cycles at all spent on the scaling.

Drawback: The devices capable of doing that usually are not found among the cheapest but in many applications well worth their money.

Up-/Downsampling (CreateImageMap)

Another very quick way to resize your images is to use CreateImageMap to up- or downsample them. CreateImageMap (CVCImg.dll) creates a new image from an existing one by simply assembling a new (and potentially different) view (i.e. VPAT) to the same block of memory. Example: You have an image with width w and height h and want to downsample it to half the width and height. With the correct set of parameters CreateImageMap will create a new image object with width w/2 and height h/2 which refers to the memory of the input image, whose VPATs are half as long as those from the original image and have only every other entry - at virtually no processing time at all and no extra cost.

Drawback: The downsampled images are not in any way interpolated. Instead the effect is achieved by simply dropping pixels. This form of subsampling is not unlikely to lead to visible artifacts in the result image. But downscaling by a factor of should still be good enough for the Barcode reader to work with the image (as long as the module size doesn’t become too small), making it from what I understood a good choice for your use case.

Interpolated Transformation (CreateMatrixTransformedImage)

The CVCImg.dll exports a function called CreateMatrixTransformedImage which - when called with the correct matrix:

will - when used with CreateMatrixTransformedImage downscale your images by a factor of 2 and apply a bilinear interpolation, making the results look nicer than what CreateImageMap outputs.

Drawbacks: Processing time is significantly higher than with CreateImageMap.

Interpolated Transformation (ResizeImage)

The function ResizeImage from the CVFoundation.dll is quite a bit faster than CreateMatrixTransformedImage (but nowhere as fast as CreateImageMap) and allows for a selectable interpolation algorithm, potentially giving even better result images than CreateMatrixTransformedImage.

Drawbacks: A :cvb: Foundation Package license is required for this approach.

FFT Based Scaling

Theoretically the best approach for scaling images is to use Fourier transforms. The above examples all add artifacts to some extent. In terms of Fourier analysis this means the introduction of new frequencies or the reduction of present frequencies. A Fourier transform based scaling doesn’t do that - it is almost completely artifact-free.

The approach is fairly simple: Transform your images into a Fourier representation (2D frequency). For upscaling enlarge this Fourier representation by adding zeroes around the spectrum, for downscaling reduce the Fourier representation by removing lines and columns from the spectrum’s outer edges. Transferring the image back into the 2D space domain will yield an image that was enlarged/downscaled accordingly - with the addition of new frequencies or removal of frequencies other than those which were too high for the new resolution anyway.

The quality of the result images even when upscaling by a large factor are stunning. However, even the seemingly perfect Fourier transform based scaling does introduce artifacts at the outer edges of the images (which may e.g. be suppressed by using a windowing function). If you are interested in this approach you should have a look at the VCFFTScaling sample program that ships with Common Vision Blox.

Disadvantage: Highest processing time of all the scaling methods listed here; license for :cvb: Foundation Package required.

1 Like

Almost too comprehensive reply… :dizzy_face: I think CreateImageMap will do just fine for me.