【问题标题】:calculate disparity in processing计算处理中的差异
【发布时间】:2020-04-13 17:56:19
【问题描述】:

我正在使用处理,我正在尝试计算 2 个校正图像的差异,为此我需要从左图像中获取每个像素并从右图像中搜索一行并找到相似的像素,即尺寸图片是 640x480 。

我需要在函数draw()中运行disparityImage(PImage imgL,PImage imgR)并且太慢,该函数在1-2秒内执行,但是如果我注释这些代码行“minD = d;rightX = xr;”从 if 块 该函数在 3-5 毫秒内执行。我不明白我的代码出了什么问题,我尝试了太多小时才找出来,但还是找不到。

void depthImage(PImage imgL, PImage imgR) { 
    for (int x=0; x<imgL.width; x=x+1) {
      for (int y=0; y<imgL.height; y=y+1) {    
        color imgleft=imgL.get(x,y);
        float r1=red(imgleft);
        float g1=green(imgleft);
        float b1=blue(imgleft);

        float minD=Integer.MAX_VALUE;
        int rightX=0;

        for (int xr=0; xr<imgR.width; xr++) {    
          color imgright=imgR.get(x,y);
          float r2=red(imgright);
          float g2=green(imgright);
          float b2=blue(imgright);

          float d=dist(r1, g1, b1, r2, g2, b2);

          if (d<minD) {
            minD=d;
            rightX=xr;
          }
        }
      }
    }
  }

【问题讨论】:

    标签: java performance processing disparity-mapping


    【解决方案1】:

    dist() 计算 2 个点之间的 Euclidean distance。对于计算,sqrt() 函数是必需的。 sqrt() 是一个非常耗时的操作。

    dist(x1, y1, z1, x2, y2, z2)
    

    可以表示为

    sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1))
    

    我建议改为计算和比较 Euclidean distance 的平方。这避免了昂贵的sqrt 操作。例如:

    void depthImage(PImage imgL, PImage imgR) {
        for (int x=0; x<imgL.width; x=x+1) {
            for (int y=0; y<imgL.height; y=y+1) {
    
                color imgleft = imgL.get(x,y);
                float r1=red(imgleft);
                float g1=green(imgleft);
                float b1=blue(imgleft);
    
                float minD_square = Integer.MAX_VALUE;
                int rightX=0;
    
                for (int xr=0; xr<imgR.width; xr++) {
    
                    color imgright=imgR.get(x,y);
                    float dr = red(imgright)   - r1;
                    float dg = green(imgright) - g1;
                    float db = blue(imgright)  - b1;
    
                    float d_square = dr*dr + dg*dg + db*db;
    
                    if (d_square < minD_square) {
                        minD_square = d_square;
                        rightX = xr;
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 2016-06-14
      相关资源
      最近更新 更多