Java 如何优化图像处理类

Java 如何优化图像处理类,java,android,image-processing,image-optimization,Java,Android,Image Processing,Image Optimization,我有一个处理位图的类,在上面放置鱼眼失真 我通过TraceView运行了我的应用程序,发现几乎所有的处理时间都花在了位图的循环上。 一位开发人员建议不要使用float,因为这会降低图形处理的速度。也不需要使用math.pow()和ceil() 目前,通过循环整个位图来放置效果大约需要42秒,是秒:) 我尝试过用整数替换浮动,这将时间缩短到37秒,但位图上不再显示该效果。 arg k最初是一个浮点值,设置失真级别,例如0.0002F,如果我传递一个int,则效果不起作用 有人能给我指出如何优化这个

我有一个处理位图的类,在上面放置鱼眼失真

我通过TraceView运行了我的应用程序,发现几乎所有的处理时间都花在了位图的循环上。
一位开发人员建议不要使用float,因为这会降低图形处理的速度。也不需要使用math.pow()和ceil() 目前,通过循环整个位图来放置效果大约需要42秒,是秒:)
我尝试过用整数替换浮动,这将时间缩短到37秒,但位图上不再显示该效果。
arg k最初是一个浮点值,设置失真级别,例如0.0002F,如果我传递一个int,则效果不起作用

有人能给我指出如何优化这个过程的正确方向吗?一旦我对它进行了优化,我想研究一下,也许不要在整个位图中循环,也许在效果周围放置一个边界框,或者使用下面的算法来确定像素是否在半径为150的圆内

class Filters{
    float xscale;
    float yscale;
    float xshift;
    float yshift;
    int [] s;
    private String TAG = "Filters";
    long getRadXStart = 0;
    long getRadXEnd = 0;
    long startSample = 0;
    long endSample = 0;
    public Filters(){

        Log.e(TAG, "***********inside filter constructor");
    }

    public Bitmap barrel (Bitmap input, float k){
        //Log.e(TAG, "***********INSIDE BARREL METHOD ");

        float centerX=input.getWidth()/2; //center of distortion
        float centerY=input.getHeight()/2;

        int width = input.getWidth(); //image bounds
        int height = input.getHeight();

        Bitmap dst = Bitmap.createBitmap(width, height,input.getConfig() ); //output pic
       // Log.e(TAG, "***********dst bitmap created ");
          xshift = calc_shift(0,centerX-1,centerX,k);

          float newcenterX = width-centerX;
          float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k);

          yshift = calc_shift(0,centerY-1,centerY,k);

          float newcenterY = height-centerY;
          float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k);

          xscale = (width-xshift-xshift_2)/width;
        //  Log.e(TAG, "***********xscale ="+xscale);
          yscale = (height-yshift-yshift_2)/height;
        //  Log.e(TAG, "***********yscale ="+yscale);
        //  Log.e(TAG, "***********filter.barrel() about to loop through bm");
          /*for(int j=0;j<dst.getHeight();j++){
              for(int i=0;i<dst.getWidth();i++){
                float x = getRadialX((float)i,(float)j,centerX,centerY,k);
                float y = getRadialY((float)i,(float)j,centerX,centerY,k);
                sampleImage(input,x,y);
                int color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
    //            System.out.print(i+" "+j+" \\");

                dst.setPixel(i, j, color);

              }
            }*/

          int origPixel;
          long startLoop = System.currentTimeMillis();
          for(int j=0;j<dst.getHeight();j++){
              for(int i=0;i<dst.getWidth();i++){
                 origPixel= input.getPixel(i,j);
                 getRadXStart = System.currentTimeMillis();
                float x = getRadialX((float)j,(float)i,centerX,centerY,k);
                getRadXEnd= System.currentTimeMillis();

                float y = getRadialY((float)j,(float)i,centerX,centerY,k);

                sampleImage(input,x,y);

                int color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
    //            System.out.print(i+" "+j+" \\");

                if( Math.sqrt( Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) ) ) <= 150 ){
                dst.setPixel(i, j, color);
                }else{
                    dst.setPixel(i,j,origPixel);
                }
              }
            }
          long endLoop = System.currentTimeMillis();
          long loopDuration = endLoop - startLoop;
          long radXDuration = getRadXEnd - getRadXStart;
          long sampleDur = endSample - startSample;

          Log.e(TAG, "sample method took "+sampleDur+"ms");
          Log.e(TAG, "getRadialX took "+radXDuration+"ms");
          Log.e(TAG, "loop took "+loopDuration+"ms");

        //  Log.e(TAG, "***********filter.barrel()  looped through bm about to return dst bm");
        return dst;
    }

    void sampleImage(Bitmap arr, float idx0, float idx1)
    {
         startSample = System.currentTimeMillis();
        s = new int [4];
      if(idx0<0 || idx1<0 || idx0>(arr.getHeight()-1) || idx1>(arr.getWidth()-1)){
        s[0]=0;
        s[1]=0;
        s[2]=0;
        s[3]=0;
        return;
      }

      float idx0_fl=(float) Math.floor(idx0);
      float idx0_cl=(float) Math.ceil(idx0);
      float idx1_fl=(float) Math.floor(idx1);
      float idx1_cl=(float) Math.ceil(idx1);

      int [] s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
      int [] s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
      int [] s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
      int [] s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);

      float x = idx0 - idx0_fl;
      float y = idx1 - idx1_fl;

      s[0]= (int) (s1[0]*(1-x)*(1-y) + s2[0]*(1-x)*y + s3[0]*x*y + s4[0]*x*(1-y));
      s[1]= (int) (s1[1]*(1-x)*(1-y) + s2[1]*(1-x)*y + s3[1]*x*y + s4[1]*x*(1-y));
      s[2]= (int) (s1[2]*(1-x)*(1-y) + s2[2]*(1-x)*y + s3[2]*x*y + s4[2]*x*(1-y));
      s[3]= (int) (s1[3]*(1-x)*(1-y) + s2[3]*(1-x)*y + s3[3]*x*y + s4[3]*x*(1-y));

      endSample = System.currentTimeMillis();
    }

    int [] getARGB(Bitmap buf,int x, int y){

        int rgb = buf.getPixel(y, x); // Returns by default ARGB.
        int [] scalar = new int[4];
        scalar[0] = (rgb >>> 24) & 0xFF;
        scalar[1] = (rgb >>> 16) & 0xFF;
        scalar[2] = (rgb >>> 8) & 0xFF;
        scalar[3] = (rgb >>> 0) & 0xFF;
        return scalar;
    }

    float getRadialX(float x,float y,float cx,float cy,float k){

      x = (x*xscale+xshift);
      y = (y*yscale+yshift);
      float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      return res;
    }

    float getRadialY(float x,float y,float cx,float cy,float k){

      x = (x*xscale+xshift);
      y = (y*yscale+yshift);
      float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      return res;
    }

    float thresh = 1;

    float calc_shift(float x1,float x2,float cx,float k){

      float x3 = (float)(x1+(x2-x1)*0.5);
      float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
      float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));

      if(res1>-thresh && res1 < thresh)
        return x1;
      if(res3<0){
        return calc_shift(x3,x2,cx,k);
      }
      else{
        return calc_shift(x1,x3,cx,k);
      }
    }



}// end of filters class
类过滤器{
浮动xscale;
浮标;
浮动X档;
浮动换档;
int[]s;
私有字符串TAG=“Filters”;
long getRadXStart=0;
长getRadXEnd=0;
长startSample=0;
long-endSample=0;
公共过滤器(){
Log.e(标记“************过滤器构造函数内部”);
}
公共位图桶(位图输入,浮点k){
//Log.e(标记为“桶内法”);
float centerX=input.getWidth()/2;//变形中心
float centerY=input.getHeight()/2;
int width=input.getWidth();//图像边界
int height=input.getHeight();
位图dst=Bitmap.createBitmap(宽度、高度、输入.getConfig());//输出图片
//Log.e(标记,“****************dst位图已创建”);
xshift=计算移位(0,centerX-1,centerX,k);
浮动新中心x=宽度中心x;
float xshift_2=计算移位(0,newcenterX-1,newcenterX,k);
yshift=计算移位(0,centerY-1,centerY,k);
浮动新中心Y=高度中心Y;
float yshift_2=计算移位(0,newcenterY-1,newcenterY,k);
xscale=(宽度-xshift-xshift_2)/width;
//Log.e(标签,“**********xscale=“+xscale”);
yscale=(高度-YSShift-YSShift_2)/高度;
//Log.e(TAG,“*************yscale=“+yscale”);
//Log.e(标记“*********filter.barrel()即将通过bm循环”);
/*对于(int j=0;j-thres&&res1如果(res3您可以尝试的一件事是避免在“sampleImage”和“getARGB”中创建/重新创建int数组,方法是在两个嵌套循环之外实例化它们一次,然后将它们传递到这些方法中。从代码可维护性的角度来看,这不是最佳实践。但是,它可以避免重复创建对象、数组初始化和垃圾收集开销。这些开销往往比代码其余部分中的算术运算要昂贵得多。

据我所见,您的代码正在执行以下操作:

for (every pixel in bitmap){
    getPixel();
    ...do something to pixel...
    setPixel();
}
getPixel()
setPixel()
函数调用相对昂贵。您可以尝试使用将所有像素放入一个数组,然后通过数组访问每个像素,而不是在循环中反复调用它们。请参阅此


如果还不够,请尝试通过C++在.< /p>中编码,首先测量函数的某些部分,看看瓶颈在哪里。不要试图通过猜测来优化。

话虽如此,我现在将尝试上述任务:)

每像素执行
sqrt()
非常昂贵-您要将其与常数进行比较,因此,请将常数平方,并将平方值与之进行比较:

if( ( Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) ) ) <= 150*150 ){

你可以试着在代码复查堆栈上发布这个:@joe好的,谢谢,我已经发布了it@btreat是的,我会试试这个,因为当我运行这个应用程序时,在图像显示之前似乎有很多GCdisplayed@turtleboy-是的,这就是我所建议的。看起来它产生了15-20%的改进,这是一个良好的开端。另一个优化机会是将用于在sampleImage末尾计算s[0-3]的一些值存储在局部变量中。@turtleboy-您也可以尝试更改'if(Math.sqrt(something)@btreat ok谢谢。排除sqrt()后,它又缩短了9秒,因此现在运行@60秒。我不确定您的意思是什么“将用于在sampleImage结尾处计算s[0-3]的一些值存储在局部变量中”。@turtleboy-在sampleImage结尾处有许多中间计算,通过每个s元素计算进行重新计算。(例如:(1-x)*(1-y))。通过将这些中间计算的结果分配给局部变量,然后在s元素的计算中重用变量值,可以获得一些收益。在其他地方也可以应用相同的策略,计算移位(x1-cx)、getRadialX(x-cx)和getRadialY(y-cy)@jim lim嗨,是的,我上周编写了这个应用程序,然后使用“Bitmap.createBitmap(array,x,y,config)”;"方法:从50秒到6秒,谢谢。我想NDK可能是我的方法。我需要MS Visual Studio来把C++和Android集成在一起吗?或者我需要任何C++编译器来创建类文件,然后通过NDK调用它们?谢谢MattNO,你不需要任何微软技术来开发Fox。r Android。下载NDK,并通过其中的一些来了解如何使用它。简言之,您需要编写C/C++代码并使用NDK提供的编译器。我也写过NDK和。尽管如此,请注意NDK优化。请阅读。
if( ( Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) ) ) <= 150*150 ){
if(((i-centerX)*(i-centerX) + (j-centerY)*(j-centerY)) <= 150){