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