# Multi-phase (phase) image interpolation algorithm, Image Scaling

Source: Internet
Author: User

From: http://blog.csdn.net/swimmingfish2004/article/details/7312361

Lanczos Algorithm analyse

The Lanczos image scaling algorithm, which we studied at the company, is sorted out today to share with you the source code of Lanczos resize in imagemagic.

1 Overview of image scaling principles

The basic principle of image scaling is to calculate the pixel value of the Target Image Based on the pixel value of the original image based on certain rules. In the process of image scaling, the most important issue is to determine the following two questions: first, when calculating each pixel value in the target image, which pixel values should be selected in the original image; the second is how to determine the weights of these selected pixel values when calculating the target pixel.

2 Lanczos Algorithm Introduction

Image Interpolation Algorithm is usually classified based on the method of determining the pixel weights involved in calculation. The mathematical formula used by the lanczos algorithm is as follows:

We can plot the L (x) image when a = 2, A = 3.

Assume that our one-dimensional functions are represented by F (x,I(X) To obtain the value of n f (x) values through Lanczos filterI(X) Is calculated as follows:

The representation of two and three dimensions is similar. Here is a two-dimensional formula:

In short, we use the lanczos algorithm to determine the weight of each pixel value involved in calculation.

3. In reality, we need to consider which pixels should be selected when calculating the target pixel at point 3.1.

In the lanczos algorithm, a rectangular area is selected to calculate new pixels.

3.2 where to start when pixel computing points are selected

In lanczos algorithm, the pixels in the middle of the image should be more important than those on both sides of the image. Therefore, the pixels used are selected from the middle.

4 Analysis of Lanczos Algorithm Implementation in magicimage 4.0 most important files

Magic/resize. c

4.1 main process call 4.1.1 zoomimage

Magickexport image * zoomimage (const image * image, const unsigned long columns, const unsigned long rows, exceptioninfo * exception)

In this function, input: image points to the original image, columns and rows indicate the scaled pixel size, and output: scaled Image

Call the resizeimage function in zoomimage

4.1.2 resizeimage

Magickexport image * resizeimage (const image * image, const unsigned long columns, const unsigned long rows, const filtertypes filter, const double blur, exceptioninfo * exception)

In this function, the input: Filter refers to the type used by the scaling function, and blur refers to the fuzzy system.

Output: scaled Image

Complete the scaling function in this function: There are four steps:

Step 1: form a scaled image's pixel empty matrix and a transitional image's pixel empty matrix. Together with the original image's pixel matrix, there are three pixel matrices, for example, scale A 10*10 pixel image to a 6*6 pixel image: Three pixel matrices are generated: 10*10 original pixel matrices, 6*6 target Pixel matrix (incorrect data), 6*10 or 10*6 transition Pixel matrix (incorrect data)

Step 2: Obtain the parameters of the filter algorithm, such as the interpolation algorithm used and the number of obfuscation parameters. In this case, the acquireresizefilter function is called to fill in a resize_filter structure.

Step 3: Call the horizontalfilter and verticalfilter functions to calculate the row scaling and column scaling respectively.

Step 4: return the scaled Pixel matrix and release the corresponding memory structure

4.1.3 acquireresizefilter

Magickexport resizefilter * acquireresizefilter (const image * image, const filtertypes filter, const magickrealtype blur, const magickbooleantype cylindrical, exceptioninfo * exception)

Output in this function: A filled resize_filter Structure

The main data structures are: Mapping two-dimensional array, which is used to specify the interpolation algorithm and filter type used.

The filters two-dimensional array is used to specify the functions and initial values used by the filter. For example, we use Lanczos Algorithm. From the table, we can see that the used function is sinc and the initial value is 1.0, the sinc function used is a 3 sinc function, that is:

L (x) = SiNc (x) * SiNc (X/3) = sin (x) * sin (X/3)/3 when-3 <x <3, when X is used to obtain other values, L (x) = 0.

 12345 `resize_filter->blur``// Sets the Blur.``resize_filter->support = 3.0``resize_filter->scale = 1.0``resize_filter->filter = sinc``resize_filter->window = sinc`
4.1.4 horizontalfilter

Calculate x_factor and y_factor. The formula is as follows:

X_factor = scaled horizontal pixel value/original horizontal pixel value

Y_factor = scaled vertical pixel value/original image vertical pixel value

In this example, x_factor = 0.6, y_factor = 0.6

Static magickbooleantype horizontalfilter (const resizefilter * resize_filter, const image * image, image * resize_image, const magickrealtype x_factor, const magicksizetype span, interval * quantum, exceptioninfo * exception)

Input of this function: Quantum = 0, span = 6 + 6

Image_view and resize_view. These two values are related to the CMYK color space.

Contributions array: This array is responsible for loading the filter, that is, the value of this array is added by the getresizefilterweight function to call the Sinc function.

The calculation process is as follows:

In the order of calculation, the content of all the red boxes is calculated horizontally, and a column is shifted to the right. Then, the data of the Green Box is calculated horizontally, that is, the data is first horizontally and then vertically.

Therefore

 123456 `for` `（columns）``{``    ``for``(rows)``        ``{``        ``}``}`
4.1.5 verticalfilter

Zoom Module
Video scaling includes up scaling and downgrading. The basic method for scaling is spatial interpolation. The following is a general mathematical expression for interpolation of the image. G (I, j) is the pixel value of the point to be interpolated in the scaled image. F (K, L) it is the pixel value at the coordinate (K, L) in the original image, and H (I-K, J-l) is the basis function of interpolation.

There are many kinds of interpolation functions, such as two-dimensional rectangular functions, linear functions, cubic functions, and s Inc functions, they correspond to the nearest neighbor interpolation, linear interpolation, cubic Interpolation, and ideal interpolation respectively (in reality, the S Inc function is used to cut off and then interpolation). The interpolation effect is arranged from difference to good arrangement, however, the implementation difficulty is also increased in turn. In practice, filters are used to implement Interpolation Basis Functions. Due to the symmetry of these interpolation functions, the interpolation functions can be divided into two parts: horizontal and vertical interpolation, for example, two-dimensional linear interpolation functions correspond to Bilinear interpo lation, and the three functions correspond to Bilinear interpo lation ), the sinc interpolation function is actually a multi-phase interpolation (PO lyphase interpo lation ). This article uses the multi-phase interpolation method to scale the image. In fact, in the 4? In area 4, multi-phase interpolation and cubic Interpolation are almost the same, but the corresponding interpolation function values are slightly different. The multi-phase interpolation method generates the output point by performing the lanczos2 function phase-shifting Interpolation on the field corresponding to the output point in the source image. 11.

Figure 11 lanczos2 Function
Assume that G (u, v) is a point in the scaled output image, and the closest point to the original image is f (I, j), and the difference between the two in the original image (X, y), then the mathematical expression of the output point G (u, v) is as follows. It can be seen that vertical filtering and horizontal filtering are implemented in two steps.

Relational formula: I = (u x W in)/W out, j = (V x H in)/H out; X = (u x W in) % W out, y = (V × H in) % H out. W in and wout are the image width before and after scaling, and h in and H out are the image height before and after scaling, respectively. Figure 12 shows a 4x4 horizontal vertical phase. The horizontal Phase values are respectively pH 0, pH 1, pH 2, pH 3, and the vertical Phase values are pv0, pv1, respectively, pv2, pv3. As long as X is obtained based on the above relationship, the Y value can obtain eight Phase values, and multi-phase filtering can be implemented.

Figure 12 4x4 horizontal vertical phase
Figure 13 shows the filter block diagram in the image scale device designed in this article. The vertical horizontal search table contains four lanczos2 function values of different phases.

Figure 13 filter in image Scale

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.