有关Mat通道的数据结构的发现

OpenCV Mat Channel的地址问题

最近因为做数字图像处理大实验的原因学习了一下warpPerspective函数的源码,在阅读源码时碰到了一些困难,其中一个就是关于Mat不同通道的数据是如何组织的。于是我做了一个小实验,下面是实验的代码:

#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>

using namespace std;
using namespace cv;

int main()
{
	int M[18];
	for (int i = 0; i < 18; i++)
	{
		M[i] = i;
	}
	Mat matA(3, 3, CV_32SC2, M);// matA中数据的地址即是M中数据的地址,改变M中的数据就会改变matA中的数据
	cout << "channel1: " << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << matA.at<Vec2i>(i, j)[0] << ' ';
		}
		cout << endl;
	}
	cout << "channel2: " << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << matA.at<Vec2i>(i, j)[1] << ' ';
		}
		cout << endl;
	}
	cout << "test3: " << endl;
	for (int i = 0; i < 9; i++)
	{
		*(M + i) += 100;
	}
	cout << "channel1_after: " << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << matA.at<Vec2i>(i, j)[0] << ' ';
		}
		cout << endl;
	}
	cout << "channel2_after: " << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << matA.at<Vec2i>(i, j)[1] << ' ';
		}
		cout << endl;
	}
	return 0;
}

代码的输出结果是:
在这里插入图片描述初始化一个数组M[18]使他的值依次为0~17,再用M初始化一个row=3,col=3,channel=2的矩阵,输出矩阵每一个通道里储存的9个数,结果一通道为{0,2,4,6,8,10,12,14,16},二通道为{1,3,5,7,9,11,13,15,17},把数组M前9个数每个加100,再打印矩阵的每个通道的值,发现通道一为{100,102,104,106,108,10,12,14,16},二通道为{101,103,105,107,9,11,13,15,17}
这说明Mat内部对待通道的存储方式是这样的
在这里插入图片描述
而不是这样的
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值