本文介绍了使用python在OpenCV中进行透视校正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对在所有4个方向上都倾斜的倾斜矩形(信用卡)进行透视校正.我可以找到它的四个角以及倾斜的相应角度,但是我找不到必须将其投影到的坐标的确切位置.我正在使用cv2.getPerspectiveTransform进行转换.

I am trying to do a perspective correction of a tilted rectangle ( a credit card), which is tilted in all the 4 directions. I could find its four corners and the respective angles of its tilt but I cannot find the exact location of the coordinates, where it has to be projected. I am using cv2.getPerspectiveTransform to do the transformation.

我具有实际卡片(非倾斜卡片)的长宽比,我想要这样的坐标以保持原始长宽比.我尝试使用边界矩形,但这会增加卡的大小.

I have the aspect ratio of the actual card (the non tilted one), I want such coordinates such that the original aspect ratio is preserved. I have tried using a bounding rectangle but this increases the size of the card.

任何帮助将不胜感激.

Any help would be appreciated.

推荐答案

这是您需要遵循的方式...

Here is the way you need to follow...

为简便起见,我将您的图片调整为较小的尺寸,

For easiness I have resized your image to smaller size,

  • 计算源图像的四边形顶点,在这里我手动进行查找,可以选择边缘检测,轮廓线等.
  Q1=manual calculation;
  Q2=manual calculation;
  Q3=manual calculation;
  Q4=manual calculation;

  • 通过保持宽高比来计算目标图像中的四边形顶点,在这里您可以从源的四边形顶点上方获取卡的宽度,并乘以宽高比来计算高度.
  •    // compute the size of the card by keeping aspect ratio.
        double ratio=1.6;
        double cardH=sqrt((Q3.x-Q2.x)*(Q3.x-Q2.x)+(Q3.y-Q2.y)*(Q3.y-Q2.y)); //Or you can give your own height
        double cardW=ratio*cardH;
        Rect R(Q1.x,Q1.y,cardW,cardH);
    

    • 现在您已经获得了用于源和目标的四边形顶点,然后应用warpPerspective.
    • 您可以参考下面的C ++代码,

      You can refer below C++ code,

         //Compute quad point for edge
          Point Q1=Point2f(90,11);
          Point Q2=Point2f(596,135);
          Point Q3=Point2f(632,452);
          Point Q4=Point2f(90,513);
      
          // compute the size of the card by keeping aspect ratio.
          double ratio=1.6;
          double cardH=sqrt((Q3.x-Q2.x)*(Q3.x-Q2.x)+(Q3.y-Q2.y)*(Q3.y-Q2.y));//Or you can give your own height
          double cardW=ratio*cardH;
          Rect R(Q1.x,Q1.y,cardW,cardH);
      
          Point R1=Point2f(R.x,R.y);
          Point R2=Point2f(R.x+R.width,R.y);
          Point R3=Point2f(Point2f(R.x+R.width,R.y+R.height));
          Point R4=Point2f(Point2f(R.x,R.y+R.height));
      
          std::vector<Point2f> quad_pts;
          std::vector<Point2f> squre_pts;
      
          quad_pts.push_back(Q1);
          quad_pts.push_back(Q2);
          quad_pts.push_back(Q3);
          quad_pts.push_back(Q4);
      
          squre_pts.push_back(R1);
          squre_pts.push_back(R2);
          squre_pts.push_back(R3);
          squre_pts.push_back(R4);
      
      
          Mat transmtx = getPerspectiveTransform(quad_pts,squre_pts);
          int offsetSize=150;
          Mat transformed = Mat::zeros(R.height+offsetSize, R.width+offsetSize, CV_8UC3);
          warpPerspective(src, transformed, transmtx, transformed.size());
      
          //rectangle(src, R, Scalar(0,255,0),1,8,0);
      
          line(src,Q1,Q2, Scalar(0,0,255),1,CV_AA,0);
          line(src,Q2,Q3, Scalar(0,0,255),1,CV_AA,0);
          line(src,Q3,Q4, Scalar(0,0,255),1,CV_AA,0);
          line(src,Q4,Q1, Scalar(0,0,255),1,CV_AA,0);
      
          imshow("quadrilateral", transformed);
          imshow("src",src);
          waitKey();
      

      这篇关于使用python在OpenCV中进行透视校正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 00:24