Research on ImageWarping Deformation Algorithm---Inverse Distance Weighted Interpolation (IDW)

Reference paper: Image Warping with Scattered Data Interpolation

The Inverse distance weighted interpolation algorithm (IDW) is actually based on the given control point pair and the displacement vector (direction and distance) of the control point pair to calculate the inverse distance weighted influence of the control point on the surrounding pixels, so as to realize each pixel of the image. displacement of the point.


The code is implemented as follows:

//IDW:Inverse distgance weighted interpolation method
//Reference:Image Warping with Scattered Data Interpolation
int f_IDW(unsigned char* srcData, int width, int height, int stride, int inputPoints[], int outputPoints[], int pointNum)
{
	int ret = 0;
	unsigned char* pSrc = srcData;
	int aa, bb, cc, dd, pos, pos1, xx, yy;
    double r1, r2, r3;
	unsigned char *pSrcL1;
    unsigned char *pSrcL2;
	unsigned char* tempData = (unsigned char*)malloc(sizeof(unsigned char)* height * stride);
	memcpy(tempData, srcData, sizeof(unsigned char) * height * stride);
	unsigned char* p = tempData;
	double w = 0, x_in, y_in, sumw, v;
	int u = 3;
	for(int j = 0; j < height; j++)
	{
		for(int i = 0; i < width; i++)
		{
			x_in = 0, y_in = 0;	
			//F function compute
			sumw = 0;
			for(int k = 0; k < pointNum; k++)
			{
				int t = (k << 1);
				v = 1.0 / (pow((double)((i - inputPoints[t]) * (i - inputPoints[t]) + (j - inputPoints[t + 1]) * (j - inputPoints[t + 1])),u));
				sumw += v;
				if(i == inputPoints[t] && j == inputPoints[t + 1])
					w = 1.0;
				else
				    w = v;		
				x_in += w * (outputPoints[t] + i - inputPoints[t]);
				y_in += w * (outputPoints[t + 1] + j - inputPoints[t + 1]);
			}
			w = 1.0 / sumw;
			x_in = x_in * w;
			y_in = y_in * w;
			//interpolation
			x_in = CLIP3(x_in, 0, width - 1);
			y_in = CLIP3(y_in, 0, height - 1);
			xx = (int)x_in;
            yy = (int)y_in;
			pSrcL1 = p + yy * stride;
            pSrcL2 = p + CLIP3((yy + 1), 0, height - 1) * stride;
			pos = (xx << 2);
            aa = pSrcL1[pos];
            bb = pSrcL1[pos + 4];
            cc = pSrcL2[pos];
            dd = pSrcL2[pos + 4];
            r1 = aa + (bb - aa) * (x_in - xx);
            r2 = cc + (dd - cc) * (x_in - xx);
            r3 = r1 + (r2 - r1) * (y_in - yy);
            pSrc[0]=(unsigned char)(CLIP3(r3, 0, 255));//B
            aa = pSrcL1[pos + 1];
            bb = pSrcL1[pos + 4 +1];
            cc = pSrcL2[pos + 1];
            dd = pSrcL2[pos + 4 + 1];
            r1 = aa + (bb - aa) * (x_in - xx);
            r2 = cc + (dd - cc) * (x_in - xx);
            r3 = r1 + (r2 - r1) * (y_in - yy);
            pSrc[1]=(unsigned char)(CLIP3(r3, 0, 255));//G
            aa = pSrcL1[pos + 2];
            bb = pSrcL1[pos + 4 + 2];
            cc = pSrcL2[pos + 2];
            dd = pSrcL2[pos + 4 + 2];
            r1 = aa + (bb - aa) * (x_in - xx);
            r2 = cc + (dd - cc) * (x_in - xx);
            r3 = r1 + (r2 - r1) * (y_in - yy);
            pSrc[2]=(unsigned char)(CLIP3(r3, 0, 255));//R
            aa = pSrcL1[pos + 3];
            bb = pSrcL1[pos + 4 + 3];
            cc = pSrcL2[pos + 3];
            dd = pSrcL2[pos + 4 + 3];
            r1=aa + (bb - aa) * (x_in - xx);
            r2=cc + (dd - cc) * (x_in - xx);
            r3=r1 + (r2 - r1) * (y_in - yy);
            pSrc[3]=(unsigned char)(CLIP3(r3, 0, 255));//A
		   pSrc += 4;
		}
	}
	free(tempData);
	return ret;
};

Here I take the face-lifting effect as an example:


In this picture, the blue point is the original point pair, and the red point is the control point. Here, only the two blue points of the face are offset, and the red control point is obtained. The effect after processing by the IDW algorithm is as follows :


As you can see, the face has become thinner, while the changes in other places are relatively small (the farther away from the face, the smaller the change).

The above is the whole process of the IDW algorithm. The IDW algorithm can not only do face-lifting, but also do some facial deformation special effects, which will not be shown in detail.

About the pictures, from the Internet, if there is any infringement, please let me know.

I QQ1358009172

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774016&siteId=291194637