Describes several common interpolation methods and codes-double and triple Interpolation

Source: Internet
Author: User
Double and triple Interpolation

Next, we use bilinear interpolation. This is bi-cubic Interpolation. We know that bilinear interpolation is calculated by four neighboring points, this double or triple operation computes 16 neighboring points for interpolation.

Bicubicinterpolation solves for the value at a new point by analyzing the 16 datapoints surrounding
Interpolationregion, see the example below.

In the newly generated image, the pixel is f (x, y). First, the coordinates mapped to the source image are f (I + u, J + V) find the latest 16 points in the original image.

tempX = ((float)w/(w+x))*i;//final convent to original picturetempY = ((float)h/(h+y))*j;

The above two sentences calculate the ing of pixels in the new image to the position in the source image. Assume that the distance between two pixels is 1, and W/(W + x) is the proportion, then multiply I, that is, the desired position, that is, the position mapped to the source image, which must be a decimal number, as shown in the figure above, z22, Z23, z32, z33 points are the four closest to the point to be reached ,. The distance between the red point and the four closest points mapped to the source image is decimal, and the distance from other points is 1 point. The calculation method is as follows:

S () indicates the weight. The weight is calculated using the formula. The formula for calculating the weight is as follows:

Inour project we set a =-0.5, and then we will get W value is calculated as follows:

double bicubPram6(double x){//bi-cubic interpolation 6 parameter computedouble temp = 0;if(x<0){x = -x;} if(x <= 1){temp = (((float)(6.0/5.0))* x - (11.0/5.0)) * x * x + 1;}else if (x < 2) {  temp = ((-0.6 * x + (16.0/5.0)) * x - (27.0/5.0)) * x + (14.0/5.0);   }else if (x < 3) {  temp = ((0.2 * x - (8.0/5.0)) * x + (21.0/5.0)) * x - (18.0/5.0);   }return temp;}

The overall calculation code is as follows:

void bicubic6(IplImage *src,int h, int w,char *wf, int x, int y,IplImage *dst,int startX,int startY,int endX,int endY){int i,j;float tempX,tempY;int xOr, yOr,xOr2,yOr2;float aX,aY;double k1[6],k2[6];double temp[6]= {{0.0}};double s[6][6] = {{0.0}};CvScalar sf, s1;int grayValue = 0;for(i = startY; i<endY; i++){for(j = startX; j<endX; j++){tempX = ((float)w/(w+x))*i;//final convent to original picturetempY = ((float)h/(h+y))*j;xOr = floor(tempX);//get integer of originalyOr = floor(tempY);aX = tempX - xOr;//the float numberaY = tempY - yOr;for(int n = 2; n>-4; n--){k1[n+3] = bicubPram6(aY+(double)n); //compute the y parameterk2[n+3] = bicubPram6(aX+(double)n);//compute the x parameter}for(int q = 3;q>-3;q--){for(int k = -2;k<4;k++){int tx =xOr+k;int ty = yOr+q;s1 =cvGet2D(src,tx,ty); s[q+2][k+2] = s1.val[0]; temp[q+2] += k2[k+2]*s[q+2][k+2];}}int result = temp[5]*k1[0]+temp[4]*k1[1]+temp[3]*k1[2]+temp[2]*k1[3]+temp[1]*k1[4]+temp[0]*k1[5];for(int z = 0; z<6;z++){temp[z] = 0;}sf = cvGet2D(src,xOr,yOr);if(abs(result-sf.val[0])<200){sf.val[0] = result;cvSet2D(dst,i,j,sf);}}}}

The method for calculating the weights is similar to that for B-splin. I won't go into details ......

The first post, I found that it was not the same as what I thought. It may be a mess, and there is no logic ...... Ask me if you don't know.

Laplace method

The calculation method for weight is as follows:

double LagPram(double x){//lagrange interpolation parameter computedouble temp = 0;if(x<0){x = -x;} if(x <= 1){temp = (0.5 * x - 1) * x * x -0.5*x+ 1;}else if (x < 2) {  temp = ((-((float)(1.0/6.0)) * x + 1) * x - ((float)(11.0/6.0))) * x + 1;  }else temp = 0;return temp;}

Just change the place where the weights are calculated in the above program to the one of the following ~

The B-splin method is similar, that is, it does not quite understand the principle of this method ......

 

There is also a method that requires 36 points, that is, the distance to the farthest point is three points, and the surrounding point is 36.

The weight is calculated as follows:

double bicubPram8(double x){//bi-cubic interpolation 8 parameter computedouble temp = 0;if(x<0){x = -x;} if(x <= 1){temp = (((float)(67.0/56.0))* x - (123.0/56.0)) * x * x + 1;}else if (x < 2) {  temp = ((-(33.0/56.0) * x + (177.0/56.0)) * x - (75.0/14.0)) * x + (39.0/14.0);   }else if (x < 3) {  temp = (((9.0/56.0) * x - (75.0/56.0)) * x + (51.0/14.0)) * x - (45.0/14.0);   }else if (x < 4) {  temp = ((-(3.0/56.0) * x + (33.0/56.0)) * x - (15.0/7.0)) * x + (18.0/7.0);   }return temp;}

The pixel calculation method is as follows:

Void bicubic8 (iplimage * SRC, int H, int W, char * WF, int X, int y, iplimage * DST, int startx, int starty, int endx, int Endy) {int I, j; float tempx, Tempy; int XOR, yor, xor2, yor2; float ax, ay; double K1 [8], K2 [8]; double temp [8] ={{ 0.0 }}; double s [8] [8] ={ {0.0 }}; // double s [4] [4] = {0.0}; values cannot be 0 cvscalar SF, S1; int grayvalue = 0; for (I = starty; I <Endy; I ++) {for (j = startx; j <endx; j ++) {tempx = (float) W/(W + x )) * I; // final convent to original picturetempy = (float) h/(H + y) * j; XOR = floor (tempx ); // get integer of originalyor = floor (Tempy); AX = tempx-XOR; // The float numberay = Tempy-Yor; For (INT n = 3; n>-5; N --) {K1 [n + 4] = bicubpram8 (Ay + (double) n); // compute the y parameterk2 [n + 4] = bicubpram8 (AX + (double) n); // compute the X parameter} For (INT q = 4; q>-4; q --) {for (int K =-3; k <5; k ++) {int Tx = XOR + k; int ty = yor + q; S1 = cvget2d (SRC, TX, Ty ); s [q + 3] [K + 3] = s1.val [0]; temp [q + 3] + = k2 [K + 3] * s [q + 3] [K + 3];} int result = temp [7] * K1 [0] + temp [6] * K1 [1] + temp [5] * K1 [2] + temp [4] * K1 [3] + temp [3] * K1 [4] + temp [2] * K1 [5] + temp [1] * K1 [6] + temp [0] * K1 [7]; for (INT z = 0; Z <8; Z ++) {temp [Z] = 0;} Sf = cvget2d (SRC, XOR, yor ); if (ABS (result-sf.val [0]) <200) {SF. val [0] = result; cvset2d (DST, I, j, SF );}}}}

In fact, I learned a method, and others are similar.

 

 

 

Contact Us

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.