立方和cat色样条对图像的影响 [英] Impact of cubic and catmull splines on image

查看:171
本文介绍了立方和cat色样条对图像的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现下面的一些功能





为此,我试图使用立方插值 Catmull插值(分别检查两个比较最好的结果),我不明白是什么影响这些插值显示在图像上,我们如何可以获得这些点值,我们点击设置该曲线?



我从这些资源获得帮助



源1



来源2



大致相同的焦点



编辑

  int main(int argc,const char ** argv)
{
Mat input = imread(E:\\img2。 jpg);
for(int i = 0; i< input.rows; i ++)
{
for(int p = 0; p< input.cols; p ++)
{
// for(int t = 0; t // {

input.at< cv :: Vec3b>(i,p) [0] = 255 * correction(input.at (i,p)[0] /255.0,ctrl,N); // B
input.at< cv :: Vec3b>(i,p)[1] = 255 * correction(input.at (i,p)[1] /255.0,ctrl ,N)。 // G
input.at< cv :: Vec3b>(i,p)[2] = 255 * correction(input.at (i,p)[2] /255.0,ctrl ,N)。 // R
//}
}
}

imshow(image,input);
waitKey();
}


解决方案

总是在相同的x坐标

,并且沿整个范围线性分布,那么你可以这样做:

  // ------------------------------------------- ---------------------------- 
const int N = 5; //控制点数(必须> = 4)
float ctrl [N] = //控制点使用线性函数启动的y值y = x
{// x值是索引* 1.0 /(N-1)
0.00,
0.25,
0.50,
0.75,
1.00,
}
// -------------------------------------------- -------------------------------
浮点修正(float col,float * ctrl,int n)
{
float di = 1.0 / float(n-1);
int i0,i1,i2,i3;
float t,tt,ttt;
float a0,a1,a2,a3,d1,d2;
//找到开始控制点
col * = float(n-1);
i1 = col col- = i1;
i0 = i1-1; if(i0 <0)i0 = 0;
i2 = i1 + 1; if(i2> = n)i2 = n-1;
i3 = i1 + 2; if(i3> = n)i3 = n-1;
//计算插值系数
d1 = 0.5 *(ctrl [i2] -ctrl [i0]);
d2 = 0.5 *(ctrl [i3] -ctrl [i1]);
a0 = ctrl [i1];
a1 = d1;
a2 =(3.0 *(ctrl [i2] -ctrl [i1])) - (2.0 * d1)-d2;
a3 = d1 + d2 +(2.0 *( - ctrl [i2] + ctrl [i1]));
//现在插入新的colro强度
t = col; tt = t * t; ttt = tt * t;
t = a0 +(a1 * t)+(a2 * tt)+(a3 * ttt)
return t;
}
// --------------------------------------- ------------------------------------




  • 它使用4点1D插值立方(从上面的注释中的链接)

  • 要获得新颜色,只需执行以下操作:

      new_col = correction(old_col,ctrl,N) 


  • 这是它的外观:







  • 绿色箭头显示导出错误(始终只在整曲线的开始和结束点)

  • 还有另外两个控制点



[注意事项] ]




  • 颜色范围为< 0.0,1.0>



$ b b

[edit1]开始/结束派生固定一点

  col,float * ctrl,int n)
{
float di = 1.0 / float(n-1);
int i0,i1,i2,i3;
float t,tt,ttt;
float a0,a1,a2,a3,d1,d2;
//找到开始控制点
col * = float(n-1);
i1 = col col- = i1;
i0 = i1-1;
i2 = i1 + 1; if(i2> = n)i2 = n-1;
i3 = i1 + 2;
//计算插值系数
if(i0> = 0)d1 = 0.5 *(ctrl [i2] -ctrl [i0]); else d1 = ctrl [i2] -ctrl [i1];
if(i3< n)d2 = 0.5 *(ctrl [i3] -ctrl [i1]); else d2 = ctrl [i2] -ctrl [i1];
a0 = ctrl [i1];
a1 = d1;
a2 =(3.0 *(ctrl [i2] -ctrl [i1])) - (2.0 * d1)-d2;
a3 = d1 + d2 +(2.0 *( - ctrl [i2] + ctrl [i1]));
//现在插入新的colro强度
t = col; tt = t * t; ttt = tt * t;
t = a0 +(a1 * t)+(a2 * tt)+(a3 * ttt)
return t;
}

[edit2]




  • 均来自此条件:

      y(t)= a0 + a1 * t + a2 * t * t + a3 * t * t * t //直接价值
    y' * t + 3 * a3 * t * t //第一个派生


  • y1,y2,y3


  • 所以我选择了y(0)= y1和y(1)

  • 现在我需要c1连续性,所以i添加y'(0)必须与y'(1)相同。

  • 对于y'(1)我选择avg方向在点y0,y1,y2之间
  • 在点y1,y2,y3之间的平均方向
  • 这些对于下一个/上一个片段是相同的,因此足够

  •   y(0)= y0 = a0 + a1 * 0 + a2 * 0 * 0 + a3 * 0 * 0 * 0 
    y(1)= y1 = a0 + a1 * 1 + a2 * 1 * 1 + a3 * 1 * 1 * 1
    y'(0)= 0.5 *(y2-y0)= a1 + 2 * a2 * 0 + 3 * a3 * 0 * 0
    y'(1)= 0.5 *(y3-y1)= a1 + 2 * a2 * 1 + 3 * a3 * 1 * 1


  • 现在解决这个等式的系统(a0,a1,a2,a3 =?)


  • 如果您需要不同的曲线属性,则只需创建不同的方程即可。

  • li>


[edit3]用法

  pic1 = pic0; //复制源图像到目的地pic是我的图像类... 
for(y = 0; y for(x = 0; x< ; pic1.xs; x ++)
{
float i;
//读,转换,写像素
i = pic1.p [y] [x] .db [0]; i = 255.0 *校正(i / 255.0,红色控制点,5); pic1.p [y] [x] .db [0] = i;
i = pic1.p [y] [x] .db [1]; i = 255.0 *校正(i / 255.0,绿色控制点,5); pic1.p [y] [x] .db [1] = i;
i = pic1.p [y] [x] .db [2]; i = 255.0 *校正(i / 255.0,蓝色控制点,5); pic1.p [y] [x] .db [2] = i;
}




  • up是每个R,G,B的控制点

  • 左下角是原始图片

  • 右下角是正确的图片


I am trying to implement some function like below

For this I am trying to use Cubic interpolation and Catmull interpolation ( check both separately to compare the best result) , what i am not understanding is what impact these interpolation show on image and how we can get these points values where we clicked to set that curve ? and do we need to define the function these black points on the image separately ?

I am getting help from these resources

Source 1

Source 2

Approx the same focus

Edit

int main (int argc, const char** argv)
{
Mat input = imread ("E:\\img2.jpg");
for(int i=0 ; i<input.rows ; i++)
{
    for (int p=0;p<input.cols;p++)
    {
        //for(int t=0; t<input.channels(); t++)
    //{

        input.at<cv::Vec3b>(i,p)[0] = 255*correction(input.at<cv::Vec3b>(i,p)[0]/255.0,ctrl,N);  //B
        input.at<cv::Vec3b>(i,p)[1] = 255*correction(input.at<cv::Vec3b>(i,p)[1]/255.0,ctrl,N);  //G
        input.at<cv::Vec3b>(i,p)[2] = 255*correction(input.at<cv::Vec3b>(i,p)[2]/255.0,ctrl,N);  //R
    //}
    }
}

imshow("image" , input);
waitKey();
}

解决方案

So if your control points are always on the same x coordinate
and linearly dispersed along whole range then you can do it like this:

//---------------------------------------------------------------------------
const int N=5;      // number of control points (must be >= 4)
float ctrl[N]=      // control points y values initiated with linear function y=x
    {           // x value is index*1.0/(N-1)
    0.00,
    0.25,
    0.50,
    0.75,
    1.00,
    };
//---------------------------------------------------------------------------
float correction(float col,float *ctrl,int n)
    {
    float di=1.0/float(n-1);
    int i0,i1,i2,i3;
    float t,tt,ttt;
    float a0,a1,a2,a3,d1,d2;
    // find start control point
    col*=float(n-1);
    i1=col; col-=i1;
    i0=i1-1; if (i0< 0) i0=0;
    i2=i1+1; if (i2>=n) i2=n-1;
    i3=i1+2; if (i3>=n) i3=n-1;
    // compute interpolation coefficients
    d1=0.5*(ctrl[i2]-ctrl[i0]);
    d2=0.5*(ctrl[i3]-ctrl[i1]);
    a0=ctrl[i1];
    a1=d1;
    a2=(3.0*(ctrl[i2]-ctrl[i1]))-(2.0*d1)-d2;
    a3=d1+d2+(2.0*(-ctrl[i2]+ctrl[i1]));
    // now interpolate new colro intensity
    t=col; tt=t*t; ttt=tt*t;
    t=a0+(a1*t)+(a2*tt)+(a3*ttt);
    return t;
    }
//---------------------------------------------------------------------------

  • it uses 4-point 1D interpolation cubic (from that link in my comment above)
  • to get new color just do this:

    new_col = correction(old_col,ctrl,N);
    

  • this is how it looks:

  • the green arrows shows derivation error (always only on start and end point of whole curve)
  • it can be corrected by adding 2 more control points
  • one before and one after all others ...

[Notes]

  • color range is < 0.0 , 1.0 >
  • if you need other then just multiply the result and divide the input ...

[edit1] the start/end derivations fixed a little

float correction(float col,float *ctrl,int n)
    {
    float di=1.0/float(n-1);
    int i0,i1,i2,i3;
    float t,tt,ttt;
    float a0,a1,a2,a3,d1,d2;
    // find start control point
    col*=float(n-1);
    i1=col; col-=i1;
    i0=i1-1;
    i2=i1+1; if (i2>=n) i2=n-1;
    i3=i1+2;
    // compute interpolation coefficients
    if (i0>=0) d1=0.5*(ctrl[i2]-ctrl[i0]); else d1=ctrl[i2]-ctrl[i1];
    if (i3< n) d2=0.5*(ctrl[i3]-ctrl[i1]); else d2=ctrl[i2]-ctrl[i1];
    a0=ctrl[i1];
    a1=d1;
    a2=(3.0*(ctrl[i2]-ctrl[i1]))-(2.0*d1)-d2;
    a3=d1+d2+(2.0*(-ctrl[i2]+ctrl[i1]));
    // now interpolate new colro intensity
    t=col; tt=t*t; ttt=tt*t;
    t=a0+(a1*t)+(a2*tt)+(a3*ttt);
    return t;
    }

[edit2] just some clarification on the coefficients

  • the are all derived from this conditions:

    y(t) = a0 + a1*t + a2*t*t + a3*t*t*t // direct value
    y'(t) = a1 + 2*a2*t + 3*a3*t*t        // first derivation
    

  • now you have points y0,y1,y2,y3

  • so I chose that y(0)=y1 and y(1)=y2
  • which gives c0 continuity (value is the same in the joint points between curves)
  • now I need c1 continuity so i add y'(0) must be the same as y'(1) from previous curve
  • for y'(0) I choose avg direction between points y0,y1,y2
  • for y'(1) I choose avg direction between points y1,y2,y3
  • these are the same for the next/previous segments so it is enough
  • now put it together

    y(0)  = y0           = a0 + a1*0 + a2*0*0 + a3*0*0*0
    y(1)  = y1           = a0 + a1*1 + a2*1*1 + a3*1*1*1
    y'(0) = 0.5*(y2-y0) = a1 + 2*a2*0 + 3*a3*0*0
    y'(1) = 0.5*(y3-y1) = a1 + 2*a2*1 + 3*a3*1*1
    

  • now solve this system of equtions (a0,a1,a2,a3 = ?)

  • and will get what I have in source code above
  • if you need different properties of the curve then just make different equations ...

[edit3] usage

pic1=pic0; // copy source image to destination pic is mine image class ...
for (y=0;y<pic1.ys;y++) // go through all pixels
 for (x=0;x<pic1.xs;x++)
    {
    float i;
     //  read, convert, write pixel 
    i=pic1.p[y][x].db[0]; i=255.0*correction(i/255.0,red control points,5); pic1.p[y][x].db[0]=i;
    i=pic1.p[y][x].db[1]; i=255.0*correction(i/255.0,green control points,5); pic1.p[y][x].db[1]=i;
    i=pic1.p[y][x].db[2]; i=255.0*correction(i/255.0,blue control points,5); pic1.p[y][x].db[2]=i;
    }

  • up are control points per R,G,B
  • down left is original image
  • down right is corrected image

这篇关于立方和cat色样条对图像的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆