1

我试图用插值旋转图像,但对于大图像实时太慢。如何加快图像的双线性插值?

的代码是这样的:(?)

for(int y=0;y<dst_h;++y) 
{ 
    for(int x=0;x<dst_w;++x) 
    { 
     //do inverse transform 
     fPoint pt(Transform(Point(x, y))); 

     //in coor of src 
     int x1= (int)floor(pt.x); 
     int y1= (int)floor(pt.y); 
     int x2= x1+1; 
     int y2= y1+1; 


     if((x1>=0&&x1<src_w&&y1>=0&&y1<src_h)&&(x2>=0&&x2<src_w&&y2>=0&&y2<src_h)) 
     { 
       Mask[y][x]= 1; //show pixel 

       float dx1= pt.x-x1; 
       float dx2= 1-dx1; 
       float dy1= pt.y-y1; 
       float dy2= 1-dy1; 

       //bilinear 
       pd[x].blue= (dy2*(ps[y1*src_w+x1].blue*dx2+ps[y1*src_w+x2].blue*dx1)+ 
         dy1*(ps[y2*src_w+x1].blue*dx2+ps[y2*src_w+x2].blue*dx1)); 
       pd[x].green= (dy2*(ps[y1*src_w+x1].green*dx2+ps[y1*src_w+x2].green*dx1)+ 
         dy1*(ps[y2*src_w+x1].green*dx2+ps[y2*src_w+x2].green*dx1)); 
       pd[x].red= (dy2*(ps[y1*src_w+x1].red*dx2+ps[y1*src_w+x2].red*dx1)+ 
         dy1*(ps[y2*src_w+x1].red*dx2+ps[y2*src_w+x2].red*dx1)); 

       //nearest neighbour 
       //pd[x]= ps[((int)pt.y)*src_w+(int)pt.x]; 
     } 
     else 
       Mask[y][x]= 0; //transparent pixel 
    } 
    pd+= dst_w; 
} 

我如何可以加快这个代码,我尝试并行的代码,但它似乎没有速度了,因为内存的访问模式。

+0

似乎更适合codereview.se – PlasmaHH

+0

您还可以使用具有双线性插值优化的函数英特尔IPP库。对于某些情况,此库优化了SIMD指令(更快的单线程性能)和多核使用。唯一的缺点是,这个SDK是付费软件。如果这不适合您的需求,那么您应该尝试OpenMP。 –

回答

1

哇,你在里面做了很多最内环像

1.float为int的转换

  • 可以做所有的彩车...
  • 这些天来他们相当快速
  • 转换是什么在杀你
  • 你也混合浮法和ints在一起(如果我看到它的权利),这是相同的...

2.transform(X,Y)

  • 任何不必要的调用使堆捣毁和慢下来
  • ,而不是添加2个变量XX,YY和insde插值他们的for循环

3.如果....

  • 为什么赫克你如果加入?
  • 限制for循环之前的范围,而不是内...
  • 背景可以填充其他维权之前或之后
5

关键是要完成大部分的计算为整数。唯一需要做的就是加权。请参阅here获取良好的资源。

从同样的资源:

int px = (int)x; // floor of x 
int py = (int)y; // floor of y 
const int stride = img->width; 
const Pixel* p0 = img->data + px + py * stride; // pointer to first pixel 

// load the four neighboring pixels 
const Pixel& p1 = p0[0 + 0 * stride]; 
const Pixel& p2 = p0[1 + 0 * stride]; 
const Pixel& p3 = p0[0 + 1 * stride]; 
const Pixel& p4 = p0[1 + 1 * stride]; 

// Calculate the weights for each pixel 
float fx = x - px; 
float fy = y - py; 
float fx1 = 1.0f - fx; 
float fy1 = 1.0f - fy; 

int w1 = fx1 * fy1 * 256.0f; 
int w2 = fx * fy1 * 256.0f; 
int w3 = fx1 * fy * 256.0f; 
int w4 = fx * fy * 256.0f; 

// Calculate the weighted sum of pixels (for each color channel) 
int outr = p1.r * w1 + p2.r * w2 + p3.r * w3 + p4.r * w4; 
int outg = p1.g * w1 + p2.g * w2 + p3.g * w3 + p4.g * w4; 
int outb = p1.b * w1 + p2.b * w2 + p3.b * w3 + p4.b * w4; 
int outa = p1.a * w1 + p2.a * w2 + p3.a * w3 + p4.a * w4;