本文主要是介绍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;}}
这篇关于2020.6.12_p38_OpenCV使用Rect类获取矩阵中某一特定的矩形区域的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!