Qt6与OpenCV4.7教程2---cv::Rect类的重载

1、建立控制台程序:

 2、在.pro文件中添加如下代码:

win32
{
    INCLUDEPATH += F:\OpenCV4.7.0\OpenCV-Build\install\include
    LIBS += F:\OpenCV4.7.0\OpenCV-Build\install\x64\mingw\bin\libopencv_*.dll
}

3、在main.cpp中进行如下修改和添加代码:

#include <iostream>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

 main函数内的内容如下:

int main( )
{
    cv::Mat I,B,temp;
    I = cv::Mat::zeros(300, 400, CV_8UC3); //创建一个黑色的画布
    B = I.clone(); //背景
    cv::Rect r1 = cv::Rect(60,60,200,100);
    cv::Rect r2 = cv::Rect(cv::Point(140, 120), cv::Size(200, 100));
    cv::rectangle(I ,r1, cv::Scalar(0, 255, 0), 2);
    cv::rectangle(I, r2, cv::Scalar(0, 0, 255), 2);
    cv::imshow("rectangle", I);
    cv::waitKey();
    //交集
    temp = I.clone();
    cv::Rect r3 = r1 & r2;
    cv::rectangle(temp, r3, cv::Scalar(255, 0, 0), 2);
    cv::imshow("Intersection", temp);
    cv::waitKey();
    //包含矩形r1和r2的最小矩形
    temp = I.clone();
    cv::Rect r4 = r1 | r2;
    cv::rectangle(temp, r4, cv::Scalar(255, 0, 0), 2);
    cv::imshow("Minimum area rectangle contain r1, r2", temp);
    cv::waitKey();
    //改变矩形大小
    temp = B.clone();
    cv::Rect rs(r1);
    rs += Size(-30, 60);
    cv::rectangle(temp, r1, cv::Scalar(0, 255, 0), 2);
    cv::rectangle(temp, rs, cv::Scalar(255, 0, 0), 2);
    cv::imshow("Enlarge", temp);
    //平移矩形
    temp = B.clone();
    cv::Rect rt(r1);
    rt += Point(30, 60); 
    cv::rectangle(temp, r1, cv::Scalar(0, 255, 0), 2);    
    cv::rectangle(temp, rt, cv::Scalar(255, 0, 0), 2);
    cv::imshow("Translate", temp);
    cv::waitKey();

    cv::Point p(80, 80);
    cv::Point q(20, 20);
    cout << "rectangle r1: " << r1 << endl;
    cout << "rectangle r2: " << r2 << endl;
    cout << "Is r1 and r2 the same rectangle: " << (r1 == r2) << endl;
    cout << "Is r1 and r2 not the same rectangle: " << (r1 != r2) << endl;
    cout << "Is r1 contains point p(80, 80): " << r1.contains(p) << endl;
    cout << "Is r1 contains point q(20, 20): " << r1.contains(q) << endl;
    system("PAUSE");
    return 0;
}

4、运行结果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值