C++ opencv计算两个cv::Rect的重复面积

C++  opencv计算两个cv::Rect的重复面积

 

 

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

int main() {
    // 创建两个矩形
    cv::Rect rect1(10, 10, 50, 50);  // (x, y, width, height)
    cv::Rect rect2(30, 30, 50, 50);
    // 检查两个矩形是否重叠
    if (rect1.x <= rect2.x + rect2.width && rect1.x + rect1.width >= rect2.x &&
        rect1.y <= rect2.y + rect2.height && rect1.y + rect1.height >= rect2.y) {
        // 如果重叠,计算重叠区域的面积
        cv::Rect overlap = rect1 & rect2;  // 创建重叠区域矩形
        double overlapArea = overlap.area();  // 计算重叠区域面积
        std::cout << "Overlap area: " << overlapArea << std::endl;
    } else {
        std::cout << "No overlap" << std::endl;
    }
    return 0;
}

 

 

 

#########################

posted @ 2024-01-19 23:19  西北逍遥  阅读(201)  评论(0编辑  收藏  举报