OpenCV中cv::Mat矩阵的四种遍历

第一种:at方法遍历

at方法遍历 单通道

#include<iostream>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main() {
	cv::Mat a = (cv::Mat_<uchar>(4, 4) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
	cout << a << endl;
	cout << endl;
	//at方法遍历并修改,单通道
	for (int i = 0; i < a.rows; i++) {
		for (int j = 0; j < a.cols; j++) {//j每次移动代表一个像素点的距离,即每次移动一个通道的距离
			a.at<uchar>(i, j) = i + j;
		}
	}
	//
	cout << a << endl;

	return 0;
}

运行结果
在这里插入图片描述

at方法遍历 多通道

(下面这种方式,既能遍历,又能修改)

#include<iostream>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main() {
	cv::Mat a(4, 4, CV_8UC3, cv::Scalar(1, 2, 3));
	cout << a << endl;
	cout << endl;
	//at方法遍历遍历并修改,三通道
	for (int i = 0; i < a.rows; i++) {
		for (int j = 0; j < a.cols; j++) {//j每次移动代表一个像素点的距离,即每次移动三个通道的距离
			cv::Vec3b temp_channel_array;//temp_channel_array临时的通道数组
			temp_channel_array[0] = 4;
			temp_channel_array[1] = 5;
			temp_channel_array[2] = 6;
			a.at<cv::Vec3b>(i, j) = temp_channel_array;
		}
	}
	cout << a << endl;

	return 0;
}

运行结果:
在这里插入图片描述
对比程序:(下面这种方式,只能遍历,不能修改)

#include<iostream>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main() {
	cv::Mat a(4, 4, CV_8UC3, cv::Scalar(1, 2, 3));
	cout << a << endl;
	cout << endl;
	//at方法遍历遍历,三通道
	for (int i = 0; i < a.rows; i++) {
		for (int j = 0; j < a.cols; j++) {//j每次移动代表一个像素点的距离,即每次移动三个通道的距离
			cv::Vec3b temp_channel_array= a.at<cv::Vec3b>(i, j);//temp_channel_array临时的通道数组
			temp_channel_array[0] = 4;
			temp_channel_array[1] = 5;
			temp_channel_array[2] = 6;
		}
	}
	cout << a << endl;

	return 0;
}

运行结果
在这里插入图片描述

第二种:指针ptr方法 遍历(两种写法)

第一种写法

#include<iostream>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main() {
	cv::Mat a(4, 4, CV_8UC3, cv::Scalar(1, 2, 3));
	cout << a << endl;
	cout << endl;
	//指针ptr方法遍历遍历,三通道
	for (int i = 0; i < a.rows; i++) {
		uchar* ptr = a.ptr<uchar>(i);//ptr表示:指向第i行中第一个像素点中第一个通道的指针?
		for (int j = 0; j < a.cols; j++) {
			*(ptr+3*j+0) = 4;//(ptr+3*j+0)表示:指向第i行第3*j列像素点中第一个通道的指针?
			*(ptr+3*j+1) = 5;//(ptr+3*j+1)表示:指向第i行第3*j列像素点中第二个通道的指针?
			*(ptr+3*j+2) = 6;//(ptr+3*j+2)表示:指向第i行第3*j列像素点中第三个通道的指针?
		}
	}
	cout << a << endl;

	return 0;
}

运行结果
在这里插入图片描述
更一般的写法:把里面的3改成矩阵的通道数 a.channels(),如下:

#include<iostream>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main() {
	cv::Mat a(4, 4, CV_8UC3, cv::Scalar(1, 2, 3));
	cout << a << endl;
	cout << endl;
	//指针ptr方法遍历遍历,三通道
	for (int i = 0; i < a.rows; i++) {
		uchar* ptr = a.ptr<uchar>(i);//ptr表示:指向第i行中第一个像素点中第一个通道的指针?
		for (int j = 0; j < a.cols; j++) {
			*(ptr + a.channels()*j + 0) = 4;//(ptr + a.channels()*j + 0)表示:指向第i行第a.channels()*j列像素点中第一个通道的指针?
			*(ptr + a.channels()*j + 1) = 5;//(ptr + a.channels()*j + 1)表示:指向第i行第a.channels()*j列像素点中第二个通道的指针?
			*(ptr + a.channels()*j + 2) = 6;//(ptr + a.channels()*j + 2)表示:指向第i行第a.channels()*j列像素点中第三个通道的指针?
		}
	}
	cout << a << endl;

	return 0;
}

运行结果
在这里插入图片描述
上面这种写法,是能看到目前这个指针 ptr + a.channels()*j + 0 所指向的是位于第几行第几列中的第几个通道的,下面的写法就不具备这种优势。

第二种写法

#include<iostream>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main() {
	cv::Mat a(4, 4, CV_8UC3, cv::Scalar(1, 2, 3));
	cout << a << endl;
	cout << endl;
	//指针ptr方法遍历遍历,三通道
	for (int i = 0; i < a.rows; i++) {
		uchar* ptr = a.ptr<uchar>(i);//ptr表示:指向第i行中第一个像素点中第一个通道的指针?
		for (int j = 0; j < a.cols*a.channels(); j++) {//a.cols*a.channels()表示:矩阵中,第i行中所有的通道数。ptr指针每次仅移动一个通道的距离
			*(ptr + j) = 4;
			//ptr[j] = 4;//或者这样也行,等价的写法
		}
	}
	cout << a << endl;

	return 0;
}

运行结果
在这里插入图片描述
上面这种写法,感觉是看不到目前这个指针 ptr+j 所指向的是位于第几行第几列中的第几个通道的,感觉不是很明了。

第三种:使用迭代器遍历

暂无

第四种:直接使用 矩阵元素的地址定位 方式遍历

暂无

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值