具有指定角度的OpenCV RotatedRect

Nam*_*912 6 opencv angle

我的情况是我有一个小的二进制图像,有一个形状,我想要找到最合适的旋转矩形(不是边界矩形).我知道有cv :: minAreaRect()你应用于cv :: findContours()找到的结果,但这在我的情况下导致了不好的结果,因为数据很吵(来自MS Kinect,见示例图片噪音敏感度由于输入数据(轮廓)略有不同而导致旋转发生变化的情况.我所做的是在我的二进制图像上使用PCA计算主轴(对噪声不太敏感),产生角度"a",现在我想创建一个RotatedRect围绕我的形状,给定主轴的角度,a).

我有一个插图,用我精湛的Paint技能制作! 插图

那么我的问题是:你们有代码片段或具体的建议来解决这个问题吗?我担心我必须做很多Bresenham迭代,希望有一个聪明的方法.

顺便说一句,对于那些不太熟悉openCV的RotatedRect数据结构的人:它由高度,宽度,角度和中心点定义,假设中心点实际上是在矩形的中心.

干杯!

Nam*_*912 6

好的,我的解决方案:方法:

  1. PCA,给出rotationRect中心的角度和第一个近似值
  2. 获取二进制形状的轮廓,将其旋转到直立位置,获得X和Y坐标的最小值/最大值,以获得边界矩形的宽度和高度
  3. 从最大X(Y)减去宽度(高度)的一半以获得"直立空间"中的中心点
  4. 通过反向旋转矩阵向后旋转此中心点

    cv::RotatedRect Utilities::getBoundingRectPCA( cv::Mat& binaryImg ) {
    cv::RotatedRect result;
    
    //1. convert to matrix that contains point coordinates as column vectors
    int count = cv::countNonZero(binaryImg);
    if (count == 0) {
        std::cout << "Utilities::getBoundingRectPCA() encountered 0 pixels in binary image!" << std::endl;
        return cv::RotatedRect();
    }
    
    cv::Mat data(2, count, CV_32FC1);
    int dataColumnIndex = 0;
    for (int row = 0; row < binaryImg.rows; row++) {
        for (int col = 0; col < binaryImg.cols; col++) {
            if (binaryImg.at<unsigned char>(row, col) != 0) {
                data.at<float>(0, dataColumnIndex) = (float) col; //x coordinate
                data.at<float>(1, dataColumnIndex) = (float) (binaryImg.rows - row); //y coordinate, such that y axis goes up
                ++dataColumnIndex;
            }
        }
    }
    
    //2. perform PCA
    const int maxComponents = 1;
    cv::PCA pca(data, cv::Mat() /*mean*/, CV_PCA_DATA_AS_COL, maxComponents);
    //result is contained in pca.eigenvectors (as row vectors)
    //std::cout << pca.eigenvectors << std::endl;
    
    //3. get angle of principal axis
    float dx = pca.eigenvectors.at<float>(0, 0);
    float dy = pca.eigenvectors.at<float>(0, 1);
    float angle = atan2f(dy, dx)  / (float)CV_PI*180.0f;
    
    //find the bounding rectangle with the given angle, by rotating the contour around the mean so that it is up-right
    //easily finding the bounding box then
    cv::Point2f center(pca.mean.at<float>(0,0), binaryImg.rows - pca.mean.at<float>(1,0));
    cv::Mat rotationMatrix = cv::getRotationMatrix2D(center, -angle, 1);
    cv::Mat rotationMatrixInverse = cv::getRotationMatrix2D(center, angle, 1);
    
    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(binaryImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    if (contours.size() != 1) {
        std::cout << "Warning: found " << contours.size() << " contours in binaryImg (expected one)" << std::endl;
        return result;
    }
    
    //turn vector of points into matrix (with points as column vectors, with a 3rd row full of 1's, i.e. points are converted to extended coords)
    cv::Mat contourMat(3, contours[0].size(), CV_64FC1);
    double* row0 = contourMat.ptr<double>(0);
    double* row1 = contourMat.ptr<double>(1);
    double* row2 = contourMat.ptr<double>(2);
    for (int i = 0; i < (int) contours[0].size(); i++) {
        row0[i] = (double) (contours[0])[i].x;
        row1[i] = (double) (contours[0])[i].y;
        row2[i] = 1;
    }
    
    cv::Mat uprightContour = rotationMatrix*contourMat;
    
    //get min/max in order to determine width and height
    double minX, minY, maxX, maxY;
    cv::minMaxLoc(cv::Mat(uprightContour, cv::Rect(0, 0, contours[0].size(), 1)), &minX, &maxX); //get minimum/maximum of first row
    cv::minMaxLoc(cv::Mat(uprightContour, cv::Rect(0, 1, contours[0].size(), 1)), &minY, &maxY); //get minimum/maximum of second row
    
    int minXi = cvFloor(minX);
    int minYi = cvFloor(minY);
    int maxXi = cvCeil(maxX);
    int maxYi = cvCeil(maxY);
    
    //fill result
    result.angle = angle;
    result.size.width = (float) (maxXi - minXi);
    result.size.height = (float) (maxYi - minYi);
    
    //Find the correct center:
    cv::Mat correctCenterUpright(3, 1, CV_64FC1);
    correctCenterUpright.at<double>(0, 0) = maxX - result.size.width/2;
    correctCenterUpright.at<double>(1,0) = maxY - result.size.height/2;
    correctCenterUpright.at<double>(2,0) = 1;
    cv::Mat correctCenterMat = rotationMatrixInverse*correctCenterUpright;
    cv::Point correctCenter = cv::Point(cvRound(correctCenterMat.at<double>(0,0)), cvRound(correctCenterMat.at<double>(1,0)));
    
    result.center = correctCenter;
    
    return result;
    
    Run Code Online (Sandbox Code Playgroud)

    }


fir*_*ant 2

如果正确理解问题,那么您所说的使用方法findContoursminAreaRect因输入数据的噪声而遭受抖动/摆动。PCA 对于这种噪音并没有更强的鲁棒性,所以我不明白为什么你认为以这种方式找到模式的方向不会像你当前的代码那么糟糕。

如果您需要时间平滑度,常用且简单的解决方案是使用过滤器,即使是像alpha-beta 过滤器这样非常简单的过滤器也可能为您提供所需的平滑度。假设在帧中n您估计旋转矩形的参数A,并且在帧中n+1您有带有估计参数的矩形B。您无需使用 绘制矩形,而是B找到和C之间的位置,然后使用in frame绘制矩形。ABCn+1