【opencv入门教程】3. Rect 类用法
文章选自:
一、Background
一个矩形对象包括x,y,width,height,即在x轴起点(一般为0)y轴起点(一般为0)宽度和高度。
二、Code
void Samples::RectFunc()
{
Mat image = imread("printer_chip_01.bmp");
Rect rect1(251, 267, 256, 256);
Rect rect2(281, 408, 256, 256);
//方法
std::cout << "x:" << rect1.x << std::endl;
std::cout << "y:" << rect1.y << std::endl;
std::cout << "width:" << rect1.width << std::endl;
std::cout << "height:" << rect1.height << std::endl;
std::cout << "左上坐标:" << rect1.tl() << std::endl;
std::cout << "右下坐标:" << rect1.br() << std::endl;
std::cout << "rect size:" << rect1.size() << std::endl;
std::cout << "rect area:" << rect1.area() << std::endl;
std::cout << "rect is empty:" << rect1.empty() << std::endl;
std::cout << "检测该矩形是否包含点:" << rect1.contains(Point(251, 267)) << std::endl;
// 将image中rect1的部分复制到roi1中
Mat roi1;
image(rect1).copyTo(roi1);
imshow("roi1", roi1);
waitKey(0);
// 将image中rect2复制到roi2中
Mat roi2;
image(rect2).copyTo(roi2);
imshow("roi2", roi2);
waitKey(0);
// rect1和rect2的交集
cv::Rect rect3 = rect1 & rect2;
Mat roi3;
image(rect3).copyTo(roi3);
imshow("roi3", roi3);
waitKey(0);
// rect1和rect2的并集(两幅图的最小外接矩形)
Rect rect4 = rect1 | rect2;
Mat roi4;
image(rect4).copyTo(roi4);
imshow("roi4", roi4);
waitKey(0);
// 将rect1复制到image的指定区域rect5中
Rect rect5(465, 267, 300, 300);
roi1.copyTo(image(rect5));
imshow("roi5", image);
waitKey(0);
}
三、Result
1.原图
2.截图
3. 输出结果