访问单通道Mat对象中的值

#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	Mat m = (Mat_<int>(3, 2) << 11, 12, 33, 43, 51, 16);
	/*************************方法一*********************************/
	for (int r = 0; r < m.rows; r++)
	{
		for (int c = 0; c < m.cols; c++)
		{
			//cout << m.at<int>(r, c) << ",";
			cout << m.at<int>(Point(c, r)) << ",";//效果一样
		}
		cout << endl;
	}
	/*************************方法二*********************************/
	for (int r = 0; r < m.rows; r++)
	{
		const int * ptr = m.ptr<int>(r);
		for (int c = 0; c < m.cols; c++)
		{
			//cout << ptr[c] << ",";
			cout << *(ptr + c) << ",";
		}
		cout << endl;
	}
	/**************************方法三*********************************/
	if (m.isContinuous())
	{
		int*ptr = m.ptr<int>(0);
		for (int n = 0; n < m.rows*m.cols; n++)
			cout << ptr[n] << ",";
	}
	/*************************方法四*********************************/
	for (int r = 0; r < m.rows; r++)
	{
		for (int c = 0; c < m.cols; c++)
		{
			cout << *((int*)(m.data + m.step[0] * r + c * m.step[1])) << ",";
			//step[0]代表每一行所占字节数,若有间隔,间隔也会被当作字节数的一部分算在其中;
			//step[1]代表每一个数值所站的字节数;
			//data是指向第一个数值的指针。
		}
		cout << endl;
	}
	waitKey(0);
}

从取值效率上说,直接使用指针的形式取值最快,使用at是最慢的,但是可读性很高。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值