【图像处理】透视变换 Perspective Transformation(2)

PerspectiveTransform PerspectiveTransform::quadrilateralToSquare(float x0, float y0, float x1, float y1, float x2,
    float y2, float x3, float y3) {
  // Here, the adjoint serves as the inverse:
  return squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint();
}

PerspectiveTransform PerspectiveTransform::buildAdjoint() {
  // Adjoint is the transpose of the cofactor matrix:
  PerspectiveTransform result(PerspectiveTransform(a22 * a33 - a23 * a32, a23 * a31 - a21 * a33, a21 * a32
                                  - a22 * a31, a13 * a32 - a12 * a33, a11 * a33 - a13 * a31, a12 * a31 - a11 * a32, a12 * a23 - a13 * a22,
                                  a13 * a21 - a11 * a23, a11 * a22 - a12 * a21));
  return result;
}

PerspectiveTransform PerspectiveTransform::times(PerspectiveTransform other) {
  PerspectiveTransform result(PerspectiveTransform(a11 * other.a11 + a21 * other.a12 + a31 * other.a13,
                                  a11 * other.a21 + a21 * other.a22 + a31 * other.a23, a11 * other.a31 + a21 * other.a32 + a31
                                  * other.a33, a12 * other.a11 + a22 * other.a12 + a32 * other.a13, a12 * other.a21 + a22
                                  * other.a22 + a32 * other.a23, a12 * other.a31 + a22 * other.a32 + a32 * other.a33, a13
                                  * other.a11 + a23 * other.a12 + a33 * other.a13, a13 * other.a21 + a23 * other.a22 + a33
                                  * other.a23, a13 * other.a31 + a23 * other.a32 + a33 * other.a33));
  return result;
}

void PerspectiveTransform::transformPoints(vector<float> &points) {
  int max = points.size();
  for (int i = 0; i < max; i += 2) {
    float x = points[i];
    float y = points[i + 1];
    float denominator = a13 * x + a23 * y + a33;
    points[i] = (a11 * x + a21 * y + a31) / denominator;
    points[i + 1] = (a12 * x + a22 * y + a32) / denominator;
  }
}

对一张透视图片变换回正面图的效果:

int main(){
 Mat img=imread("boy.png");
 int img_height = img.rows;
 int img_width = img.cols;
 Mat img_trans = Mat::zeros(img_height,img_width,CV_8UC3);
 PerspectiveTransform tansform = PerspectiveTransform::quadrilateralToQuadrilateral(
  0,0,
  img_width-1,0,
  0,img_height-1,
  img_width-1,img_height-1,
  150,250, // top left
  771,0, // top right
  0,1023,// bottom left
  650,1023
  );
 vector<float> ponits;
 for(int i=0;i<img_height;i++){
  for(int j=0;j<img_width;j++){
   ponits.push_back(j);
   ponits.push_back(i);
  }
 }
 tansform.transformPoints(ponits);
 for(int i=0;i<img_height;i++){
  uchar*  t= img_trans.ptr<uchar>(i);
  for (int j=0;j<img_width;j++){
   int tmp = i*img_width+j;
   int x = ponits[tmp*2];
   int y = ponits[tmp*2+1];
   if(x<0||x>(img_width-1)||y<0||y>(img_height-1))
    continue;
   uchar* p = img.ptr<uchar>(y);
   t[j*3] = p[x*3];
   t[j*3+1] = p[x*3+1];
   t[j*3+2] = p[x*3+2];
  }
 }
 imwrite("trans.png",img_trans);
 return 0;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d693409c75e402a10a095cf08fa14914.html