cv::mat inside

#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 对象有自己的信息头,但共享同一个矩阵。这通过让矩阵指针指向同一地址而实现。而拷贝构造函数则只拷贝信息头和矩阵指针 ,而不拷贝矩阵。(都是引用,改变一个的话都会改变 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值