1、javacv 1.5.10版本
java
package com.example.demo.ffpemg;
import lombok.SneakyThrows;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.opencv.opencv_core.Mat;
import javax.swing.*;
import java.awt.image.BufferedImage;
import static org.bytedeco.opencv.global.opencv_core.BORDER_DEFAULT;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
import static org.bytedeco.opencv.global.opencv_imgproc.Laplacian;
public class OpenCVTest1 {
@SneakyThrows
public static void main(String[] args) {
// 读取图片
final Mat src = imread("data/boldt.jpg");
display(src, "Input");
// 拉普拉斯滤波器(Laplacian Filter)是图像处理中的一种常见操作,它用于检测图像中的边缘。
// 拉普拉斯滤波器是一种二阶导数滤波器,能够突出图像中的快速变化部分,如边缘和纹理
// 应用拉普拉斯滤波器
// 注意:这里的参数可以根据需要进行调整
// 第三个参数表示滤波器的深度,通常使用与源图像相同的深度
// 第四个参数表示滤波器的大小,必须是正奇数
// 第五个参数表示scale因子,用于控制结果的缩放
// 第六个参数表示delta值,该值会被加到最终的像素值上
final Mat dest = new Mat();
Laplacian(src, dest, src.depth(), 1, 3, 0, BORDER_DEFAULT);
display(dest, "Laplacian");
}
public static void display(Mat image, String caption) {
// Create image window named "My Image".
final CanvasFrame canvas = new CanvasFrame(caption, 1.0);
// Request closing of the application when the image window is closed.
canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Convert from OpenCV Mat to Java Buffered image for display
final BufferedImage bi = OpenCVUtilsJava.toBufferedImage(image);
// Show image on window.
canvas.showImage(bi);
}
}
OpenCVUtilsJava.java
java
package com.example.demo.ffpemg;
import org.bytedeco.javacv.*;
import org.bytedeco.opencv.opencv_core.Mat;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
/**
* Helper methods that simplify use of OpenCV API.
*/
public class OpenCVUtilsJava {
/**
* Load an image and show in a CanvasFrame. If image cannot be loaded the application will exit with code 1.
*
* @return loaded image
*/
public Mat loadAndShowOrExit(File file) {
return loadAndShowOrExit(file, IMREAD_COLOR);
}
/**
* Load an image and show in a CanvasFrame. If image cannot be loaded the application will exit with code 1.
*
* @param flags Flags specifying the color type of a loaded image:
* <ul>
* <li> `>0` Return a 3-channel color image</li>
* <li> `=0` Return a gray scale image</li>
* <li> `<0` Return the loaded image as is. Note that in the current implementation
* the alpha channel, if any, is stripped from the output image. For example, a 4-channel
* RGBA image is loaded as RGB if the `flags` is greater than 0.</li>
* </ul>
* Default is gray scale.
* @return loaded image
*/
public static Mat loadAndShowOrExit(File file, Integer flags) {
Mat image = loadOrExit(file, flags);
show(image, file.getName());
return image;
}
/**
* Load an image. If image cannot be loaded the application will exit with code 1.
*
* @return loaded image
*/
public static Mat loadOrExit(File file) {
return loadOrExit(file, IMREAD_COLOR);
}
/**
* Load an image. If image cannot be loaded the application will exit with code 1.
*
* @param flags Flags specifying the color type of a loaded image:
* <ul>
* <li> `>0` Return a 3-channel color image</li>
* <li> `=0` Return a gray scale image</li>
* <li> `<0` Return the loaded image as is. Note that in the current implementation
* the alpha channel, if any, is stripped from the output image. For example, a 4-channel
* RGBA image is loaded as RGB if the `flags` is greater than 0.</li>
* </ul>
* Default is gray scale.
* @return loaded image
*/
public static Mat loadOrExit(File file, Integer flags) {
Mat image = imread(file.getAbsolutePath(), flags);
if (image.empty()) {
System.out.println("Couldn't load image: " + file.getAbsolutePath());
System.exit(1);
}
return image;
}
/**
* Show image in a window. Closing the window will exit the application.
*/
public static void show(Mat mat, String title) {
BufferedImage image = toBufferedImage(mat);
show(image, title);
}
/**
* Show image in a window. Closing the window will exit the application.
*/
public static void show(BufferedImage image, String title) {
CanvasFrame canvas = new CanvasFrame(title, 1);
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.showImage(image);
}
public static BufferedImage toBufferedImage(Mat mat) {
// Make sure that FrameConverters and JavaCV Frame are properly closed
try (OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat()) {
try (Frame frame = converter.convert(mat)) {
try (Java2DFrameConverter java2DConverter = new Java2DFrameConverter()) {
return java2DConverter.convert(frame);
}
}
}
}
/**
* Save the image to the specified file.
* <p>
* The image format is chosen based on the filename extension (see `imread()` in OpenCV documentation for the list of extensions).
* Only 8-bit (or 16-bit in case of PNG, JPEG 2000, and TIFF) single-channel or
* 3-channel (with 'BGR' channel order) images can be saved using this function.
* If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving.
*
* @param file file to save to. File name extension decides output image format.
* @param image image to save.
*/
public void save(File file, Mat image) {
imwrite(file.getAbsolutePath(), image);
}
/**
* Convert a Mat to ScalaFX Image
*/
public static javafx.scene.image.Image toFXImage(Mat mat) {
try(OpenCVFrameConverter.ToMat openCVConverter = new OpenCVFrameConverter.ToMat()) {
try(Frame frame = openCVConverter.convert(mat)){
try(JavaFXFrameConverter javaFXConverter = new JavaFXFrameConverter()) {
return javaFXConverter.convert(frame);
}
}
}
}
}