opencv c++中的9种方式创建矩阵Mat
//1 create a mat with random values
//2 create zeors or ones Mat
//3 common attribute of Mat
//4 access element of Mat by at
//5 access element of Mat by ptr
//6 access element of Mat by ptr and is continous
//7 get one row or one col of a Mat
//8 get several rows or several cols of a Mat
//9 get sub region of a Mat
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
Mat convertGray2Color(Mat img_gray) {
Mat img_color(img_gray.rows, img_gray.cols, CV_32FC3);
float tmp = 0;
for (int r = 0; r < img_gray.rows; r++) {
for (int c = 0; c < img_gray.cols; c++) {
tmp = img_gray.at<float>(r, c);
img_color.at<Vec3f>(r, c)[0] = abs(255 - tmp); //blue
img_color.at<Vec3f>(r, c)[1] = abs(127 - tmp); //green
img_color.at<Vec3f>(r, c)[2] = abs(0 - tmp); //red
}
}
return img_color;
}
int main()
{
std::cout << "Hello World!\n";
//1 create a mat with random values
Mat m = Mat(2, 3, CV_32FC(1));
randu(m, Scalar::all(0), Scalar::all(255));
//cout << "random m" << m << endl;
//imshow("randomMat", convertGray2Color(m));
//waitKey(0);
//2 create zeors or ones Mat
Mat m1;
m1.create(2, 3, CV_32FC(1));
Mat m_zero = Mat::zeros(2, 3, CV_32FC1);
Mat m_one = Mat::ones(2, 3, CV_32FC1);
//3 common attribute of Mat
Mat m_initList = (Mat_<int>(2, 3) << 1, 2, 3, 4, 5, 6);
cout << m_initList << endl;
cout << "size=" << m_initList.size() << endl;
cout << "row="<< m_initList.rows << endl;
cout << "cols="<< m_initList.cols << endl;
cout << "channels=" << m_initList.channels() << endl;
cout << "areas=" << m_initList.total() << endl;
cout << "dims=" << m_initList.dims << endl;
//4 access element of Mat by at
Mat m2 = (Mat_<int>(3, 2) << 11, 12, 33, 43, 51, 16);
for (int r = 0; r < m2.rows; r++) {
for (int c = 0; c < m2.cols; c++) {
cout << m2.at<int>(r, c)<<",";
}
cout << endl;
}
//5 access element of Mat by ptr
for (int r = 0; r < m2.rows; r++) {
const int* ptr = m2.ptr<int>(r);
for (int c = 0; c < m2.cols; c++) {
cout << ptr[c] << ",";
}
cout << endl;
}
//6 access element of Mat by ptr and is continous
if (m2.isContinuous()) {
int* ptr = m2.ptr<int>(0);
for (int n = 0; n<m2.rows * m2.cols; n++) {
cout << ptr[n] << ",";
}
}
//Vec
Vec<int, 3>vi(21, 32, 14);
cout << "rows=" << vi.rows << endl;
cout << "cols=" << vi.cols << endl;
cout << "vi[0]=" << vi[0] << endl;
cout << "vi(0)=" << vi(0) << endl;
//7 get one row or one col of a Mat
int r = 0, c=0;
Mat mr = m2.row(r);
Mat mc = m2.col(c);
cout << "row r of m2" << mr << endl;
cout << "col c of m2" << mc << endl;
//8 get several rows or several cols of a Mat
Mat m3 = (Mat_<int>(5, 5) << 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25);
Mat r_range = m3.rowRange(Range(2, 4));
cout << "r_range"<< r_range << endl;
Mat c_range = m3.colRange(Range(1, 3));
cout << "c_range" << r_range << endl;
Mat r_range_clone1 = m3.rowRange(2, 4).clone();
Mat r_range_clone2;
m3.rowRange(2, 4).copyTo(r_range_clone2);
//9 get sub region of a Mat
Mat roi1 = m3(Rect(Point(2, 1), Point(3, 2))); //point at left up corner, and right down corner
Mat roi2 = m3(Rect(2, 1, 2, 2)); //x,y,width, height
Mat roi3 = m3(Rect(Point(2, 1), Size(2, 2))); //point at left up corner, and size
return EXIT_SUCCESS;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!