c++ 利用源码实现彩色图像的反相,对像素值进行操作

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

using namespace std;
using namespace cv;

int main()
{
	Mat example = imread("D:\\VC项目\\img1.jpg");
	if (!example.data)
	{
		cout << "图片路径错误!!!" << endl;
	}
	Mat out_ex1;
	cvtColor(example, out_ex1, COLOR_BGR2GRAY);
	Mat out_ex2 = example.clone();
	Mat out_ex3 = example.clone();
	int height = example.rows;
	int weight = example.cols;
	int gray;
	int nc_example = example.channels();

	//操作像素实现反相
	for (int row = 0; row < height;row++)
	{
		for (int col = 0; col < weight; col++)
		{
			if (nc_example == 1)
			{
				gray = out_ex1.at<uchar>(row, col);
				out_ex1.at<uchar>(row, col) = 255 - gray;
			}
			else
			{
				int out_ex2_b = out_ex2.at<Vec3b>(row, col)[0]; //Vec3b把rgb图像的每个通道读取成uchar类型
				int out_ex2_g = out_ex2.at<Vec3b>(row, col)[1];//Vec3f把数据读成float类型
				int out_ex2_r = out_ex2.at<Vec3b>(row, col)[2];
				out_ex2.at<Vec3b>(row, col)[0] = 255 - out_ex2_b;
				out_ex2.at<Vec3b>(row, col)[1] = 255 - out_ex2_g;
				out_ex2.at<Vec3b>(row, col)[2] = 255 - out_ex2_r;
			}
		}
	}

	bitwise_not(example, out_ex3);//直接利用opencv中的函数实现反转
	imshow("example", example);
	imshow("out_ex1", out_ex1);
	imshow("out_ex2", out_ex2);
	imshow("out_ex3", out_ex3);
	waitKey(0);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值