0

I'm trying to do an distortion with sine/cosine on the y axis with Cubic interpolation with the next code, I'm keeping the same size of the new image as the old image . And the results doesn't look good. I'm doing the interpolation only on the y axis. I did the calculation on base of this Math

Mat cubic(Mat old, int freq){
int height = old.size().height;
int width = old.size().width;
int p0,p1,p2,p3;
int Bp0,Bp1,Bp2,Bp3;
int Gp0,Gp1,Gp2,Gp3;
int Rp0,Rp1,Rp2,Rp3;
unsigned char B,G,R;

double Ba,Bb,Bc,Bd;
double Ga,Gb,Gc,Gd;
double Ra,Rb,Rc,Rd;
double fi,fj;
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) ;
        //Get the indexes of the for neighbors 
         if(fj > 20 &  fj < height){
        p1 = floor(fj);
        p2 = ceil(fj);
        p0 = p1-1;
        p3 = p2+1;
        //get the color values
        Vec3b p0Color = old.at<Vec3b>(Point(floor(fi),p0));
        Vec3b p1Color = old.at<Vec3b>(Point(floor(fi),p1)); 
        Vec3b p2Color = old.at<Vec3b>(Point(floor(fi),p2)); 
        Vec3b p3Color = old.at<Vec3b>(Point(floor(fi),p3));
        //cout <<" fj:" <<fj<< " p0: " << p0 <<" p1: "<< p1 << " p2: "<<p2 <<" p3:" <<p3;

        Bp0 = p0Color.val[0]; Bp1 = p1Color.val[0]; Bp2 = p2Color.val[0]; Bp3 = p3Color.val[0]; 
        Ba = -0.5*Bp0 + 1.5*Bp1 - 1.5*Bp2 + 0.5*Bp3;
        Bb = Bp0 - 2.5*Bp1 + 2*Bp2 -0.5*Bp3;
        Bc = -0.5*Bp0 + 0.5*Bp2;
        Bd = Bp1;

        Gp0 = p0Color.val[1]; Gp1 = p1Color.val[1]; Gp2 = p2Color.val[1]; Gp3 = p3Color.val[1]; 
        Ga = -0.5*Gp0 + 1.5*Gp1 - 1.5*Gp2 + 0.5*Gp3;
        Gb = Gp0 - 2.5*Gp1 + 2*Gp2 -0.5*Gp3;
        Gc = -0.5*Gp0 + 0.5*Gp2;
        Gd = Gp1;

        Rp0 = p0Color.val[2]; Rp1 = p1Color.val[2]; Rp2 = p2Color.val[2]; Rp3 = p3Color.val[2]; 
        Ra = -0.5*Rp0 + 1.5*Rp1 - 1.5*Rp2 + 0.5*Rp3;
        Rb = Rp0 - 2.5*Rp1 + 2*Rp2 -0.5*Rp3;
        Rc = -0.5*Rp0 + 0.5*Rp2;
        Rd = Rp1;

        B = floor(Ba*pow(fj,3) + Bb*pow(fj,2) + Bc*fj + Bd);
        G = floor(Ga*pow(fj,3) + Gb*pow(fj,2) + Gc*fj + Gd);
        R = floor(Ra*pow(fj,3) + Rb*pow(fj,2) + Rc*fj + Rd);
        if(B==0){
            cout <<"list:" << Ba <<" " << Bb <<" "<< Bc <<" " << Bd;
        }
        Vec3b finalColor(B,G,R);
        res.at<Vec3b>(j,i) = finalColor;
    }   
    }
}
namedWindow( "cubic", CV_WINDOW_AUTOSIZE );
imshow( "cubic", res); 
return res;

} The origin photo enter image description here

The bilinear interpolation enter image description here

The cubic interpolation enter image description here

Thanks

3
  • Integer overflow, perhaps? Mar 30, 2015 at 12:11
  • Its true there is an integer overflow on the edges of segments. I d'ont know how to solve it. Mar 30, 2015 at 12:34
  • Maybe change the quantization range by some factor? You could divide every pixel by 2 (or some constant) before the calculation, and afterwards multiply all of them by 2 again. Mar 30, 2015 at 12:57

1 Answer 1

0

Problem solved.

Mat cubic(Mat old, int freq){
int height = old.size().height;
int width = old.size().width;
int p0,p1,p2,p3;
int Bp0,Bp1,Bp2,Bp3;
int Gp0,Gp1,Gp2,Gp3;
int Rp0,Rp1,Rp2,Rp3;
double B,G,R;

double fi,fj;
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) ;
        //Get the indexes of the for neighbors 
         if(fj > 20 &  fj < height){
        p1 = floor(fj);
        p2 = ceil(fj);
        p0 = p1-1;
        p3 = p2+1;
        //get the color values
        Vec3b p0Color = old.at<Vec3b>(Point(floor(fi),p0));
        Vec3b p1Color = old.at<Vec3b>(Point(floor(fi),p1)); 
        Vec3b p2Color = old.at<Vec3b>(Point(floor(fi),p2)); 
        Vec3b p3Color = old.at<Vec3b>(Point(floor(fi),p3));
        if(i == 258 && j==59){
        }
        Bp0 = p0Color.val[0]; Bp1 = p1Color.val[0]; Bp2 = p2Color.val[0]; Bp3 = p3Color.val[0]; 
        Gp0 = p0Color.val[1]; Gp1 = p1Color.val[1]; Gp2 = p2Color.val[1]; Gp3 = p3Color.val[1]; 
        Rp0 = p0Color.val[2]; Rp1 = p1Color.val[2]; Rp2 = p2Color.val[2]; Rp3 = p3Color.val[2];

       //Normalize the fj
       fj = (fj - floor(fj));

        B= Bp1 + 0.5 * fj*(Bp2 - Bp0 + fj*(2.0*Bp0 - 5.0*Bp1 + 4.0*Bp2 - Bp3 + fj*(3.0*(Bp1 - Bp2) + Bp3 - Bp0)));
        G= Gp1 + 0.5 * fj*(Gp2 - Gp0 + fj*(2.0*Gp0 - 5.0*Gp1 + 4.0*Gp2 - Gp3 + fj*(3.0*(Gp1 - Gp2) + Gp3 - Gp0)));   
        R= Rp1 + 0.5 * fj*(Rp2 - Rp0 + fj*(2.0*Rp0 - 5.0*Rp1 + 4.0*Rp2 - Rp3 + fj*(3.0*(Rp1 - Rp2) + Rp3 - Rp0)));

        if(B > 255)
            B=255;
        if(G>255)
            G=255;
        if(R>255)
            R=255;
        if(B < 0)
            B=0;
        if(G<0)
            G=0;
        if(R<0)
            R=0;
        Vec3b finalColor(B,G,R);
        res.at<Vec3b>(j,i) = finalColor;
    }   
    }
}
namedWindow( "cubic", CV_WINDOW_AUTOSIZE );
imshow( "cubic", res); 
return res;

}

I forgot to normalize the fj before placing it in the final cubic function

The new results:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.