Java + opencv 实现老照片特效滤镜
一、效果展示
如图所示, 左边为原始图片, 右边为经过处理后的老照片风格的图片, 图片来源与网络, 如果有侵权, 请联系本人删除.

二、实现思路
如何获取原始图片的像素值, 只需要遍历时获取对应通道的值, 通道和像素值关系对应 如下
0通道 - b
1通道 - g
2通道 - r
B = (int) (0.273 * r + 0.535 * g + 0.131 * b)
G = (int) (0.347 * r + 0.683 * g + 0.167 * b)
R = (int) (0.395 * r + 0.763 * g + 0.188 * b)
三、完整代码
package com.biubiu.example;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
/**
* @author :张音乐
* @date :Created in 2021/4/26 下午4:05
* @description:老照片特效
* @email: zhangyule1993@sina.com
* @version: 1.0
*/
public class ImageToOld {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
String path = "D:\\upload\\hanfu.jpg";
Mat img = Imgcodecs.imread(path);
if(img.empty()) {
System.out.println("cannot open file");
return;
}
Mat origin = img.clone();
HighGui.imshow("原始图", origin);
int height = img.height();
int width = img.width();
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
double b = img.get(i, j)[0];
double g = img.get(i, j)[1];
double r = img.get(i, j)[2];
// 计算新的图像中的RGB值
// 约束图像像素值,防止溢出, 变换后的RGB值要约束在0 ~ 255 之间
int B = (int) (0.273 * r + 0.535 * g + 0.131 * b);
int G = (int) (0.347 * r + 0.683 * g + 0.167 * b);
int R = (int) (0.395 * r + 0.763 * g + 0.188 * b);
// 照片变旧只需对图片的颜色空间进行处理
img.put(i, j, Math.max(0, Math.min(B, 255)), Math.max(0, Math.min(G, 255)), Math.max(0, Math.min(R, 255)));
}
}
HighGui.imshow("特效图", img);
HighGui.waitKey(0);
// 释放所有的窗体资源
HighGui.destroyAllWindows();
}
}
四、注意事项
需要性别识别网络模型的同学可以评论区域留下邮箱,我看到的话会给你发的, .代码无法调试通过也可以私信或者评论区提问, 如果我能解决都会提供帮助.
建议先读往期文章系统学习一下, 传送门: