【问题标题】:How to correct a RotatedRect skew with OpenCV?如何使用 OpenCV 纠正 RotatedRect 倾斜?
【发布时间】:2019-08-24 18:56:56
【问题描述】:

我使用 Opencv 编写了一个 Android 应用程序,我的图像处理算法需要对检测到的矩形进行正确的旋转,所以作为该过程的开始,我

  1. 检测最大的矩形为RotatedRect。
  2. 获取矩形的旋转角度和中心。
  3. 用getRotationMatrix2D创建一个旋转矩阵
  4. 使用warpAffine执行仿射变换
  5. 使用getRectSubPix提取检测到的矩形

孔处理工作正常,但对象倾斜有问题。

当对象逆时针旋转时,歪斜校正效果很好,但是如果它顺时针旋转,矩形会旋转 90 度。

我需要提一下,因为Opencv相机的方向错误,我先在矩形旋转角度加上-90。

这是我的代码

         /* *******************************************************************************************
         * get angle and size from the rotated rect
         *******************************************************************************************/
        double rect_angle = rbox.angle - 90.0;
        Size rect_size = rbox.size;

        /* *******************************************************************************************
         * correct the orientation 
         *******************************************************************************************/
        double d = rect_size.width;
        rect_size.width = rect_size.height;
        rect_size.height = d;

        M = Imgproc.getRotationMatrix2D(rbox.center, rect_angle, 1.0);
        Imgproc.warpAffine(origMat, rotated, M, origMat.size());

        /* *******************************************************************************************
         * crop the resulting image
         *******************************************************************************************/
        if (rect_size.width > 75 && rect_size.height > 75)
            Imgproc.getRectSubPix(rotated, new Size(rect_size.width - 75, rect_size.height - 75), rbox.center, rotated);

        /* *******************************************************************************************
         * resize the result image because rotated has the size of the rect not the original image
         * which cause the preview camera to be black because of wrong dimensions
         *******************************************************************************************/
        Imgproc.resize(rotated, rotated, origMat.size());

这是对象顺时针旋转时的结果图像

这是对象逆时针旋转的结果图像

这是原图

我应该提一下,当对象不旋转时,角度为 -90 如果它顺时针旋转角度趋于-179,如果它逆时针旋转角度变为-179并且趋向于-90。

我尝试设置这样的条件

if (rbox.angle < -90.0) {
  rect_angle -= 90.0;
}

但没有任何效果。

我知道这是一个数学问题,但我找不到实现它的方法,希望你们能帮我解决这个问题。

【问题讨论】:

    标签: android opencv image-processing computer-vision opencv4android


    【解决方案1】:

    为大家关心的答案,我找到了解决办法。

    我处理的是检测到的矩形的宽度和高度,而不是角度。

    所以在创建旋转矩阵之前我添加了这个测试代码

    if (rbox.size.width < rbox.size.height) {
             rect_angle += 90.0;
             double d1 = rect_size.height;
             rect_size.height = rect_size.width;
             rect_size.width = d1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多