图像像素遍历的三种方式

常用的图像像素遍历的三种方式有:

  1. 指针访问
  2. 迭代器iterator
  3. 动态地址at
#include <iostream>
#include <opencv2/opencv.hpp>
#include <cmath>

using namespace std;
using namespace cv;

//指针访问
void pointPixel(Mat &src, Mat &dst, int x);
//迭代器iterator
void iteratorPixel(Mat &src, Mat &dst, int x);
//动态地址at
void atPixel(Mat &src, Mat &dst, int x);

int main()
{
	Mat input = imread("D:/opencv_coding/opencv_8.jpg");
	imshow("the input image.", input);

	Mat output;
	pointPixel(input, output, 3);
	//pointPixel(input, output, 3);
	//atPixel(input, output, 3);
	
	imshow("the output image.", output);

	waitKey();
	system("pause");
	return 0;
}

void pointPixel(Mat &src, Mat &dst, int x)
{
	dst = src.clone();
	int rows = dst.rows;
	int cols = dst.cols * dst.channels();

	for (int i = 0; i < rows; i++)
	{
		uchar *data = dst.ptr<uchar>(i);
		for (int j = 0; j < cols; j++)
		{
			data[j] = data[j] / (x*x) + x / 2;
		}
	}
}

void iteratorPixel(Mat &src, Mat &dst, int x)
{
	dst = src.clone();
	Mat_<Vec3b>::iterator it = dst.begin<Vec3b>();
	Mat_<Vec3b>::iterator itend = dst.end<Vec3b>();
	for (; it != itend; ++it)
	{
		(*it)[0] = (*it)[0] / (x*x) + x / 2;
		(*it)[1] = (*it)[1] / (x*x) + x / 2;
		(*it)[2] = (*it)[2] / (x*x) + x / 2;
	}
}

void atPixel(Mat &src, Mat &dst, int x)
{
	dst = src.clone();
	int rows = dst.rows;
	int cols = dst.cols;
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			dst.at<Vec3b>(i, j)[0] = dst.at<Vec3b>(i, j)[0] / (x*x) + x / 2;
			dst.at<Vec3b>(i, j)[1] = dst.at<Vec3b>(i, j)[1] / (x*x) + x / 2;
			dst.at<Vec3b>(i, j)[2] = dst.at<Vec3b>(i, j)[2] / (x*x) + x / 2;
		}
	}
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值