javacv图片美颜处理,视频美颜处理
javacv图片美颜处理,视频美颜处理
国产剧明星演戏自带十级滤镜,是众所周知的秘密;
使用opencv也能实现一定的美颜效果:
一、图片美颜
代码
package top.lingkang.test.demo;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import java.io.File;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
/**
* @author lingkang
* Created by 2022/6/6
*/
public class Demo01 {
public static void main(String[] args) {
Mat result = new Mat();
Mat image = imread("C:\\Users\\Administrator\\Desktop\\temp\\2.png");
int level = 18;// 值越大,过滤强度越大
bilateralFilter(image, result, level, level * 2, level / 2);
File out = new File("d:/2.png");
if (out.exists())
out.delete();
imwrite(out.getPath(), result);
}
}
二、视频美颜
视频美颜代码
package top.lingkang.test.demo;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.JavaFXFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_videoio.VideoCapture;
import static org.bytedeco.opencv.global.opencv_imgproc.bilateralFilter;
import static org.bytedeco.opencv.global.opencv_videoio.CAP_PROP_FRAME_HEIGHT;
import static org.bytedeco.opencv.global.opencv_videoio.CAP_PROP_FRAME_WIDTH;
/**
* @author lingkang
* Created by 2022/6/6
*/
public class Demo02 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("最帅的↓↓↓↓↓");
primaryStage.setWidth(600);
primaryStage.setHeight(800);
ImageView imageView = new ImageView();
imageView.setFitWidth(550);
imageView.setFitHeight(750);
HBox box = new HBox();
box.getChildren().add(imageView);
primaryStage.setScene(new Scene(box));
primaryStage.show();
int level = 12;// 值越大,过滤强度越大
// 图像处理
new Thread(new Runnable() {
@Override
public void run() {
JavaFXFrameConverter fxFrameConverter = new JavaFXFrameConverter();
OpenCVFrameConverter openCVFrameConverter = new OpenCVFrameConverter.ToMat();
try {
// 调用摄像头
VideoCapture capture = new VideoCapture();
capture.set(CAP_PROP_FRAME_WIDTH, 600);
capture.set(CAP_PROP_FRAME_HEIGHT, 800);
if (!capture.open(0)) {
System.out.println("无法打开摄像头!");
return;
}
Mat mat = new Mat();
int i = 0;
while (true) {
// 读取
capture.read(mat);
Mat result = new Mat();
// 数值自行调整
bilateralFilter(mat, result, level, level, 100);
Frame convert = openCVFrameConverter.convert(result);
Image image = fxFrameConverter.convert(convert);
imageView.setImage(image);
Thread.sleep(100);
if (i > 200) {// 简单循环
capture.release();
capture.close();
break;
}
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public static void main(String[] args) {
launch(args);
}
}