OpenCV自带函数实现灰度图像平移和旋转算法(平面内)

float shift_and_rot_test_opencv(cv::Mat des, Vector2f shift, int rot)
{
  float center_x=(DIM_SAMPLE_POINTS_X-1)/2.0;
  float center_y=(DIM_SAMPLE_POINTS_Y-1)/2.0;
  cv::Point center = cv::Point(center_x, center_y);
  double scale = 1.0;
  //img_shift_rot_test = cv::getRotationMatrix2D(center, (double)rot, 1.0);
  cv::Mat trans_mat = cv::getRotationMatrix2D(center, (double)rot, 1.0);
  trans_mat.at<float>(0,2) += shift(0)/SENSOR_RANGE/2.0*DIM_SAMPLE_POINTS_X;
  trans_mat.at<float>(1,2) += -shift(1)/SENSOR_RANGE/2.0*DIM_SAMPLE_POINTS_Y;
  cv::warpAffine(img4, img_shift_rot_test, trans_mat, img4.size());
  cv::absdiff(img_shift_rot_test, des, img_absdiff);
  //img_absdiff *= 0.5;
  float err = 0.0;
  int rows = img_absdiff.rows;
  int cols = img_absdiff.cols;
  for(int row = 0; row < rows; row++)
    for(int col = 0; col < cols; col++)
    {
      err += img_absdiff.at<float>(row, col);
    }
  cv::imshow("error abs", img_absdiff);
  cv::waitKey(10);
  return err/2.0/current_total;
}

getRotationMatrix2D函数用于取得用于旋转图像的2D矩阵(2*3):https://docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#gafbbc470ce83812914a70abfb604f4326

然后通过 warpAffine()函数进行变换,shift用于表示偏移量,坐标系y轴与图像y轴相反;

返回值为误差比例。

TIPS:为加快运算速度,cv::Mat类型中间数据需要进行初始化。

猜你喜欢

转载自blog.csdn.net/li4692625/article/details/109410866