qq_44251971 2023-03-01 21:42 采纳率: 25%
浏览 33
已结题

opencv中boxPoints的返回值问题

opencv中boxPoints的返回值问题

boxPoints在c++环境下调用时,返回的是一个mat对象,与python的返回值不同


void minRcet() {
    Mat image,result;
    Mat contrast(720,640,CV_8UC4,Scalar(0,0,0));
    image = imread(rectHello,0);
    //showImage(image);
    threshold(image, result, 5, 255,THRESH_BINARY);
    //contrastImage(image, result);
    vector<vector<Point>> contours;
    vector<Vec4i> hierachy;
    Mat position;
    findContours(result, contours, hierachy, RETR_TREE,CHAIN_APPROX_SIMPLE);
    drawContours(contrast, contours, -1, Scalar(0, 0, 255), 2);
    //contrastImage(result,contrast);
    RotatedRect rectPoint = minAreaRect(contours[1]);
    Mat boxPts;
    boxPoints(rectPoint,boxPts);
    cout << endl << boxPts << endl;
    //drawContours(contrast, boxPts, -1, Scalar(0, 255, 0));
//    showImage(contrast);
}

输出的mat值为

img

又因drawContours绘制需要使用point类型的坐标才能绘制,但是boxPoints函数返回的mat对象信息如上图所示,不能把一个个点分开来作为point2f的坐标格式,就是想知道如何将 minAreaRect的图形用C++语言绘制出来

初学opencv不太懂,希望得到有经验的伙伴帮助

  • 写回答

3条回答 默认 最新

  • 社区专家-Monster-XH 2023-03-01 21:52
    关注

    基于Monster 组和GPT的调写:
    在C++中,boxPoints函数的第二个参数是一个Mat类型的输出参数,它会被用来存储旋转矩形的四个顶点坐标。因此,你需要先创建一个大小为(4, 2)的Mat对象,并将其作为boxPoints函数的第二个参数传入。最后,你可以将输出的Mat对象转换为一个vector类型,以便在drawContours函数中使用。

    下面是修改后的代码示例:

    void minRcet() {
        Mat image, result;
        Mat contrast(720, 640, CV_8UC4, Scalar(0, 0, 0));
        image = imread(rectHello, 0);
        threshold(image, result, 5, 255, THRESH_BINARY);
        vector<vector<Point>> contours;
        vector<Vec4i> hierarchy;
        findContours(result, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
        drawContours(contrast, contours, -1, Scalar(0, 0, 255), 2);
        RotatedRect rectPoint = minAreaRect(contours[1]);
        Mat boxPts;
        boxPoints(rectPoint, boxPts);
    
        // 将Mat对象转换为vector<Point2f>类型
        vector<Point2f> boxPtsVec;
        boxPtsVec.reserve(4);
        for (int i = 0; i < 4; ++i) {
            boxPtsVec.emplace_back(boxPts.at<float>(i, 0), boxPts.at<float>(i, 1));
        }
    
        // 绘制旋转矩形
        vector<vector<Point>> boxContours{boxPtsVec};
        drawContours(contrast, boxContours, 0, Scalar(0, 255, 0), 2);
    
        // showImage(contrast);
    }
    
    
    

    创建了一个名为boxPtsVec的vector类型的对象,并用boxPts的数据填充了它。然后,创建了一个vector<vector>类型的对象boxContours,其中包含了boxPtsVec。最后,我们将boxContours传递给drawContours函数来绘制旋转矩形。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 修改了问题 3月1日
  • 创建了问题 3月1日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan