简体   繁体   English

OpenCV:查找旋转点的原始坐标

[英]OpenCV: Find original coordinates of a rotated point

I have the following problem. 我有以下问题。 I'm searching for eyes within an image using HaarClassifiers. 我正在使用HaarClassifiers在图像中搜索眼睛。 Due to the rotation of the head I'm trying to find eyes within different angles. 由于头部旋转,我试图寻找不同角度的眼睛。 For that, I rotate the image by different angles. 为此,我将图像旋转了不同的角度。 For rotating the frame, I use the code (written in C++): 为了旋转框架,我使用代码(用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));

This works fine and I am able to extract two ROI Rectangles for the eyes. 这可以正常工作,并且我能够为眼睛提取两个ROI矩形。 So, I have the top/left coordinates of each ROI as well as their width and height. 因此,我具有每个ROI的顶部/左侧坐标以及它们的宽度和高度。 However, these coordinates are the coordinates in the rotated image. 但是,这些坐标是旋转图像中的坐标。 I don't know how I can backproject this rectangle onto the original frame. 我不知道如何将这个矩形反向投影到原始框架上。

Assuming I have the obtaind eye pair rois for the unscaled frame (full_image), but still roated. 假设我获得了未缩放帧(full_image)的眼睛对rois,但仍然旋转。

eye0_roi and eye1_roi

How can I rotate them back, such that they map their correct position? 我如何旋转它们,以便它们绘制正确的位置?

Best regards, Andre 最好的问候,安德烈

You can use the invertAffineTransform to get the inverse matrix and use this matrix to rotate point back: 您可以使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM