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);
                }
            }
        }
    }
}
相关推荐
Niuguangshuo6 分钟前
深度学习模型中音频流式处理
人工智能·深度学习·音视频
带带老表学爬虫18 分钟前
opencv阈值操作
人工智能·opencv·计算机视觉
dundunmm36 分钟前
论文阅读:Statistical Comparisons of Classifiers over Multiple Data Sets
论文阅读·人工智能·算法·机器学习·评估方法
WBingJ1 小时前
李宏毅机器学习深度学习:机器学习任务攻略
人工智能·深度学习·机器学习
**之火1 小时前
(五)机器学习 - 数据分布
人工智能·机器学习
martian6651 小时前
人工智能机器学习基本概念详解
人工智能·机器学习
嘟嘟实验室1 小时前
PDFMathTranslate,PDF多语言翻译,批量处理,学术论文,双语对照(WIN/MAC)
人工智能·macos·aigc·自动翻译
远洋录1 小时前
大型前端应用状态管理实战:从 Redux 到 React Query 的演进之路
前端·人工智能·react
coldstarry1 小时前
sheng的学习笔记-AI-自然语言处理(NLP),机器翻译,情感分类,词嵌入
人工智能·深度学习·自然语言处理·机器翻译
高山莫衣1 小时前
【返璞归真】-Lasso 回归(Least Absolute Shrinkage and Selection Operator,最小绝对值收缩和选择算子)
人工智能·数据挖掘·回归