使用 OpenCV 将帧转换为就像从上方获取的一样
Transform a frame to be as if it was taken from above using OpenCV
我正在从事一个使用光流技术估计无人机(四轴飞行器)位置的项目。我目前有一个使用 OpenCV 中的 farneback 算法的代码。当相机始终指向地面时,当前代码工作正常。
现在,我想添加对相机未垂直指向的情况的支持 - 这意味着四轴飞行器现在具有俯仰/滚动/偏航(欧拉角)。四轴飞行器的欧拉角是已知的,我正在寻找一种方法来根据已知的当前欧拉角计算和应用所需的变换。这样结果图像就好像是从上面拍摄的一样(见下图)。
我找到了通过来自 OpenCV 的 findHomography
或 getPerspectiveTransform
函数在具有 4 个角的 2 组(源和目标)时计算转换的方法。但是我找不到任何只知道欧拉角的方法(因为我不知道目标图像核心)。
所以我的问题是我可以使用什么方法以及如何将帧转换为好像是从上方拍摄的,必要时仅使用欧拉角和距地面的相机高度?
为了演示我需要的东西:
我当前代码的相关部分如下:
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);
}
更新:
成功添加旋转后,我仍然需要将图像居中(并且不要超出框架 window,如下所示)。我想我需要某种翻译。 我希望源图像的中心位于目标图像的中心。我怎样才能添加这个?
有效的旋转函数:
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());
}
当我 运行 它与 rotateFrame(origFrame, processedFrame, cameraMatrix, 0, 0, 0);
时,我得到了预期的图像:
但是当我 运行 它以 10 度的角度滚动时 rotateFrame(origFrame, processedFrame, cameraMatrix, 20*(M_PI/180), 0, 0);
。图像超出框架 window:
这是我在 Eigen 中使用 4 个角的方式:
// Desired four corners
std::vector<Eigen::Vector2f> Normalized_Reference_Pattern = { Eigen::Vector2f(0, 0), Eigen::Vector2f(0, 2), Eigen::Vector2f(2, 0), Eigen::Vector2f(2, 2) };
// Current four points
std::vector<Eigen::Vector2f> CurrentCentroids = { /* Whatever four corners you want, but relative sueqnece to above */ };
// Transform for current to desired
auto Master_Transform = get_perspective_transform(CurrentCentroids, Normalized_Reference_Pattern);
// abilitu to use the same tranformation for other points (other than the corners) in the image
Eigen::Vector2f Master_Transform_Centroid = Master_Transform * Eigen::Vector2f(currentX, currentY);
这是我的黑匣子:
Eigen::Matrix3f get_perspective_transform(const std::vector<Eigen::Vector2f>& points_from,const std::vector<Eigen::Vector2f>& points_to)
{
cv::Mat transform_cv = cv::getPerspectiveTransform(
convert::to_cv(points_from),
convert::to_cv(points_to));
Eigen::Matrix3f transform_eigen;
cv::cv2eigen(transform_cv, transform_eigen);
return transform_eigen;
}
如果您有 calibration intrinsics matrix A (3x3), and there is no translation between camara poses, all you need to find homography H (3x3) is to construct rotation matrix R (3x3) from euler angles 并应用以下公式:
H = A * R * A.inv()
其中.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 |
W 是你的最终变形。
我得出一个结论,我必须使用 4x4 单应矩阵才能获得我想要的结果。为了找到正确的单应矩阵,我们需要:
- 3D旋转矩阵
R
.
- 相机标定本征矩阵
A1
及其逆矩阵A2
。
- 平移矩阵
T
.
我们可以通过将围绕 X、Y、Z 轴的旋转矩阵相乘来组成 3D 旋转矩阵 R
:
Mat R = RZ * RY * RX
为了在图像上应用变换并保持其居中,我们需要添加由 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);
给定所有这些矩阵,我们可以组成我们的单应矩阵 H
:
Mat H = A2 * (T * (R * A1))
有了这个单应矩阵,我们就可以使用 OpenCV 中的 warpPerspective
函数来应用变换。
warpPerspective(input, output, H, input.size(), INTER_LANCZOS4);
为了解决这个解决方案的结论和完整性,这里是完整的代码:
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);
}
结果:
我正在从事一个使用光流技术估计无人机(四轴飞行器)位置的项目。我目前有一个使用 OpenCV 中的 farneback 算法的代码。当相机始终指向地面时,当前代码工作正常。
现在,我想添加对相机未垂直指向的情况的支持 - 这意味着四轴飞行器现在具有俯仰/滚动/偏航(欧拉角)。四轴飞行器的欧拉角是已知的,我正在寻找一种方法来根据已知的当前欧拉角计算和应用所需的变换。这样结果图像就好像是从上面拍摄的一样(见下图)。
我找到了通过来自 OpenCV 的 findHomography
或 getPerspectiveTransform
函数在具有 4 个角的 2 组(源和目标)时计算转换的方法。但是我找不到任何只知道欧拉角的方法(因为我不知道目标图像核心)。
所以我的问题是我可以使用什么方法以及如何将帧转换为好像是从上方拍摄的,必要时仅使用欧拉角和距地面的相机高度?
为了演示我需要的东西:
我当前代码的相关部分如下:
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);
}
更新:
成功添加旋转后,我仍然需要将图像居中(并且不要超出框架 window,如下所示)。我想我需要某种翻译。 我希望源图像的中心位于目标图像的中心。我怎样才能添加这个?
有效的旋转函数:
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());
}
当我 运行 它与 rotateFrame(origFrame, processedFrame, cameraMatrix, 0, 0, 0);
时,我得到了预期的图像:
但是当我 运行 它以 10 度的角度滚动时 rotateFrame(origFrame, processedFrame, cameraMatrix, 20*(M_PI/180), 0, 0);
。图像超出框架 window:
这是我在 Eigen 中使用 4 个角的方式:
// Desired four corners
std::vector<Eigen::Vector2f> Normalized_Reference_Pattern = { Eigen::Vector2f(0, 0), Eigen::Vector2f(0, 2), Eigen::Vector2f(2, 0), Eigen::Vector2f(2, 2) };
// Current four points
std::vector<Eigen::Vector2f> CurrentCentroids = { /* Whatever four corners you want, but relative sueqnece to above */ };
// Transform for current to desired
auto Master_Transform = get_perspective_transform(CurrentCentroids, Normalized_Reference_Pattern);
// abilitu to use the same tranformation for other points (other than the corners) in the image
Eigen::Vector2f Master_Transform_Centroid = Master_Transform * Eigen::Vector2f(currentX, currentY);
这是我的黑匣子:
Eigen::Matrix3f get_perspective_transform(const std::vector<Eigen::Vector2f>& points_from,const std::vector<Eigen::Vector2f>& points_to)
{
cv::Mat transform_cv = cv::getPerspectiveTransform(
convert::to_cv(points_from),
convert::to_cv(points_to));
Eigen::Matrix3f transform_eigen;
cv::cv2eigen(transform_cv, transform_eigen);
return transform_eigen;
}
如果您有 calibration intrinsics matrix A (3x3), and there is no translation between camara poses, all you need to find homography H (3x3) is to construct rotation matrix R (3x3) from euler angles 并应用以下公式:
H = A * R * A.inv()
其中.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 |
W 是你的最终变形。
我得出一个结论,我必须使用 4x4 单应矩阵才能获得我想要的结果。为了找到正确的单应矩阵,我们需要:
- 3D旋转矩阵
R
. - 相机标定本征矩阵
A1
及其逆矩阵A2
。 - 平移矩阵
T
.
我们可以通过将围绕 X、Y、Z 轴的旋转矩阵相乘来组成 3D 旋转矩阵 R
:
Mat R = RZ * RY * RX
为了在图像上应用变换并保持其居中,我们需要添加由 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);
给定所有这些矩阵,我们可以组成我们的单应矩阵 H
:
Mat H = A2 * (T * (R * A1))
有了这个单应矩阵,我们就可以使用 OpenCV 中的 warpPerspective
函数来应用变换。
warpPerspective(input, output, H, input.size(), INTER_LANCZOS4);
为了解决这个解决方案的结论和完整性,这里是完整的代码:
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);
}
结果: