主题:[讨论]为什么对图像做卷积运算后,图像整体右移
用双三次曲面插值,做图像放大,也可以说是曲面插值
同样用到了卷积运算,运算的结果是
图像的右移
附件中,有一副图像l.jpg可以看到右边有几纵列很明显应该在左边
周围的黑框,是因为双三次曲面插值最外圈不能插,要另外处理,这点代码我还没写
[code]
//双三次曲面插值
template<typename T>
bool cubicalhyperbola_interpolate(T * inIm,
unsigned long imW,
unsigned long imH,
T * outIm,
unsigned long imOutW,
unsigned long imOutH,
unsigned zoomX,
unsigned zoomY)
{
//循环控制,输入图像的当前位置
unsigned long inTmpPos = 0;
//循环控制,输出图像的当前位置
unsigned long outTmpPos = 0,aPos = 0;
//循环控制标记,i控制高度,j控制宽度
unsigned long i = 0,j = 0,k = 0,l = 0;
//防止越界,宽高均减去1
unsigned long imHH = imH-3;
unsigned long imWW = imW-3;
unsigned long addSize = imOutW*(zoomY-1);
//循环控制标记,x控制X(水平)方向的放大倍数,y控制Y(竖直)方向的放大倍数
unsigned long x = 0,y = 0;
unsigned kernelPos = 0;
unsigned long gridSize = (zoomY+1)*(zoomX+1);
//采样系数
double *wx[4],*wy[4],dx,dy;
wx[0] = new double[zoomX];
wx[1] = new double[zoomX];
wx[2] = new double[zoomX];
wx[3] = new double[zoomX];
wy[0] = new double[zoomY];
wy[1] = new double[zoomY];
wy[2] = new double[zoomY];
wy[3] = new double[zoomY];
for (x = 0; x < zoomX; x++)
{
dx = x/zoomX;
wx[0][x] = 2*pow(dx,2) - pow(dx,3) - dx;
wx[1][x] = pow(dx,3) + 1 - 2*pow(dx,2);
wx[2][x] = pow(dx,2) - pow(dx,3) + dx;
wx[3][x] = pow(dx,3) - pow(dx,2);
}
for (y = 0; y < zoomY; y++)
{
dy = y/zoomY;
wy[0][y] = 2*pow(dy,2) - pow(dy,3) - dy;
wy[1][y] = pow(dy,3) + 1 - 2*pow(dy,2);
wy[2][y] = pow(dy,2) - pow(dy,3) + dy;
wy[3][y] = pow(dy,3) - pow(dy,2);
}
//图像范围控制
outTmpPos = addSize + imOutW + zoomX ;
for (i = 0; i < imHH; i++)
{
for (j = 0; j < imWW; j++)
{
//插值运算
for (y = 0; y < zoomY; y++)
{
for (x = 0; x < zoomX; x++)
{
//卷积计算
kernelPos = 0;
for (k = 0; k < 4 ; k++)
{
for (l = 0; l < 4; l++)
{
outIm[outTmpPos+y*imOutW+x]
+= inIm[inTmpPos+kernelPos+l]*wy[k][y]*wx[l][x];
}
kernelPos += 4;
}
}
}
inTmpPos++;
outTmpPos += zoomX ;
}
inTmpPos += 3;
outTmpPos += (addSize + zoomX + zoomX + 1);//addSize = imOutW*zoomY;
}
//释放内存资源
delete [] wx[0];
delete [] wx[1];
delete [] wx[2];
delete [] wx[3];
delete [] wy[0];
delete [] wy[1];
delete [] wy[2];
delete [] wy[3];
return true;
}
[/code]
同样用到了卷积运算,运算的结果是
图像的右移
附件中,有一副图像l.jpg可以看到右边有几纵列很明显应该在左边
周围的黑框,是因为双三次曲面插值最外圈不能插,要另外处理,这点代码我还没写
[code]
//双三次曲面插值
template<typename T>
bool cubicalhyperbola_interpolate(T * inIm,
unsigned long imW,
unsigned long imH,
T * outIm,
unsigned long imOutW,
unsigned long imOutH,
unsigned zoomX,
unsigned zoomY)
{
//循环控制,输入图像的当前位置
unsigned long inTmpPos = 0;
//循环控制,输出图像的当前位置
unsigned long outTmpPos = 0,aPos = 0;
//循环控制标记,i控制高度,j控制宽度
unsigned long i = 0,j = 0,k = 0,l = 0;
//防止越界,宽高均减去1
unsigned long imHH = imH-3;
unsigned long imWW = imW-3;
unsigned long addSize = imOutW*(zoomY-1);
//循环控制标记,x控制X(水平)方向的放大倍数,y控制Y(竖直)方向的放大倍数
unsigned long x = 0,y = 0;
unsigned kernelPos = 0;
unsigned long gridSize = (zoomY+1)*(zoomX+1);
//采样系数
double *wx[4],*wy[4],dx,dy;
wx[0] = new double[zoomX];
wx[1] = new double[zoomX];
wx[2] = new double[zoomX];
wx[3] = new double[zoomX];
wy[0] = new double[zoomY];
wy[1] = new double[zoomY];
wy[2] = new double[zoomY];
wy[3] = new double[zoomY];
for (x = 0; x < zoomX; x++)
{
dx = x/zoomX;
wx[0][x] = 2*pow(dx,2) - pow(dx,3) - dx;
wx[1][x] = pow(dx,3) + 1 - 2*pow(dx,2);
wx[2][x] = pow(dx,2) - pow(dx,3) + dx;
wx[3][x] = pow(dx,3) - pow(dx,2);
}
for (y = 0; y < zoomY; y++)
{
dy = y/zoomY;
wy[0][y] = 2*pow(dy,2) - pow(dy,3) - dy;
wy[1][y] = pow(dy,3) + 1 - 2*pow(dy,2);
wy[2][y] = pow(dy,2) - pow(dy,3) + dy;
wy[3][y] = pow(dy,3) - pow(dy,2);
}
//图像范围控制
outTmpPos = addSize + imOutW + zoomX ;
for (i = 0; i < imHH; i++)
{
for (j = 0; j < imWW; j++)
{
//插值运算
for (y = 0; y < zoomY; y++)
{
for (x = 0; x < zoomX; x++)
{
//卷积计算
kernelPos = 0;
for (k = 0; k < 4 ; k++)
{
for (l = 0; l < 4; l++)
{
outIm[outTmpPos+y*imOutW+x]
+= inIm[inTmpPos+kernelPos+l]*wy[k][y]*wx[l][x];
}
kernelPos += 4;
}
}
}
inTmpPos++;
outTmpPos += zoomX ;
}
inTmpPos += 3;
outTmpPos += (addSize + zoomX + zoomX + 1);//addSize = imOutW*zoomY;
}
//释放内存资源
delete [] wx[0];
delete [] wx[1];
delete [] wx[2];
delete [] wx[3];
delete [] wy[0];
delete [] wy[1];
delete [] wy[2];
delete [] wy[3];
return true;
}
[/code]