#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 對象有自己的信息頭,但共享同一個矩陣。這通過讓矩陣指針指向同一地址而實現。而拷貝構造函數則只拷貝信息頭和矩陣指針 ,而不拷貝矩陣。(都是引用,改變一個的話都會改變