JavaCV中openCV的拉普拉斯滤波器处理

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);
                }
            }
        }
    }
}
相关推荐
AI军哥12 分钟前
MySQL8的安装方法
人工智能·mysql·yolo·机器学习·deepseek
余弦的倒数26 分钟前
知识蒸馏和迁移学习的区别
人工智能·机器学习·迁移学习
Allen Bright26 分钟前
【机器学习-线性回归-2】理解线性回归中的连续值与离散值
人工智能·机器学习·线性回归
青松@FasterAI1 小时前
【程序员 NLP 入门】词嵌入 - 上下文中的窗口大小是什么意思? (★小白必会版★)
人工智能·自然语言处理
AIGC大时代1 小时前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
硅谷秋水1 小时前
GAIA-2:用于自动驾驶的可控多视图生成世界模型
人工智能·机器学习·自动驾驶
偶尔微微一笑2 小时前
AI网络渗透kali应用(gptshell)
linux·人工智能·python·自然语言处理·编辑器
深度之眼2 小时前
2025时间序列都有哪些创新点可做——总结篇
人工智能·深度学习·机器学习·时间序列
晓数2 小时前
【硬核干货】JetBrains AI Assistant 干货笔记
人工智能·笔记·jetbrains·ai assistant
jndingxin2 小时前
OpenCV 图形API(60)颜色空间转换-----将图像从 YUV 色彩空间转换为 RGB 色彩空间函数YUV2RGB()
人工智能·opencv·计算机视觉