双线性插值正弦图像失真 C++
Image distortion with sine with bilinear interpolation c++
我正在尝试使用 sine/cosine 和下一个代码的双线性插值进行失真,我保持新图像与旧图像的大小相同。结果看起来不太好。
Mat biliniar(Mat old, int freq){
namedWindow( "car2", CV_WINDOW_AUTOSIZE );
imshow( "car2", old);
int height = old.size().height;
int width = old.size().width;
unsigned char R1,R2,R3,R4; // Colours at the four neighbours
unsigned char G1,G2,G3,G4;
unsigned char B1,B2,B3,B4;
double RT1, GT1, BT1; // Interpolated colours at T1 and T2
double RT2, GT2, BT2;
unsigned char R,G,B; // Final colour at a destination pixel
unsigned char *dst; // Destination image - must be allocated here!
int x,y; // Coordinates on destination image
double fi,fj; // Corresponding coordinates on source image
Mat res(height,width,CV_8UC3,cv::Scalar(100));
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
fi = i;
fj = (j - (((height/30)*(sin((M_PI*i)/(freq/2)))))) + sqrt(height) ;
//cout << fj << " ";
if(fj > 0 & fj < height){
//cout << " p1: x:" << floor(fi) << " y:" << floor(fj);
//cout << " p2: x:" << ceil(fi) << " y:" << floor(fj);
//cout << " p3: x:" << floor(fi) << " y:" << ceil(fj);
//cout << " p4: x:" << ceil(fi) << " y:" << ceil(fj);
Vec3b color1 = old.at<Vec3b>(Point(floor(fi)-1,floor(fj)));
R1 = color1.val[0]; G1 = color1.val[1]; B1 = color1.val[2];
Vec3b color2 = old.at<Vec3b>(Point(ceil(fi)+1,floor(fj)));
R2 = color2.val[0]; G2 = color2.val[1]; B2 = color2.val[2];
Vec3b color3 = old.at<Vec3b>(Point(floor(fi)-1,ceil(fj)));
R3 = color3.val[0]; G3 = color3.val[1]; B3 = color3.val[2];
Vec3b color4 = old.at<Vec3b>(Point(ceil(fi)+1,ceil(fj)));
R4 = color4.val[0]; G4 = color4.val[1]; B4 = color4.val[2];
RT1 = (R1+R2)/2; GT1 = (G1+G2)/2; BT1 = (B1+B2)/2;
RT2 = (R3+R4)/2; GT2 = (G3+G4)/2; BT2 = (B3+B4)/2;
R=(unsigned char)(RT1+RT2);
G=(unsigned char)(GT1+GT2);
B=(unsigned char)(BT1+BT2);
Vec3b finalColor(R,G,B);
res.at<Vec3b>(j,i) = finalColor;
}
}
}
namedWindow( "bilinear", CV_WINDOW_AUTOSIZE );
imshow( "bilinear", res);
return res;
}
最近邻插值的结果好得多,有人知道为什么吗?
谢谢
您实际上并没有在该代码中进行任何双线性插值,您只是将所有四个像素加在一起。
您需要计算 fi 和 fj 的小数部分并使用它们执行插值,例如:
double fi_part = fi - floor(fi);
double fj_part = fj - floor(fj);
// perform interpolation in i direction:
RT1 = (R1*(1.0-fi_part)) + (R2*fi_part);
GT1 = (G1*(1.0-fi_part)) + (G2*fi_part);
BT1 = (B1*(1.0-fi_part)) + (B2*fi_part);
RT2 = (R3*(1.0-fi_part)) + (R4*fi_part);
GT2 = (G3*(1.0-fi_part)) + (G4*fi_part);
BT2 = (B3*(1.0-fi_part)) + (B4*fi_part);
// perform interpolation in j direction:
R=(unsigned char)(RT1*(1.0-fj_part)+RT2*fj_part);
G=(unsigned char)(GT1*(1.0-fj_part)+GT2*fj_part);
B=(unsigned char)(BT1*(1.0-fj_part)+BT2*fj_part);
Vec3b finalColor(R,G,B);
res.at<Vec3b>(j,i) = finalColor;
因此,随着 fi_part 在您移动一个纹素时从 0.0 增加到 1.0,越来越少的 R1、G1、B1 和越来越多的 R2、G2、B2 将有助于 RT1、GT1 ,BT1。对于 RT2 等也是如此,对于涉及 fj_part.
的计算也是如此
代码在速度方面可以改进很多,但希望您已经掌握了基本思想。
我正在尝试使用 sine/cosine 和下一个代码的双线性插值进行失真,我保持新图像与旧图像的大小相同。结果看起来不太好。
Mat biliniar(Mat old, int freq){
namedWindow( "car2", CV_WINDOW_AUTOSIZE );
imshow( "car2", old);
int height = old.size().height;
int width = old.size().width;
unsigned char R1,R2,R3,R4; // Colours at the four neighbours
unsigned char G1,G2,G3,G4;
unsigned char B1,B2,B3,B4;
double RT1, GT1, BT1; // Interpolated colours at T1 and T2
double RT2, GT2, BT2;
unsigned char R,G,B; // Final colour at a destination pixel
unsigned char *dst; // Destination image - must be allocated here!
int x,y; // Coordinates on destination image
double fi,fj; // Corresponding coordinates on source image
Mat res(height,width,CV_8UC3,cv::Scalar(100));
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
fi = i;
fj = (j - (((height/30)*(sin((M_PI*i)/(freq/2)))))) + sqrt(height) ;
//cout << fj << " ";
if(fj > 0 & fj < height){
//cout << " p1: x:" << floor(fi) << " y:" << floor(fj);
//cout << " p2: x:" << ceil(fi) << " y:" << floor(fj);
//cout << " p3: x:" << floor(fi) << " y:" << ceil(fj);
//cout << " p4: x:" << ceil(fi) << " y:" << ceil(fj);
Vec3b color1 = old.at<Vec3b>(Point(floor(fi)-1,floor(fj)));
R1 = color1.val[0]; G1 = color1.val[1]; B1 = color1.val[2];
Vec3b color2 = old.at<Vec3b>(Point(ceil(fi)+1,floor(fj)));
R2 = color2.val[0]; G2 = color2.val[1]; B2 = color2.val[2];
Vec3b color3 = old.at<Vec3b>(Point(floor(fi)-1,ceil(fj)));
R3 = color3.val[0]; G3 = color3.val[1]; B3 = color3.val[2];
Vec3b color4 = old.at<Vec3b>(Point(ceil(fi)+1,ceil(fj)));
R4 = color4.val[0]; G4 = color4.val[1]; B4 = color4.val[2];
RT1 = (R1+R2)/2; GT1 = (G1+G2)/2; BT1 = (B1+B2)/2;
RT2 = (R3+R4)/2; GT2 = (G3+G4)/2; BT2 = (B3+B4)/2;
R=(unsigned char)(RT1+RT2);
G=(unsigned char)(GT1+GT2);
B=(unsigned char)(BT1+BT2);
Vec3b finalColor(R,G,B);
res.at<Vec3b>(j,i) = finalColor;
}
}
}
namedWindow( "bilinear", CV_WINDOW_AUTOSIZE );
imshow( "bilinear", res);
return res;
}
最近邻插值的结果好得多,有人知道为什么吗?
谢谢
您实际上并没有在该代码中进行任何双线性插值,您只是将所有四个像素加在一起。
您需要计算 fi 和 fj 的小数部分并使用它们执行插值,例如:
double fi_part = fi - floor(fi);
double fj_part = fj - floor(fj);
// perform interpolation in i direction:
RT1 = (R1*(1.0-fi_part)) + (R2*fi_part);
GT1 = (G1*(1.0-fi_part)) + (G2*fi_part);
BT1 = (B1*(1.0-fi_part)) + (B2*fi_part);
RT2 = (R3*(1.0-fi_part)) + (R4*fi_part);
GT2 = (G3*(1.0-fi_part)) + (G4*fi_part);
BT2 = (B3*(1.0-fi_part)) + (B4*fi_part);
// perform interpolation in j direction:
R=(unsigned char)(RT1*(1.0-fj_part)+RT2*fj_part);
G=(unsigned char)(GT1*(1.0-fj_part)+GT2*fj_part);
B=(unsigned char)(BT1*(1.0-fj_part)+BT2*fj_part);
Vec3b finalColor(R,G,B);
res.at<Vec3b>(j,i) = finalColor;
因此,随着 fi_part 在您移动一个纹素时从 0.0 增加到 1.0,越来越少的 R1、G1、B1 和越来越多的 R2、G2、B2 将有助于 RT1、GT1 ,BT1。对于 RT2 等也是如此,对于涉及 fj_part.
的计算也是如此代码在速度方面可以改进很多,但希望您已经掌握了基本思想。