图像处理之像素的修改

1、像素的修改操作

c++取图像的像素:img.at操作。
读取彩色图像的像素值:
b通道: img.at(row,col)[0] ; // 表示彩色3通道,分别是b、g、r
g通道: img.at(row,col)[1] ;
r通道: img.at(row,col)[2];

示例图像:
示例图像
结果图像:
结果图像

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

using namespace std;
using namespace cv;

int main(int argc, char** args)
{
	//读取图像
	Mat img = imread("G:/testimg/img.png");
	if (img.empty())
	{
		printf("this image cannot load");
		return -1;
	}
	
	//修改像素
	Mat dst;
	dst.create(img.size(), img.type());
	int height = img.rows;
	int width = img.cols;
	int nc = img.channels();

	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
				int gray = img.at<uchar>(row, col);
				dst.at<uchar>(row, col) = gray;
			}
			else if(nc==3)
			{
				int b = img.at<Vec3b>(row, col)[0];
				int g = img.at<Vec3b>(row, col)[1];
				int r = img.at<Vec3b>(row, col)[2];
				dst.at<Vec3b>(row, col)[0] = 255 - b;
				dst.at<Vec3b>(row, col)[1] = 255 - g;
				dst.at<Vec3b>(row, col)[2] = 255 - r;
			}
			
		}
	}
	namedWindow("output", WINDOW_AUTOSIZE);
	imshow("output", dst);
	waitKey(0);
	return 0;
}
2、函数api:bitwise_not()

上式两个for循环进行的像素的修改,可以使用OpenCV内置函数 bitwise_not()进行替换。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值