
Rect(x,y,width,height)
x:左上角的列坐标。
y:左上角的行坐标。
width:裁剪几列。
height:裁剪几行。
#include <iostream>
#include <filesystem>
#include <string>
#include <windows.h>
#include <opencv2/opencv.hpp>
namespace fs = std::filesystem;
using namespace cv;
using namespace std;
// Mat Rect提取ROI区域 / 裁剪Mat数据
// ROI 在图像处理过程中,我们可能会对图像的某一个特定区域感兴趣,该区域被称为感兴趣区域(Region of Interest, ROI)。
void roi()
{
// test
float b[5][3] = { {1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15} };
Mat a(5, 3, CV_32FC1, b);
//float temp;
//temp = a.at<float>(0, 1);
//cout << temp << endl;
cout << a << endl;
/*
Rect(x, y, width, height)
x:左上角的列坐标
y:左上角的行坐标
width:裁剪几列
height:裁剪几行
*/
Mat c = a(Rect(0, 1, 2, 3));
cout << c << endl;
}
int main()
{
// 提取roi区域
roi();
waitKey(0);
destroyAllWindows();
return 0;
}

#include <iostream>
#include <filesystem>
#include <string>
#include <windows.h>
#include <opencv2/opencv.hpp>
namespace fs = std::filesystem;
using namespace cv;
using namespace std;
// Mat Rect提取ROI区域 / 裁剪Mat数据
// ROI 在图像处理过程中,我们可能会对图像的某一个特定区域感兴趣,该区域被称为感兴趣区域(Region of Interest, ROI)。
void roi()
{
// test
float b[5][3] = { {1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15} };
Mat a(5, 3, CV_32FC1, b);
//float temp;
//temp = a.at<float>(0, 1);
//cout << temp << endl;
cout << a << endl;
/*
Rect(x, y, width, height)
x:左上角的列坐标
y:左上角的行坐标
width:裁剪几列
height:裁剪几行
*/
Mat c = a(Rect(0, 1, 2, 3));
cout << c << endl;
}
// Mat Rect提取ROI区域 / 裁剪Mat数据
// ROI 在图像处理过程中,我们可能会对图像的某一个特定区域感兴趣,该区域被称为感兴趣区域(Region of Interest, ROI)。
void roipic()
{
cv::Mat src = cv::imread("F:/opencv/console/x64/Debug/jzh.png");
printf("row: %d, col: %d\n", src.rows, src.cols);
Rect rect(50, 50, 250, 250); // 划定区域
Mat roi = src(rect);
cout << "roi_size:" << rect.size() << endl; // 返回rect的尺寸
cout << "roi_area:" << rect.area() << endl; // 返回rect的面积
cout << "roi_tl_point:" << rect.tl() << endl; // 返回rect的左上顶点的坐标
cout << "roi_br_point:" << rect.br() << endl; // 返回rect的右下顶点的坐标
cout << "roi_width:" << rect.width << endl; // 返回rect的宽度
cout << "roi_height:" << rect.height << endl; // 回rect的高度
namedWindow("src"); // 创建窗口;
namedWindow("roi");
imshow("src", src); // 显示图像
imshow("roi", roi);
}
int main()
{
// 提取roi区域
//roi();
roipic();
waitKey(0);
destroyAllWindows();
return 0;
}