A. *_*rid 4 c++ opencv image-processing computer-vision image-rotation
我正在研究一个使用光流技术估计 UAV(四轴飞行器)位置的项目。我目前有一个使用OpenCV 的farneback算法的代码。当相机始终指向地面时,当前代码工作正常。
现在,我想为相机没有直接指向下方的情况添加支持 - 这意味着四轴飞行器现在具有俯仰/滚转/偏航(欧拉角)。四轴飞行器的欧拉角是已知的,我正在寻找一种方法来计算和应用基于已知当前欧拉角所需的变换。这样结果图像就好像是从上面拍摄的一样(见下图)。
我找到了在通过OpenCVfindHomography或getPerspectiveTransformOpenCV 函数获得 2 组(源和目标)4 个角时计算转换的方法。但是我找不到任何可以只知道欧拉角的方法(因为我不知道目标图像核心)。
所以我的问题是我可以使用什么方法,以及如何在必要时仅使用欧拉角和相机距地面的高度将帧转换为好像是从上面拍摄的一样?
为了证明我需要什么:
我当前代码的相关部分如下:
for(;;)
{
Mat m, disp, warp;
vector<Point2f> corners;
// take out frame- still distorted
cap >> origFrame;
// undistort the frame using the calibration parameters
cv::undistort(origFrame, undistortFrame, cameraMatrix, distCoeffs, noArray());
// lower the process effort by transforming the picture to gray
cvtColor(undistortFrame, gray, COLOR_BGR2GRAY);
if( !prevgray.empty() )
{
// calculate flow
calcOpticalFlowFarneback(prevgray, gray, uflow, 0.5, 3/*def 3 */, 10/* def 15*/, 3, 3, 1.2 /* def 1.2*/, 0);
uflow.copyTo(flow);
// get average
calcAvgOpticalFlow(flow, 16, corners);
// calculate range of view - 2*tan(fov/2)*distance
rovX = 2*0.44523*distanceSonar*100; // 2 * tan(48/2) * dist(cm)
rovY = 2*0.32492*distanceSonar*100; // 2 * tan(36/2) * dist(cm)
// calculate final x, y location
location[0] += (currLocation.x/WIDTH_RES)*rovX;
location[1] += (currLocation.y/HEIGHT_RES)*rovY;
}
//break conditions
if(waitKey(1)>=0)
break;
if(end_run)
break;
std::swap(prevgray, gray);
}
Run Code Online (Sandbox Code Playgroud)
成功添加旋转后,我仍然需要将我的图像居中(而不是超出框架窗口,如下所示)。我想我需要某种翻译。我希望源图像的中心位于目标图像的中心。我怎样才能添加这个?
有效的旋转功能:
void rotateFrame(const Mat &input, Mat &output, Mat &A , double roll, double pitch, double yaw){
Mat Rx = (Mat_<double>(3, 3) <<
1, 0, 0,
0, cos(roll), -sin(roll),
0, sin(roll), cos(roll));
Mat Ry = (Mat_<double>(3, 3) <<
cos(pitch), 0, sin(pitch),
0, 1, 0,
-sin(pitch), 0, cos(pitch));
Mat Rz = (Mat_<double>(3, 3) <<
cos(yaw), -sin(yaw), 0,
sin(yaw), cos(yaw), 0,
0, 0, 1);
Mat R = Rx*Ry*Rz;
Mat trans = A*R*A.inv();
warpPerspective(input, output, trans, input.size());
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,rotateFrame(origFrame, processedFrame, cameraMatrix, 0, 0, 0);我得到了预期的图像:
但是当我以 10 度滚动时运行它rotateFrame(origFrame, processedFrame, cameraMatrix, 20*(M_PI/180), 0, 0);。图像正在离开框架窗口:
如果您有一个校准内在矩阵 A (3x3),并且相机姿势之间没有平移,那么您需要找到单应性 H (3x3) 就是从欧拉角构造旋转矩阵 R (3x3)并应用以下公式:
H = A * R * A.inv()
Run Code Online (Sandbox Code Playgroud)
其中 .inv() 是矩阵求逆。
更新:
如果要使图像居中,则应以这种方式添加平移:(这是找到中心的扭曲位置并将该点平移回中心)
|dx| | 320 / 2 |
|dy| = H * | 240 / 2 |
|1 | | 1 |
| 1 0 (320/2-dx) |
W = | 0 1 (240/2-dy) | * H
| 0 0 1 |
Run Code Online (Sandbox Code Playgroud)
W 是您的最终变换。
我得出的结论是,我必须使用 4x4 单应性矩阵才能得到我想要的东西。为了找到正确的单应矩阵,我们需要:
R。A1及其逆矩阵A2。T。我们可以通过将绕轴 X、Y、Z 的旋转矩阵相乘来组成 3D 旋转矩阵R:
Mat R = RZ * RY * RX
Run Code Online (Sandbox Code Playgroud)
为了对图像应用变换并使其保持居中,我们需要添加由 4x4 矩阵给出的平移,其中dx=0; dy=0; dz=1:
Mat T = (Mat_<double>(4, 4) <<
1, 0, 0, dx,
0, 1, 0, dy,
0, 0, 1, dz,
0, 0, 0, 1);
Run Code Online (Sandbox Code Playgroud)
给定所有这些矩阵,我们可以组成我们的单应性矩阵H:
Mat H = A2 * (T * (R * A1))
Run Code Online (Sandbox Code Playgroud)
有了这个单应性矩阵,我们就可以使用warpPerspectiveOpenCV 中的函数来应用变换。
warpPerspective(input, output, H, input.size(), INTER_LANCZOS4);
Run Code Online (Sandbox Code Playgroud)
为了该解决方案的结论和完整性,这里是完整的代码:
void rotateImage(const Mat &input, UMat &output, double roll, double pitch, double yaw,
double dx, double dy, double dz, double f, double cx, double cy)
{
// Camera Calibration Intrinsics Matrix
Mat A2 = (Mat_<double>(3,4) <<
f, 0, cx, 0,
0, f, cy, 0,
0, 0, 1, 0);
// Inverted Camera Calibration Intrinsics Matrix
Mat A1 = (Mat_<double>(4,3) <<
1/f, 0, -cx/f,
0, 1/f, -cy/f,
0, 0, 0,
0, 0, 1);
// Rotation matrices around the X, Y, and Z axis
Mat RX = (Mat_<double>(4, 4) <<
1, 0, 0, 0,
0, cos(roll), -sin(roll), 0,
0, sin(roll), cos(roll), 0,
0, 0, 0, 1);
Mat RY = (Mat_<double>(4, 4) <<
cos(pitch), 0, sin(pitch), 0,
0, 1, 0, 0,
-sin(pitch), 0, cos(pitch), 0,
0, 0, 0, 1);
Mat RZ = (Mat_<double>(4, 4) <<
cos(yaw), -sin(yaw), 0, 0,
sin(yaw), cos(yaw), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Translation matrix
Mat T = (Mat_<double>(4, 4) <<
1, 0, 0, dx,
0, 1, 0, dy,
0, 0, 1, dz,
0, 0, 0, 1);
// Compose rotation matrix with (RX, RY, RZ)
Mat R = RZ * RY * RX;
// Final transformation matrix
Mat H = A2 * (T * (R * A1));
// Apply matrix transformation
warpPerspective(input, output, H, input.size(), INTER_LANCZOS4);
}
Run Code Online (Sandbox Code Playgroud)