Skip to content

Downsample

gabrielazapatero edited this page May 11, 2021 · 5 revisions

Downsampling with and without anti-aliasing filter

This tool allows to downsample an image within different options. The alternatives to modify the image are by the absence or presence of an anti-aliasing filter.

Within image processing terms, downsampling is based on the reduction in spatial resolution while maintaining unchanged the two-dimensional representation. In essence, downsampling an image reduces the number of samples that can represent the signal. One of its most common implementations is to reduce storage size of images.

From the frequency domain point of view, downsampling a signal will alias the high frequency portion and the low frequency portion. Such event applied to images leads to wanting to conserve only the low-frequency portion. In order to prevent the creation of images at undesirable frequencies, the original image needs to be preprocessed, by employing an anti-aliasing filter, to eliminate the high-frequency portion so that aliasing will not occur. Such filter will prevent the copies of the signal's spectrum from being added together, hence the distortion that is characteristic of this type of procedure.

Among the settings for the design of the filter, the one implemented has been Gaussian filtering. This procedure could be compared as low-pass filtering. In general, when downsampling an image with high-frequency contents, and with the purpose of avoiding aliasing artifacts, this filter will produce some kind of "blurring".

Performance of the application

For the correct performance of the application, the user should specify two parameters: the downsampling factor and sigma.

The downsampling factor is usually an integer or a rational fraction. This factor will either multiply the sampling time or, similarly, divide the sampling rate.

Additionally, the second parameter, being sigma, represents the standard deviation of the 2-D Gaussian smoothing kernel. This parameter has been selected to be user-specified as its value is application-dependent and ,moreover, it is interesting to study its impact on the changes in the image's behavior.

+ Both the downsampling factor and sigma must be equal or greater than 1.

Once the two parameters have been specified and the execute button is pressed the resulting images will appear on the screen. Within the displays we will find 3 different images: downsampled image without anti-aliasing filter, downsampled image with anti-aliasing filter type I and downsampled image with anti-aliasing filter type II. The difference between the two utilized filters will be explained in the next section.

Let's have a closer look at the effect of sigma's value. The more we increase the standard deviation, the wider the kernel will be, leading to a more blurred image. An extremely large value will blur the image in such a way that features from the original image might be unrecognizable.

The previous explanation regarding sigma is exemplified in the following image:

MATLAB's code

Firstly, the size of the columns and rows of the image is calculated. Now that the original dimensions are known the sampling modifications can be applied to those.

        R=size(I,1);
        C=size(I,2);

Then, there will be a verifying step to see if the image is grayscale or RGB.

        [rows, columns, numberOfColorChannels] = size(I);
        if numberOfColorChannels == 1

If the image is in fact B&W the procedure will be as follows. In this first section the image will be downsampled directly without making use of a filter.

        factor= app.Minimumfactorvalue1Spinner.Value;            
        down1 = I(1:factor:R,1:factor:C, :);

The next stage will downsample the image by using a Gaussian filter that will preserve the low-frequencies of the input image. The standard deviation, also known as sigma, is a 2-element vector of positive numbers.

        sigma= [app.Minimumsigmavalue1Spinner.Value, app.Minimumsigmavalue1Spinner.Value];
        B = imgaussfilt(I,sigma);
        down2 = B(1:factor:R,1:factor:C, :);

The final approach has been to employ the MATLAB function "imresize". This function allows for downsampling by indicating the respective factor and method used. In this case, we are using a box-shaped Interpolation Kernel to avoid anti-aliasing.

        factor2= 1/factor;
        down3 = imresize(I,factor2,'box', 'Antialiasing', true);

On the other hand, if the image is RGB, each channel will be treated independently and the previous methodology will be applied to each channel individually. As an example, the code used to downsample without a filter can be seen below.

        RC = I(:,:,1);
        GC = I(:,:,2);
        BC = I(:,:,3);
        
        J_1 = RC(1:factor:R,1:factor:C, :);
        J_2 = GC(1:factor:R,1:factor:C, :);
        J_3 = BC(1:factor:R,1:factor:C, :);
        
        down1=zeros([size(J_1,1) size(J_1,2) 3]);
        down1(:,:,1) = J_1;
        down1(:,:,2) = J_2;
        down1(:,:,3) = J_3;
Clone this wiki locally