OpenCV Mat实例详解 四

        OpenCV Mat实例详解三中详细介绍来了OpenCV Mat类的公有静态成员函数,下面介绍OpenCV Mat类的其他常用成员函数。

OpenCV Mat类常用成员函数

        Mat & adjustROI (int dtop, int dbottom, int dleft, int dright);

        dtop ROI 上边界移动值,如果为正,上边界向上移动,如果值为负,则向下移动。

        dbottom  ROI 下边界移动值,如果为正,下边界向下移动,如果值为负则向上移动。

        dleftm  ROI 左边界移动值,如果为正,左边界向左移动,如果值为负,则向右移动。

        dbottom  ROI 右边界移动值,如果为正,右边界向右移动,如果值为负,则向左移动。

        这是一个比较有用的函数,在机器视觉中作目标检测及尺寸量测时,在对ROI区域左动态调整时,会用到这个函数。

        下面来演示该函数的使用方法,新建一个空的控制台程序,在程序中加入如下代码:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{Mat src = imread("1.bmp");if(src.empty()){ cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);/*roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*/waitKey(0);return 0;
}

试运行,结果如下:

我们感兴趣的是载入图片左下角的那颗IC,但取得ROI区域时,却取入了部分别的原件图像。这种情况下就需要用到adjustROI函数来调整ROI区域。下面将程序中注释掉的程序段的注释取消,让adjustROI函数起作用,注释去掉后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{Mat src = imread("1.bmp");if(src.empty()){ cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);waitKey(0);return 0;
}

     试运行,结果如下:

最下面 的图片即为调用adjustROI函数调整后的ROI区域,不再包含误选的元件。

void assignTo (Mat &m, int type=-1) const;

        它提供了一种Mat type 转换方法,如果type为-1.则不做type转换,这是矩阵表达式引擎调用的内部使用的方法。

        修改上面的示例代码,来演示该函数的使用,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{Mat src = imread("1.bmp");if(src.empty()){ cout << "Load image failed!" << endl;return -1;}imshow("src", src);/*cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*/Mat dst;src.assignTo(dst, -1);imshow("dst",dst);waitKey(0);return 0;
}

试运行,结果如下:

再修改上面的示例代码,修改后如下:

#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{Mat src = imread("1.bmp");if(src.empty()){ cout << "Load image failed!" << endl;return -1;}//imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;/*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*/src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;waitKey(0);return 0;
}

试运行,结果如下:

        可以看出已实现src的type转换。虽然可实现type的转换,但是对于已有数据的Mat对象,这种转换可能导致原有数据没法使用及原有图像无法显示。

        at() 

    用以获取Mat对象数据矩阵某以元素的值或者给其赋值。其原型有以下几种形式;

template<typename _Tp >  _Tp &  at (int i0=0)

 template<typename _Tp >  _Tp &  at (int i0=0) const

template<typename _Tp >    _Tp &  at (int row, int col)

template<typename _Tp >   const _Tp &  at (int row, int col) const

template<typename _Tp >    _Tp & at (int i0, int i1, int i2)

template<typename _Tp >    const _Tp & at (int i0, int i1, int i2) const

template<typename _Tp >    _Tp & at (const int *idx)

template<typename _Tp >   const _Tp & at (const int *idx) const

template<typename _Tp >  _Tp &  at (const Vec< int, n > &idx) 

template<typename _Tp >  const _Tp &  at (const Vec< int, n > &idx) const 

template<typename _Tp >   _Tp &   at (Point pt)

template<typename _Tp >  const  _Tp &   at (Point pt) const

上面的at函数原型都有带const与不带const两种形式,二者的区别在于可接受的参数不同。

修改上面代码,来演示其应用,修改后得到代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){ cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*/int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}waitKey(0);return 0;
}

试运行,结果如下:

再修改以上代码,修改后如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){ cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*/Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}waitKey(0);return 0;
}

试运行,结果如下:

再修改以上代码,修改后如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){ cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*/Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}waitKey(0);return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*/Mat m = Mat(5, 5, CV_8UC1);int const length = m.cols * m.rows*m.channels();int mdata[25] = {};for (size_t i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}waitKey(0);return 0;
}

试运行,结果如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*/Mat m = Mat(5, 5, CV_8UC1);int const length = m.cols * m.rows*m.channels();Point mdata[25] = {};for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}waitKey(0);return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*/Mat m = Mat(5, 5, CV_8UC1);int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*/for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}waitKey(0);return 0;
}

 试运行,结果如下:

template<typename _Tp >  MatIterator_< _Tp >  begin ()

template<typename _Tp >  MatIterator_< _Tp >  begin () const

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*/Mat m = Mat(5, 5, CV_8UC1);typedef Vec<uchar, 1> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){*it_start = i + 1;i++;it_start++;}while (it1_start != it1_end){cout << *it1_start;it1_start++;}cout <<endl;cout << m << endl;waitKey(0);return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*/Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;MatIterator_<uchar> it_start = m.begin<uchar>();MatIterator_<uchar> it_end = m.end<uchar>();MatConstIterator_<uchar> it1_start = m.begin<uchar>();MatConstIterator_<uchar> it1_end = m.end<uchar>();int i = 0;while (it_start != it_end){*it_start = i + 1;i++;it_start++;}while (it1_start != it1_end){cout <<(int) *it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;waitKey(0);return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下: 

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*/Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;waitKey(0);return 0;
}

试运行,结果如下: 

int channels () const

返回Mat对象数据矩阵的通道数,该函数以多次出现再前面的示例中,这里就不再做演示。

int  checkVector (int elemChannels, int depth=-1, bool requireContinuous=true) const

        elemChannels    矩阵应具有的通道数或列数。对于2-D矩阵,当矩阵只有1列时,它应该具有elemChannels通道;当矩阵只有1个通道时,它应该有elemChannels列。对于三维矩阵,它应该只有一个通道。此外,如果平面的数量不是一个,那么每个平面内的行数必须是1;如果每个平面内的行数不是1,则平面数必须是1。

        depth                   矩阵元素位深度,缺省位-1,对任意深度设置位-1是合适的。

        requireContinuous 如果设置位true,矩阵需是连续的。

        返回值  如果不满足需求条件返回-1,否则返回矩阵元素个数,注意元素可能有多个通道。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*/Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;cout << src.checkVector(src.channels(), -1, true)<<endl;waitKey(0);return 0;
}

试运行,结果如下:

CV_NODISCARD_STD Mat clone () const

返回一个克隆的Mat对象。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*/Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;//cout << src.checkVector(src.channels(), -1, true)<<endl;Mat dst = src.clone();imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst channels:  " << dst.channels() << endl;waitKey(0);return 0;
}

试运行,结果如下:

Mat  col (int x) const

为指定的Mat的对象创建矩阵列头,该方法为指定的矩阵生成一个新的头,这是一个O(1)运算,与矩阵大小无关,x无论输入多大(0~源的cols范围内),新生成的矩阵cols都为1。新矩阵的基础数据与原始矩阵共享。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*//*Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;//cout << src.checkVector(src.channels(), -1, true)<<endl;Mat dst = src.clone();imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst channels:  " << dst.channels() << endl;*/Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;Mat dst = src.col(5);imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;waitKey(0);return 0;
}

试运行,结果如下:

再修改上面的示例代码,插入一行修改dst cols的代码,修改后的代码如下: 

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*//*Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;//cout << src.checkVector(src.channels(), -1, true)<<endl;Mat dst = src.clone();imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst channels:  " << dst.channels() << endl;*/Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;Mat dst = src.col(5); dst.cols = 100;imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;waitKey(0);return 0;
}

试运行,结果如下: 

可以看出,当生成新Mat矩阵头后可以通过修改cols值,来调整新生成的Mat对象的矩阵数据范围。

Mat  colRange (int startcol, int endcol) const

Mat colRange (const Range &r) const

这两个函数与上一个函数的作用差不多,只不过是这两个一开始就指定cols范围。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*//*Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;//cout << src.checkVector(src.channels(), -1, true)<<endl;Mat dst = src.clone();imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst channels:  " << dst.channels() << endl;*/Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;//Mat dst = src.col(5); Mat dst = src.colRange(5,150);Mat dst1 = src.colRange(Range(50,220));//dst.cols = 100;imshow("dst", dst);imshow("dst1", dst1);moveWindow("dst1", 0, 200);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst1 cols: " << dst1.cols << endl;cout << " dst1 rows: " << dst1.rows << endl;cout << " dst1 type: " << dst1.type() << endl;waitKey(0);return 0;
}

试运行,结果如下:  

用上面三个函数生成的Mat对象能否调用adjustROI函数来调整ROI区域呢?答案是肯定的。

void convertTo (OutputArray m, int rtype, double alpha=1, double beta=0) const

m 输出目标Mat 对象,如果其类型及大小不合适,将会被重新分配

alpha 像素BGR值比例缩放因子,可改变图像的明暗,不能缩改变图像大小,缺省为1

beta 添加到缩放因子的可选增量。

该方法将源像素值转换为目标数据类型。 saturate_cast<> 应用在末尾以避免可能的溢出,如下:

 修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

void  copySize (const Mat &m)

内部使用函数;正确地重新分配 _size、_step 数组

m 源Mat对象

 修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*//*Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;//cout << src.checkVector(src.channels(), -1, true)<<endl;Mat dst = src.clone();imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst channels:  " << dst.channels() << endl;*//**Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;//Mat dst = src.col(5); Mat dst = src.colRange(5,150);Mat dst1 = src.colRange(Range(50,220));//dst.cols = 100;imshow("dst", dst);imshow("dst1", dst1);moveWindow("dst1", 0, 200);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst1 cols: " << dst1.cols << endl;cout << " dst1 rows: " << dst1.rows << endl;cout << " dst1 type: " << dst1.type() << endl;*//**Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);src.convertTo(src, src.type(), 1.2, 0);imshow("src new", src);*/Mat src = Mat(5, 5, CV_8UC1);cout << src.size() << endl;Mat dst = Mat(8, 8, CV_8UC3);src.copySize(dst);cout << src.size() << endl;waitKey(0);return 0;
}

 试运行,结果如下:

void copyTo (OutputArray m) const

void  copyTo (OutputArray m, InputArray mask) const

拷贝到另一个Mat对象

m 目标Mat对象

mask 与 *this 大小相同的操作掩码。它的非零元素表示哪些矩阵元素需要复制。掩码必须为 CV_8U 类型,并且可以有 1 个或多个通道。

 修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{/*Mat src = imread("1.bmp");if(src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*//*Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;//cout << src.checkVector(src.channels(), -1, true)<<endl;Mat dst = src.clone();imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst channels:  " << dst.channels() << endl;*//**Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;//Mat dst = src.col(5); Mat dst = src.colRange(5,150);Mat dst1 = src.colRange(Range(50,220));//dst.cols = 100;imshow("dst", dst);imshow("dst1", dst1);moveWindow("dst1", 0, 200);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst1 cols: " << dst1.cols << endl;cout << " dst1 rows: " << dst1.rows << endl;cout << " dst1 type: " << dst1.type() << endl;*//**Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);src.convertTo(src, src.type(), 1.2, 0);imshow("src new", src);*//*Mat src = Mat(5, 5, CV_8UC1);cout << src.size() << endl;Mat dst = Mat(8, 8, CV_8UC3);src.copySize(dst);cout << src.size() << endl;*/Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);Mat dst;src.copyTo(dst);imshow("dst", dst);Mat mask(src.size(), CV_8UC1, cv::Scalar(255));mask(Rect(0, 0, src.cols/2, src.rows / 2)) = Scalar(0);Mat dst1(src.size(), src.type());src.copyTo(dst1, mask);imshow("dst1", dst1);waitKey(0);return 0;
}

试运行,结果如下:

void create (int rows, int cols, int type)

void  create (Size size, int type)

void create (int ndims, const int *sizes, int type)

void  create (const std::vector< int > &sizes, int type)

以上四个函数都是用以生成新的Mat对象,差别仅是接受的参数不同。

rows 新Mat对象的rows

cols 新Mat对象的cols

type 新Mat兑现的type

size 含新Mat对象rows、cols的vector对象

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

return -1;}imshow("src", src);cout << "src cols: "<<src.cols << endl;cout << "src rows: "<<src.rows << endl;cout << "src type: "<< src.type() << endl;*//*Rect rec(0, 130, 120,90);Mat roi = Mat(src, rec);imshow("roi", roi);moveWindow("roi", 300, 300);roi.adjustROI(-5, 0, -5, -10);imshow("roi adj", roi);moveWindow("roi adj", 500, 500);*//*Mat dst;src.assignTo(dst, -1);imshow("dst",dst);*//*src.assignTo(src, CV_16FC3);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;*//*int mdata[] = { 10,10 };Mat m = Mat(1, mdata, CV_8UC1);for (size_t i = 0; i < 10; i++){m.at<uchar>(i) = i;}for (size_t i = 0; i < 10; i++){cout << (int)m.at<uchar>(i) << endl;}*//*Mat m = Mat(5, 5, CV_8UC1);for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)m.at<uchar>(i, j) = i + j;}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++)cout << (int)m.at<uchar>(i, j);cout << endl;}*//*Mat m = Mat(5, 5, CV_8UC3);for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){m.at<uchar>((i, j),0) = j;m.at<uchar>((i, j), 1) = j + 1;m.at<uchar>((i, j), 2) = j+ 2;}}for (size_t i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){cout << (int)m.at<uchar>((i, j),0);cout << (int)m.at<uchar>((i, j), 1);cout << (int)m.at<uchar>((i, j), 2) << "  ";}cout << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//int const length = m.cols * m.rows*m.channels();//Point mdata[25] = {};//Vec<int, 25> mdata;/*for (int i = 0; i < m.rows; i++){for (int j = 0; j < m.cols; j++){mdata[i * 5 + j].x = i;mdata[i * 5 + j].y = j;}}*//*for (int i = 0; i < length; i++){mdata[i] = i;}for (size_t i = 0; i < length; i++){m.at<uchar>(mdata[i]) = (uchar)i;}for (int i = 0; i < length; i++){cout << (int)m.at<uchar>(mdata[i]) << endl;}*///Mat m = Mat(5, 5, CV_8UC1);//typedef Vec<uchar, 1> vec;//MatIterator_<uchar> it_start = m.begin<uchar>();//MatIterator_<uchar> it_end = m.end<uchar>();//MatConstIterator_<uchar> it1_start = m.begin<uchar>();//MatConstIterator_<uchar> it1_end = m.end<uchar>();/*while (it_start != it_end){*it_start = i + 1;i++;it_start++;}*//*Mat m = Mat(5, 5, CV_8UC3);typedef Vec<uchar, 3> vec;MatIterator_<vec> it_start = m.begin<vec>();MatIterator_<vec> it_end = m.end<vec>();MatConstIterator_<vec> it1_start = m.begin<vec>();MatConstIterator_<vec> it1_end = m.end<vec>();int i = 0;while (it_start != it_end){it_start[0] = 255-i;it_start[1] = 255 - 2 * i;it_start[1] = 255 - 3 * i;i++;it_start++;}while (it1_start != it1_end){cout <<*it1_start<<"  ";it1_start++;}cout <<endl;cout << m << endl;*//*Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;cout << "src channels:  " << src.channels() << endl;//cout << src.checkVector(src.channels(), -1, true)<<endl;Mat dst = src.clone();imshow("dst", dst);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst channels:  " << dst.channels() << endl;*//**Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);cout << "src cols: " << src.cols << endl;cout << "src rows: " << src.rows << endl;cout << "src type: " << src.type() << endl;//Mat dst = src.col(5); Mat dst = src.colRange(5,150);Mat dst1 = src.colRange(Range(50,220));//dst.cols = 100;imshow("dst", dst);imshow("dst1", dst1);moveWindow("dst1", 0, 200);cout << " dst cols: " << dst.cols << endl;cout << " dst rows: " << dst.rows << endl;cout << " dst type: " << dst.type() << endl;cout << " dst1 cols: " << dst1.cols << endl;cout << " dst1 rows: " << dst1.rows << endl;cout << " dst1 type: " << dst1.type() << endl;*//**Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);src.convertTo(src, src.type(), 1.2, 0);imshow("src new", src);*//*Mat src = Mat(5, 5, CV_8UC1);cout << src.size() << endl;Mat dst = Mat(8, 8, CV_8UC3);src.copySize(dst);cout << src.size() << endl;*//*Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src", src);Mat dst;src.copyTo(dst);imshow("dst", dst);Mat mask(src.size(), CV_8UC1, cv::Scalar(255));mask(Rect(0, 0, src.cols/2, src.rows / 2)) = Scalar(0);Mat dst1(src.size(), src.type());src.copyTo(dst1, mask);imshow("dst1", dst1);*/Mat src = imread("1.bmp");if (src.empty()){cout << "Load image failed!" << endl;return -1;}imshow("src 1th", src);src.create(src.rows-10, src.cols-10, src.type());imshow("src 2th", src);src.create(Size(src.rows, src.cols + 10), src.type());imshow("src 3th", src);int size[] = { 250,230 };src.create(2, size, CV_8UC3);imshow("src 4th", src);vector<int> vec(2);vec[0] = 230;vec[1] = 250;src.create(vec, CV_8UC3);imshow("src 5th", src);waitKey(0);return 0;
}

试运行,结果如下:

        由于篇幅关系,OpenCV Mat类的其他常用成员函数就暂时介绍到这里,剩余部分函数将在下篇中介绍。

        本篇博文示例是基于OpenCV4.8(opencv目录位于d盘根目录下)及VS2022。示例源码已上传到CSDN,其链接为:https://download.csdn.net/download/billliu66/88839772

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/471888.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

结构体对齐规则及为什么会有结构体对齐

前言&#xff1a; 大家在学习结构体中&#xff0c;在计算结构体大小时想必会很疑惑&#xff0c;为什么结构体的大小不是按照常理像数组一样一个字节一个字节的挨在一起放&#xff1f;今天带大家一起深入探讨一下背后的规则和原因。 结构体对齐规则&#xff1a; 结构体对齐其实…

【防网盘在线解压】Peazip 豌豆压缩 v9.7.0

软件介绍 Peazip 是一个免费的文件归档应用程序&#xff0c; 支持跨平台&#xff0c;是和WinRar、WinZip类似软件的开源免费替代品&#xff1b;支持压缩/ 存档到 7Z&#xff0c; ARC、Brotli BR、BZip2、GZip、 PAQ、PEA、RAR、自解压档案、TAR、WIM、XZ、Zstandard ZST、打开…

最短路径(dijstra算法,链式前向星,堆优化)

【模板】单源最短路径&#xff08;弱化版&#xff09; 对于这题我们使用邻接矩阵的话会导致弓箭复杂度会大大提升&#xff0c;所以我们就需要学习一种新的数据结构&#xff0c;名叫链式前向星&#xff0c;在链式前向星中&#xff0c;我们需要定义一个结构体数组&#xff0c;其中…

【实战】二、Jest难点进阶(一) —— 前端要学的测试课 从Jest入门到TDD BDD双实战(五)

文章目录 一、Jest 前端自动化测试框架基础入门二、Jest难点进阶1.snapshot 快照测试 学习内容来源&#xff1a;Jest入门到TDD/BDD双实战_前端要学的测试课 相对原教程&#xff0c;我在学习开始时&#xff08;2023.08&#xff09;采用的是当前最新版本&#xff1a; 项版本babe…

SVD奇异值分解

一、奇异值 奇异值&#xff08;Singular Values&#xff09;是线性代数中矩阵的重要性质之一&#xff0c;与奇异值分解&#xff08;SVD&#xff09;密切相关。让我们来更详细地了解一下奇异值的概念&#xff1a; 定义&#xff1a; 对于一个矩阵 ( A )&#xff0c;它的奇异值是…

C++ “雪花算法“原理

C雪花算法并不是传统的数据结构与算法而是一种崭新的分布式算法 属于深层次C 本篇文章就来描述一下雪花算法 什么是雪花算法: 雪花算法&#xff08;Snowflake&#xff09;是Twitter开源的一种分布式唯一ID生成算法。它可以在不依赖于数据库等其他存储设施的情况下&#xff0c…

算法刷题:有效三角形个数

有效三角形个数 .题目链接题目详情算法原理补充知识点双指针:对撞指针 我的答案 . 题目链接 有效三角形个数 题目详情 算法原理 补充知识点 有效三角形需要满足的条件: ab>cac>bbc>a 其实在满足1的时候,c是最大的,那么2和3是显然成立的,因此我们可以这样解题: 对…

platformio 提示 fatal error: TimeLib.h: No such file or directory 的解决方案

在platformio编译arduino项目的时候&#xff0c;如果提示fatal error: TimeLib.h: No such file or directory&#xff0c;解决方法有2&#xff1a; 方法1&#xff1a; 在项目的platformio.ini文件中&#xff0c;添加 lib_deps # Using library Id44方法2&#xff1a; 通过…

第三百四十九回

文章目录 1. 概念介绍2. 原理与方法2.1 知识对比2.2 使用方法 3. 示例代码4. 内容总结 我们在上一章回中介绍了"加密包crypto"相关的内容&#xff0c;本章回中将介绍characters包.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1. 概念介绍 在项目中会遇到获取字…

阿里云“BGP(多线)”和“BGP(多线)_精品”区别价格对比

阿里云香港等地域服务器的网络线路类型可以选择BGP&#xff08;多线&#xff09;和 BGP&#xff08;多线&#xff09;精品&#xff0c;普通的BGP多线和精品有什么区别&#xff1f;BGP&#xff08;多线&#xff09;适用于香港本地、香港和海外之间的互联网访问。使用BGP&#xf…

月薪30K-100K,新一波工作机会来了,你准备好了吗

纯血版鸿蒙发布&#xff0c;开启一个新时代 1月18日下午&#xff0c;在“鸿蒙千帆起”发布会上&#xff0c;华为揭秘鸿蒙生态和纯血鸿蒙星河版HarmonyOS NEXT进阶的新进展。“几年来&#xff0c;在众多伙伴和开发者的共同努力下&#xff0c;鸿蒙生态设备数已达8亿&#xff0c;…