OpenCV学习(C++)——像素点的读取

OpenCV学习(C++)——像素点的读取

C++中的像素遍历与访问

对彩色图片处理:(原像素-255)反色 基于数组下表改变每一个像素点

数组遍历

void QuickDemo::pixel_visit_demo(Mat &image)
{
	int w = image.cols;//获取图像宽度
	int h = image.rows;//获取图像高度
	int dims = image.channels();//获取通道数
	for (int row = 0; row < h; row++)
	{
		for (int col = 0; col < w; col++)
		{
			if (dims == 1)//灰度图像
			{
				int pv = image.at<uchar>(row, col);//单通道 像素值是uchar型
				image.at<uchar>(row, col) = 255 - pv;
			}
			if (dims == 3)//彩色图像
			{
				Vec3b bgr=image.at<Vec3b>(row, col);//返回三个值哦	可以把bgr这个结构看成数组
				image.at<Vec3b>(row, col)[0] = 255 - bgr[0];
				image.at<Vec3b>(row, col)[1] = 255 - bgr[1];
				image.at<Vec3b>(row, col)[2] = 255 - bgr[2];
			}
		}
	}
		imshow("像素读写演示", image);
}

指针方式遍历

把上面的代码中的双重for循环改成下main这个就行了

	for (int row = 0; row < h; row++)
	{
		uchar *current_row = image.ptr<uchar>(row);
		for (int col = 0; col < w; col++)
		{
			if (dims == 1)//灰度图像
			{
				int pv = *current_row;//单通道 像素值是uchar型
				*current_row++ = 255 - pv;
			}
			if (dims == 3)//彩色图像
			{
				*current_row++ = 255 - *current_row;//往后走三个位置(三通道)
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
			}
		}
	}

记得自己声明void QuickDemo::pixel_visit_demo(Mat &image);这个函数

主函数

#include<opencv2/opencv.hpp>
#include<iostream>
#include<quickjopencv.h>


using namespace std;
using namespace cv;

int main()
{
	Mat src = imread("C:/Users/LENOVO/Desktop/work/picture/2.png");
	if (src.empty())//没找到图像
	{
		printf("could not find picture!\n");
		return -1;
	}
	imshow("输入窗口", src);//输入窗口名字:输入窗口

	QuickDemo qd;
	qd.pixel_visit_demo(src);
	
	waitKey(0);
	destroyAllWindows();
	return 0;
}
  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wy-1226

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值