Vio*_*ion 3 c++ opencv rotation
我有以下问题。我正在使用 HaarClassifiers 在图像中搜索眼睛。由于头部的旋转,我试图在不同的角度找到眼睛。为此,我将图像旋转不同角度。为了旋转框架,我使用代码(用 C++ 编写):
Point2i rotCenter;
rotCenter.x = scaledFrame.cols / 2;
rotCenter.y = scaledFrame.rows / 2;
Mat rotationMatrix = getRotationMatrix2D(rotCenter, angle, 1);
warpAffine(scaledFrame, scaledFrame, rotationMatrix, Size(scaledFrame.cols, scaledFrame.rows));
Run Code Online (Sandbox Code Playgroud)
这工作正常,我能够为眼睛提取两个 ROI 矩形。所以,我有每个 ROI 的上/左坐标以及它们的宽度和高度。然而,这些坐标是旋转图像中的坐标。我不知道如何将这个矩形反投影到原始框架上。
假设我获得了未缩放帧(full_image)的眼睛对 rois,但仍然旋转。
eye0_roi and eye1_roi
Run Code Online (Sandbox Code Playgroud)
我怎样才能将它们旋转回来,以便它们映射到正确的位置?
最好的问候,安德烈
您可以使用invertAffineTransform来获取逆矩阵并使用该矩阵将点旋转回来:
Mat RotateImg(const Mat& img, double angle, Mat& invertMat)
{
Point center = Point( img.cols/2, img.rows/2);
double scale = 1;
Mat warpMat = getRotationMatrix2D( center, angle, scale );
Mat dst = Mat(img.size(), CV_8U, Scalar(128));
warpAffine( img, dst, warpMat, img.size(), 1, 0, Scalar(255, 255, 255));
invertAffineTransform(warpMat, invertMat);
return dst;
}
Point RotateBackPoint(const Point& dstPoint, const Mat& invertMat)
{
cv::Point orgPoint;
orgPoint.x = invertMat.at<double>(0,0)*dstPoint.x + invertMat.at<double>(0,1)*dstPoint.y + invertMat.at<double>(0,2);
orgPoint.y = invertMat.at<double>(1,0)*dstPoint.x + invertMat.at<double>(1,1)*dstPoint.y + invertMat.at<double>(1,2);
return orgPoint;
}
Run Code Online (Sandbox Code Playgroud)