使用均方误差代替SAD进行图像补偿

时间:2018-10-21 04:17:09

标签: c++ image image-processing mean

我有一个任务,其中使用SAD进行图像合成。另一个任务是在代码中使用MSE代替SAD。我正在为此苦苦挣扎,所以有人可以帮助我吗?这是SAD的代码。

     find_motion(my_image_comp *ref, my_image_comp *tgt,
              int start_row, int start_col, int block_width, int block_height)
  /* This function finds the motion vector which best describes the motion
     between the `ref' and `tgt' frames, over a specified block in the
     `tgt' frame.  Specifically, the block in the `tgt' frame commences
     at the coordinates given by `start_row' and `start_col' and extends
     over `block_width' columns and `block_height' rows.  The function finds
     the translational offset (the returned vector) which describes the
     best matching block of the same size in the `ref' frame, where
     the "best match" is interpreted as the one which minimizes the sum of
     absolute differences (SAD) metric. */
{
  mvector vec, best_vec;
  int sad, best_sad=256*block_width*block_height;
  for (vec.y=-8; vec.y <= 8; vec.y++)
    for (vec.x=-8; vec.x <= 8; vec.x++)
      {
        int ref_row = start_row-vec.y;
        int ref_col = start_col-vec.x;
        if ((ref_row < 0) || (ref_col < 0) ||
            ((ref_row+block_height) > ref->height) ||
            ((ref_col+block_width) > ref->width))
          continue; // Translated block not containe within reference frame
        int r, c;
        int *rp = ref->buf + ref_row*ref->stride + ref_col;
        int *tp = tgt->buf + start_row*tgt->stride + start_col;
        for (sad=0, r=block_height; r > 0; r--,
             rp+=ref->stride, tp+=tgt->stride)
          for (c=0; c < block_width; c++)
            {
              int diff = tp[c] - rp[c];
              sad += (diff < 0)?(-diff):diff;
            }
        if (sad < best_sad)
          {
            best_sad = sad;
            best_vec = vec;
          }
      }

  return best_vec;
}

1 个答案:

答案 0 :(得分:0)

我想我自己得到了答案。 其

for (mse = 0, r = block_height; r > 0; r--,
              rp+=ref->stride, tp+=tgt->stride)
          for (c=0; c < block_width; c++)
            {
              int diff = tp[c] - rp[c];
              temp = (diff*diff) / (block_height*block_width);
              mse += temp;
              temp = 0;
            }

        if (mse < best_mse)
        {
            best_mse = mse;
            best_vec = vec;
        }
      }

  return best_vec;
}