OpenCV中6种访问Mat元素的方法

30 篇文章 5 订阅

Mat中不管是以at访问还是ptr访问,都是行优先 ,先Y轴后X轴(即先行后列)

1、使用at访问

/*
*OpenCV2中Mat的at操作访问矩阵元素
*
*/

#include <highgui.h>

using namespace std ;
using namespace cv ;

int main()
{
	Mat image = imread("forest.jpg") ;
	imshow("image" , image) ;

	//三通道图像,at(y , x)索引是先行(y轴) , 后列(x轴)
	//第一种方法
	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols / 2 ; ++ w)
		{
			image.at<Vec3b>(h , w)[0] = 255 ;
			image.at<Vec3b>(h , w)[1] = 0 ;
			image.at<Vec3b>(h , w)[2] = 0 ;
		}
	}
	imshow("color1" , image) ;
	//方法二
	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols / 2 ; ++ w)
		{
			Vec3b &bgr = image.at<Vec3b>(h , w) ;
			bgr.val[0] = 0 ;
			bgr.val[1] = 255 ;
			bgr.val[2] = 0 ;
		}
	}
	imshow("color2" , image) ;

	image = imread("forest.jpg" , 0) ;
	//单通道图像,at(y , x)索引是先行(y轴) , 后列(x轴)
	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols / 2 ; ++ w)
		{
			image.at<uchar>(h , w) = 128 ;
		}
	}
	imshow("gray" , image) ;
	waitKey(0) ;
	return 0 ;
}

2、使用ptr访问

/*
*OpenCV2中Mat操作ptr访问矩阵元素
*
*/

#include <highgui.h>

using namespace std ;
using namespace cv ;

int main()
{
	Mat image = imread("forest.jpg") ;
	imshow("image" , image) ;

	//三通道图像,at(y , x)索引是先行(y轴) , 后列(x轴)
	//第一种方法
	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols / 2 ; ++ w)
		{
			uchar *ptr = image.ptr<uchar>(h , w) ;
			ptr[0] = 255 ;
			ptr[1] = 0 ;
			ptr[2] = 0 ;
		}
	}
	imshow("color1" , image) ;
	//第二种方法
	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols / 2 ; ++ w)
		{
			Vec3b *ptr = image.ptr<Vec3b>(h , w) ;
			ptr->val[0] = 0 ;
			ptr->val[1] = 255 ;
			ptr->val[2] = 0 ;
		}
	}
	imshow("color2" , image) ;

	image = imread("forest.jpg" , 0) ;
	//单通道图像,at(y , x)索引是先行(y轴) , 后列(x轴)
	//第一种方法
	for(int h = 0 ; h < image.rows ; ++ h)
	{
		uchar *ptr = image.ptr(h) ;
		for(int w = 0 ; w < image.cols / 2 ; ++ w)
		{
			ptr[w] = 128 ;
		}
	}
	imshow("gray1" , image) ;

	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols / 2 ; ++ w)
		{
			uchar *ptr = image.ptr<uchar>(h , w) ;
			*ptr = 255 ;
		}
	}
	imshow("gray2" , image) ;

	waitKey(0) ;
	return 0 ;
}

3、迭代器访问

/*
*OpenCV2中Mat的迭代器访问Mat元素
*
*/

#include <highgui.h>

using namespace std ;
using namespace cv ;

int main()
{
	Mat image = imread("forest.jpg") ;
	imshow("image" , image) ;

	//三通道图像
	Mat_<Vec3b>::iterator it = image.begin<Vec3b>() ;
	Mat_<Vec3b>::iterator itend = image.end<Vec3b>() ;

	for(;it != itend ; ++ it)
	{
		(*it)[0] = 255 ;
		(*it)[1] = 0 ;
		(*it)[2] = 0 ;
	}
	imshow("color1" , image) ;
	//单通道图像
	image = imread("forest.jpg" , 0) ;
	Mat_<uchar>::iterator it1 = image.begin<uchar>() ;
	Mat_<uchar>::iterator itend1 = image.end<uchar>() ;
	for (;it1 != itend1 ; ++ it1)
	{
		(*it1) = 128 ;
	}

	imshow("gray" , image) ;
	waitKey(0) ;
	return 0 ;
}

4、data操作

/*
*Mat中的data操作
*/

#include <highgui.h>

using namespace std ;
using namespace cv ;

int main()
{
	Mat image = imread("forest.jpg") ;
	imshow("image" , image) ;
	//三通道
	uchar *data = image.data ;

	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols/2 ; ++ w)
		{
			*data ++ = 128 ;
			*data ++ = 128 ;
			*data ++ = 128 ;
		}
	}
	imshow("data" , image) ;
	//单通道
	image = imread("forest.jpg" , 0) ;
	imshow("image" , image) ;

	data = image.data ;
	for(int h = 0 ; h < image.rows ; ++ h)
	{
		for(int w = 0 ; w < image.cols/2 ; ++ w)
		{
			*data ++ = 128 ;
		}
	}
	imshow("data1" , image) ;

	waitKey(0) ;
	return 0 ;
}

5、row , col操作

#include <highgui.h>

using namespace std ;
using namespace cv ;

int main()
{
	Mat image = imread("forest.jpg") ;
	imshow("image" , image) ;

	for(int i = 0 ; i < 100 ; ++ i)
	{
		image.row(i).setTo(Scalar(0 , 0 , 0)) ;//设定第i行数据
		image.col(i).setTo(Scalar(0 , 0 , 0)) ;//设定第i列数据
	}

	imshow("image" , image) ;
	waitKey(0) ;
	return 0 ;
}

6、高效访问

#include <highgui.h>

using namespace std ;
using namespace cv ;

int main()
{
	Mat image = imread("forest.jpg") ;
	imshow("image" , image) ;
	//单通道多通道都适用
	int nRows = image.rows ;
	int nCols = image.cols * image.channels() ;

	if(image.isContinuous())
	{
		nCols = nRows * nCols ;
		nRows = 1 ;
	}

	for(int h = 0 ; h < nRows ; ++ h)
	{
		uchar *ptr = image.ptr<uchar>(h) ;
		for(int w = 0 ; w < nCols ; ++ w)
		{
			//ptr[w] = 128 ;
			*ptr ++ = 128 ;
		}
	}

	imshow("high" , image) ;

	waitKey(0) ;
	return 0 ;
}

转自:http://lib.csdn.net/article/opencv/28174

  • 27
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值