OpenCV/C++ 图片复古滤镜处理

偷偷拿来记录一下萌新的cs路——day 41 

R = 0.393r + 0.769g + 0.189b

G = 0.349r + 0.686g + 0.168b

B = 0.272r + 0.534g + 0.131b

#include<math.h>
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
	auto frame = imread("cat.jpg");  // 读取图像

	int width = frame.cols;
	int heigh = frame.rows;
	RNG rng;
	Mat img(frame.size(), CV_8UC3);
	for (int y = 0; y < heigh; y++)
	{
		uchar* P0 = frame.ptr<uchar>(y);
		uchar* P1 = img.ptr<uchar>(y);
		for (int x = 0; x < width; x++)
		{
			float B = P0[3 * x];
			float G = P0[3 * x + 1];
			float R = P0[3 * x + 2];
			float newB = 0.272 * R + 0.534 * G + 0.131 * B;
			float newG = 0.349 * R + 0.686 * G + 0.168 * B;
			float newR = 0.393 * R + 0.769 * G + 0.189 * B;
			if (newB < 0)newB = 0;
			if (newB > 255)newB = 255;
			if (newG < 0)newG = 0;
			if (newG > 255)newG = 255;
			if (newR < 0)newR = 0;
			if (newR > 255)newR = 255;
			P1[3 * x] = (uchar)newB;
			P1[3 * x + 1] = (uchar)newG;
			P1[3 * x + 2] = (uchar)newR;
		}
	}

	imshow("origin", frame);
	imshow("filter", img);
	waitKey();
	imwrite("E:/filter.jpg", img);  // 将img输出到图像文件
	destroyAllWindows();
	return 0;
}

运行结果

学了更多知识还会回来更新的!有误之处请大佬指正,感谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值