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);
                }
            }
        }
    }
}
相关推荐
大鱼>2 分钟前
NLP 基础:文本预处理/词向量/文本分类
人工智能·深度学习·集成学习·boosting
武子康3 分钟前
调查研究-215 Anthropic 双线扩张:从 Claude 模型公司到 AI 工业栈
人工智能·openai·claude
KaMeidebaby5 分钟前
卡梅德生物技术快报|实操手册:CXCL4 蛋白原核表达全套工艺,两步层析去除蛋白多聚体附完整电泳数据
人工智能·算法·机器学习·架构·spark
nap-joker8 分钟前
具备多项先验知识的图神经网络用于多组学数据分析
人工智能·深度学习·神经网络·图神经网络·多组学数据·生物先验·ppi蛋白质-蛋白质互作信息
AIGCmagic社区12 分钟前
Unlimited OCR 论文精读:R-SWA 如何实现一次性长文档解析
人工智能·算法·aigc
满怀冰雪12 分钟前
24_中间件系统源码分析_Middleware链的洋葱模型与异常处理
人工智能·python·中间件·langchain
呆呆敲代码的小Y17 分钟前
AI Agent 实战:last30days-skill-cn 一键搜索中国 8 大平台,30 秒生成深度研究报告
人工智能
AI行业学习20 分钟前
2026 版 Notepad++ 完整图文安装指南|官方渠道无捆绑,一键切换中文界面
开发语言·人工智能·python·html·notepad++
aneasystone本尊20 分钟前
OpenMontage 快速入门
人工智能
诚信定制83921 分钟前
2025年CSDN年度技术趋势预测:AI、云原生与开发者工具的未来
人工智能·云原生