Turn: Http://blog.csdn.net/swimmingfish2004/article/details/7312361Lanczos algorithm Analyse
In the company when the Lanczos image thumbnail algorithm, sorted out today to share, analysis is imagemagic inside the Lanczos resize source code.
1 Introduction to Image scaling principle
The basic principle of image scaling is to calculate the pixel value of the target image by certain rules according to the pixel value of the original image. In the process of image scaling, the most important thing is to determine the following two questions: first, when calculating each pixel value in the destination image, you should select which pixel values in the original image, and the second is how the weights of the selected pixel values are determined when calculating the destination pixel.
2 Lanczos Algorithm Introduction
Various image interpolation algorithm are generally categorized by determining the pixel weights that participate in the calculation. The mathematical formula used by the Lanczos algorithm is as follows:
We can draw an L (x) image when A=2,a=3
Assuming that our one-dimensional function is represented by F (x),I(x) to indicate that the value of n f (x) is obtained by Lanczos filter, then i(x) is calculated as follows:
Two-dimensional, three-dimensional representations are similar. Here is a two-dimensional calculation formula:
In short, the main function of our use of the Lanczos algorithm is to determine the weights of the pixel values for each participating calculation.
3 points to consider in reality 3.1 which pixel points to select when calculating the destination pixel
In the Lanczos algorithm, a rectangular area is selected to calculate the new pixel point
3.2 Where to start when selecting pixel compute points
In Lanczos algorithm, the pixels in the middle of the image are more important than the pixels on either side of the image, so when selecting the pixels used, it is 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 *ex ception)
In this function, the input: image points to the original image, columns and rows represent scaled pixel size, 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 Filtertyp Es filter,const double blur, Exceptioninfo *exception)
In this function, input: Filter refers to the type used by the scaling function, blur refers to the fuzzy system
Output: Scaled image
Complete the zoom function in this function: It is divided into four main steps:
The first step: Mister into a scaled image after the pixel empty matrix and a transition image of the pixel empty matrix, along with the original image of the pixel matrix here are a total of three pixel matrices, for example, the image of a 10*10 pixel is scaled to 6*6 pixels: This produces a three-pixel matrix: 10*10 's original pixel matrix, 6*6 Destination pixel matrix (incorrect data), 6*10 or 10*6 transition pixel matrix (incorrect data)
The second step: get the parameters of the filtering algorithm, such as the use of what interpolation algorithm, fuzzy participation is how much and so on. This is specifically called the Acquireresizefilter function to populate a resize_filter structure.
The third step is to call the Horizontalfilter function and the Verticalfilter function to calculate the scale of the row and the scale of the column, respectively.
Fourth step: 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 well-filled resize_filter structure
The main data structures are: mapping two-dimensional array, used to specify the interpolation algorithm and filter type used
Filters two-dimensional array, used to specify the function and initial value of the filter, as we choose Lanczos algorithm, the table shows that the function used is sinc, the initial value is 1.0, the sinc function is 3 sinc function, namely:
L (x) = sinc (x) *sinc (X/3) = sin (x) * sin (X/3)/3 when -3<x<3, X takes other values L (x) = 0
12345 |
resize_filter->blur //设置模糊度 resize_filter->support = 3.0 resize_filter->scale = 1.0 resize_filter->filter = sinc resize_filter->window = sinc |
4.1.4 Horizontalfilter
To calculate the X_factor,y_factor, the formula is as follows:
X_factor = horizontal pixel value after scaling/horizontal pixel value of the original picture
Y_factor = vertical pixel value after scaling/vertical pixel value of the original picture
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,magickoffsettype *quantum,exceptioninfo *exception)
The 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 called by the Getresizefilterweight function sinc function, add the corresponding calculated value
The calculation process is as follows:
In the order of calculation, the contents of all red boxes are calculated horizontally, one column to the right, and then the data of the green box is calculated horizontally and vertically.
So for the loop
123456 |
for (columns) { for (rows) { } } |
4.1.5 Verticalfilter
See Horizontalfilter
Additional Information
2.3. 4 Scaling Module
Video scaling includes two aspects of magnification (up scaling) and narrowing (Downsca Ling), while the basic method for scaling is spatial interpolation. The following formula is a general mathematical expression for interpolating an image, where G (i,j) is the pixel value of the interpolated point in the scaled image, and F (k,l) is the pixel value at the coordinates (K,L) in the original image, and H (i-k,j-l) is the interpolation basis function.
The selection of interpolation basis functions can be many, usually have two-dimensional rectangular function, linear function, three functions and S inc function, respectively, they correspond to the nearest neighbor interpolation, linear interpolation, three interpolation and ideal interpolation (actually using the S inc function truncated after interpolation), the interpolation effect is from poor to good arrangement, But the difficulty of implementation also increased in turn. In the actual processing is to use the filter to achieve the interpolation basis function, and because of the symmetry of these interpolation, it can be decomposed into two parts of transverse and longitudinal interpolation, such as a two-dimensional linear interpolation function corresponding to bilinear interpolation (Bilinear Interpo lation), three functions corresponding to the two three-time interpolation (B Icub IC Interpo lation), for the sinc interpolation function is actually multi-phase interpolation (Po lyphase interpo lation). In this paper, the multi-phase interpolation method is used to achieve image scaling, in fact 4. Multi-phase interpolation and three interpolation are almost identical in 4 domain sizes, except that the corresponding interpolation function values are slightly different. The multi-phase interpolation method is to produce the output point by Lanczos2 function phase interpolation to the field of the output point corresponding to the original image. As shown in 11.
Figure One Lanczos2 function
Assuming that G (u,v) is scaled to output a point in the image, it reverts to the nearest point of the original image is F (i,j) and the two are in the source (x, y) of the coordinates, the output point g (U,V) is the following mathematical expression, from which can be seen in fact divided into two steps to achieve vertical filtering and horizontal filtering.
There is a relational formula: I = (uxw in)/w out,j = (vxh in)/h out; x = (Uxw in)% W Out,y = (vxh in)%H out. W in and Wout are the image widths before and after zooming, and h in and h out are the image heights before and after zooming. Figure 12 is a 4x4 domain horizontal vertical phase where the horizontal phase values are PH 0,ph 1,ph 2,ph 3 and the vertical phase values are Pv0,pv1,pv2,pv3 respectively. The multi-phase filtering can be achieved by obtaining the X, Y value of the 8 phase value based on the above relation.
Figure 4x4 Field Horizontal Vertical phase
Figure 13 is a block diagram of the filter in the image scaler designed in this paper, where the vertical horizontal lookup table holds 4 different phase Lanczos2 function values.
Figure 13 filters in the image scaler
(This article transferred from the Electronic Engineering World: http://www.eeworld.com.cn/FPGA/2011/0330/article_1947_2.html)
Multi-phase image interpolation algorithm (Lanczos, sinc)