这是opencv RotatedRect的错误? [英] Is it a bug of opencv RotatedRect?

查看:742
本文介绍了这是opencv RotatedRect的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码:

#include<iostream>
#include<opencv2/core/core.hpp>
using namespace std;
using namespace cv;

int main()
{
    Point2f p[4];
    RotatedRect rr(Point2f(),Size2f(1,2),0);
    rr.points(p);
    for(int i = 0;i < 4;i++) cout<<p[i]<<endl;
    Rect r = rr.boundingRect();
    cout<< r.x << " " << r.y << " " << r.br().x << " " << r.br().y <<endl;
    return 0;
}

RotatedRect rr的左上角是(-0.5,-1) ,而RotatedRect rr的右下角是(0.5,1)。因此,包含旋转矩形的最小右上矩形是[(-1,-1),(1,1)],( - 1, 1)是左上角,(1,1)是右下角。但结果rect的右下角是(2,2)。

The top-left corner of RotatedRect rr is (-0.5,-1),and the bottom-right corner of RotatedRect rr is (0.5,1).So the minminimal up-right rectangle containing the rotated rectangle is [(-1,-1),(1,1)],(-1,-1) is the top-left corner,(1,1) is the bottom-right corner.But the result rect's bottom-right corner is (2,2).

我读了源代码:

Rect RotatedRect::boundingRect() const
{
    Point2f pt[4];
    points(pt);
    Rect r(cvFloor(std::min(std::min(std::min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
           cvFloor(std::min(std::min(std::min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
           cvCeil(std::max(std::max(std::max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
           cvCeil(std::max(std::max(std::max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
    r.width -= r.x - 1;
    r.height -= r.y - 1;
    return r;
}

我不知道代码 r.width - = rx - 1; r.height - = ry - 1;
我认为应该是 r .width - = rx; r.height - = ry;
谁能告诉我是源错误? p>

I wonder the code r.width -= r.x - 1; and r.height -= r.y - 1; And I think it should be r.width -= r.x; and r.height -= r.y; Who can tell me is it the source wrong?

推荐答案

看起来像 cv :: boundingRect RotatedRect :: boundingRect 不一致。我使用这个小测试应用程序

Looks like cv::boundingRect and RotatedRect::boundingRect are not consistent. I used this little test application

int main(int argc, char* argv[])
{
    cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png");

    //cv::RotatedRect rr = cv::RotatedRect(cv::Point2f(), cv::Size2f(1, 2), 0);

    cv::RotatedRect rr = cv::RotatedRect(cv::Point2f(256,256), cv::Size2f(150, 100), 45);

    cv::Rect rect1 = rr.boundingRect();

    cv::Point2f pt[4];
    rr.points(pt);

    std::vector<cv::Point> pts;
    pts.push_back(pt[0]);
    pts.push_back(pt[1]);
    pts.push_back(pt[2]);
    pts.push_back(pt[3]);

    cv::Rect rect2 = cv::boundingRect(pts);

    std::cout << "RotatedRect::boundingRect: " << rect1 << std::endl;
    std::cout << "cv::boundingRect: " << rect2 << std::endl;

    cv::line(input, pt[0], pt[1], cv::Scalar(0, 0, 255));
    cv::line(input, pt[1], pt[2], cv::Scalar(0, 0, 255));
    cv::line(input, pt[2], pt[3], cv::Scalar(0, 0, 255));
    cv::line(input, pt[3], pt[0], cv::Scalar(0, 0, 255));

    cv::rectangle(input, rect1, cv::Scalar(255, 0, 0));
    cv::rectangle(input, rect2, cv::Scalar(0, 255, 0));

    cv::imshow("input", input);
    cv::waitKey(0);
    return 0;
}

结果给出了稍微不同的边界矩形(RotatedRect :: boundingRect cv :: boundingRect置于ON点)。这可能是一个bug,也许最初cv :: RotatedRect :: boundingRect是cv :: boundingRect的优化副本,并且cv :: boundingRect中的错误修正没有更新成员函数。不确定...我会建议一直使用通用cv :: boundingRect(虽然没有测量速度)。

The result gives slightly different bounding rects (RotatedRect::boundingRect surrounds the points while cv::boundingRect is placed ON the points). This might really be a bug, maybe initially cv::RotatedRect::boundingRect was an optimized copy of cv::boundingRect, and a bugfix in cv::boundingRect didn't update the member function. Not sure... I would recommend to use the general purpose cv::boundingRect all the time (didn't measure speed though).

这篇关于这是opencv RotatedRect的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆