【发布时间】:2019-08-24 18:56:56
【问题描述】:
我使用 Opencv 编写了一个 Android 应用程序,我的图像处理算法需要对检测到的矩形进行正确的旋转,所以作为该过程的开始,我
- 检测最大的矩形为
RotatedRect。 - 获取矩形的旋转角度和中心。
- 用
getRotationMatrix2D创建一个旋转矩阵 - 使用
warpAffine执行仿射变换 - 使用
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