2020.6.12_p38_OpenCV使用Rect类获取矩阵中某一特定的矩形区域

 

2020.6.12_p38_使用Rect类获取矩阵中某一特定的矩形区域

//2020.6.12_p38_使用Rect类获取矩阵中某一特定的矩形区域
#include <opencv2/core.hpp>
using namespace cv;
#include <iostream>
using namespace std;int main(int argc, char *argv[])
{Mat matrix = (Mat_<int>(5, 5) << 1,  2,  3,  4,  5, 6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);Mat roi1 = matrix(Rect(Point(2, 1), Point(4, 3)));//Point(2, 1)左上角坐标,Point(4, 3)不是右下角坐标.Point(4,3)-Point(2,1)得到宽:4-2=2,高:3-1=2Mat roi2 = matrix(Rect(2,1,2,2));//起点坐标2,1;宽2,高2Mat roi3 = matrix(Rect(Point(2,1),Size(2,2)));//左上角坐标,+尺寸Mat roi5 = matrix(Rect(2, 1, 2, 2)).clone();//克隆,复制原始位置数据for (int r = 0; r < roi3.rows; r++){const int *ptr = roi3.ptr<int>(r);for (int c = 0; c < roi3.cols; c++){cout << ptr[c] << ",";}cout << endl;}cout << "roi1--1:" << endl;for (int r = 0; r < roi1.rows; r++){const int *ptr = roi1.ptr<int>(r);for (int c = 0; c < roi1.cols; c++){cout << ptr[c] << ",";}cout << endl;}cout << "使用at访问元素:" << endl;for (int r = 0; r < roi1.rows; r++){for (int c = 0; c < roi1.cols; c++){cout << roi1.at<int>(r, c) << ",";}cout << endl;}}