#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
int main()
{
int row = 4, col = 4;
cv::Mat vertexmap(row, col, CV_32FC3);
for(int i = 0; i < row; ++i)
{
float* matRow = (float*)vertexmap.ptr(i);
for(int j = 0; j < col; ++j)
{
matRow[j*3] = j+i;
matRow[j*3 + 1] = j+i;
matRow[j*3 + 2] = j+i;
}
}
for(int i = 0; i < row; ++i)
{
float* matRow = (float*)vertexmap.ptr(i);
for(int j = 0; j < col; ++j)
{
cout<<matRow[j*3]<<" ";
cout<<matRow[j*3 + 1]<<" ";
cout<<matRow[j*3 + 2]<<" ";
}
cout<<"\n";
}
cout<<"OK\n";
return 0;
}
***********************************************************
~$ g++ -o test test.cpp `pkg-config --cflags --libs opencv`
~$ ./test
0 0 0 1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3 4 4 4
2 2 2 3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5 6 6 6
OK
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
int main()
{
int row = 4, col = 4;
cv::Mat vertexmap(row, col, CV_32FC3);
for(int i = 0; i < row; ++i)
{
float* matRow = (float*)vertexmap.ptr(i);
for(int j = 0; j < col; ++j)
{
matRow[j*3] = j+i;
matRow[j*3 + 1] = j+i;
matRow[j*3 + 2] = j+i;
}
}
for(int i = 0; i < row; ++i)
{
float* matRow = (float*)vertexmap.ptr(i);
for(int j = 0; j < col; ++j)
{
cout<<matRow[j*3]<<" ";
cout<<matRow[j*3 + 1]<<" ";
cout<<matRow[j*3 + 2]<<" ";
}
cout<<"\n";
}
cout<<"\nsecond\n";
for(int i = 0; i < vertexmap.rows; ++i)
{
float* matRow = (float*)vertexmap.ptr(i);
for(int j = 0; j < vertexmap.cols; ++j)
{
cout<<vertexmap.at<cv::Vec3f>(i,j)[0]<<" ";
cout<<vertexmap.at<cv::Vec3f>(i,j)[1]<<" ";
cout<<vertexmap.at<cv::Vec3f>(i,j)[2]<<" ";
}
cout<<"\n";
}
cout<<"OK\n";
int sz[]={3,2,2};
cv::Mat dm(3,sz,CV_32FC1,cv::Scalar(0));
cout<<dm.step[0]<<","<<dm.step[1]<<","<<dm.step[2]<<"\n";//similar to sizeof,get byte size of a dim?
cout<<dm.elemSize()<<"\n";//similar to sizeof,get byte size of an element
return 0;
}
~$ g++ -o test test.cpp `pkg-config --cflags --libs opencv`
~$ ./test
0 0 0 1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3 4 4 4
2 2 2 3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5 6 6 6
second
0 0 0 1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3 4 4 4
2 2 2 3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5 6 6 6
OK
16,8,4
4
6.为了搞定这个问题,OpenCV使用引用计数机制。其思路是让每个Mat 对象有自己的信息头,但共享同一个矩阵。这通过让矩阵指针指向同一地址而实现。而拷贝构造函数则只拷贝信息头和矩阵指针 ,而不拷贝矩阵。(都是引用,改变一个的话都会改变