//2020.6.12_p35_Utilisez la fonction membre rowRang ou colRange pour obtenir les lignes ou colonnes continues de la matrice
// Range (int _start, int _end), ceci est une séquence fermée et ouverte à gauche (_start, _end), Range (2, 5), la séquence générée est 2, 3, 4. Le package de gauche à droite n'inclut pas
// la matrice de clonage de copie
//2020.6.12_p35_使用成员函数rowRang或colRange得到矩阵的连续行或者连续列
//Range(int _start,int _end),这是一个左闭有开的序列[_start,_end), Range(2,5),产生的是2,3,4的序列。左包右不包括
//复制克隆矩阵
#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);
//访问matrix的第2行与第3行。
//Mat r_range = matrix.rowRange(Range(2,4));
//也可以直接写
Mat r_range = matrix.rowRange(2, 4);
for (int r = 0; r < r_range.rows; r++)
{
for (int c = 0; c < r_range.cols; c++)
{
cout << r_range.at<int>(r, c) << ",";
}
cout << endl;
}
cout << "获取第1,2列" << endl;
Mat c_range = matrix.colRange(1, 3);//获取第1,2列
for (int r = 0; r < c_range.rows; r++)
{
for (int c = 0; c < c_range.cols; c++)
{
cout << c_range.at<int>(r, c) << ",";
}
cout << endl;
}
cout << "修改了r_range里面的第一个值后:" << endl;
r_range.at<int>(0, 0) = 1000;
cout << "r_range里面的第一个值后:r_range结果" << endl;
for (int r = 0; r < r_range.rows; r++)
{
for (int c = 0; c < r_range.cols; c++)
{
cout << r_range.at<int>(r, c) << ",";
}
cout << endl;
}
cout << "r_range里面的第一个值后:matrix结果" << endl;
for (int r = 0; r < matrix.rows; r++)
{
for (int c = 0; c < matrix.cols; c++)
{
cout << matrix.at<int>(r, c) << ",";
}
cout << endl;
}
cout << endl;
//复制克隆矩阵,clone,copyTo
Mat r_range1 = matrix.rowRange(2, 4).clone();
Mat r_range2;
matrix.rowRange(2, 4).copyTo(r_range2);
cout << "clone结果:" << endl;
for (int r = 0; r < r_range1.rows; r++)
{
for (int c = 0; c < r_range1.cols; c++)
{
cout << r_range1.at<int>(r, c) << ",";
}
cout << endl;
}
cout << "copyTo结果:" << endl;
for (int r = 0; r < r_range2.rows; r++)
{
for (int c = 0; c < r_range2.cols; c++)
{
cout << r_range2.at<int>(r, c) << ",";
}
cout << endl;
}
}