首頁 > 軟體

Java OpenCV影象處理之自定義影象濾波運算元

2022-02-19 10:00:10

範例程式碼

package com.xu.image;

import java.io.File;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

/**
 * @Title: Image.java
 * @Description: OpenCV 測試檔案
 * @Package com.xu.test
 * @author: hyacinth
 * @date: 2019年5月7日12:13:13
 * @version: V-1.0.0
 * @Copyright: 2019 hyacinth
 */
public class Image {

    static {
        String os = System.getProperty("os.name");
        String type = System.getProperty("sun.arch.data.model");
        if (os.toUpperCase().contains("WINDOWS")) {
            File lib;
            if (type.endsWith("64")) {
                lib = new File("D:\Learn\OpenCV\OpenCV-4.5.5\build\java\x64\" + System.mapLibraryName("opencv_java455"));
            } else {
                lib = new File("D:\Learn\OpenCV\OpenCV-4.5.5\build\java\x86\" + System.mapLibraryName("opencv_java455"));
            }
            System.load(lib.getAbsolutePath());
        }
    }

    public static void main(String[] args) {
        kernel3();
    }

    /**
     * OpenCV-4.0.0 自定義濾波(降噪)(Robert運算元)
     *
     * @return: void
     * @date: 2019年5月7日12:16:55
     */
    public static void kernel1() {
        Mat src = Imgcodecs.imread("D:\OneDrive\桌面\1.jpg");
        HighGui.imshow("Robert運算元 原圖", src.clone());
        Mat dst_x = new Mat();
        Mat dst_y = new Mat();

        //Robert運算元-X軸
        Mat kernel_x = new Mat(2, 2, 1);
        kernel_x.put(0, 0, 1);
        kernel_x.put(0, 1, 0);
        kernel_x.put(1, 0, 0);
        kernel_x.put(1, 1, -1);
        Imgproc.filter2D(src, dst_x, -1, kernel_x, new Point(-1, -1), 0.0);

        //Robert運算元-Y軸
        Mat kernel_y = new Mat(2, 2, 1);
        kernel_y.put(0, 0, 0);
        kernel_y.put(0, 1, 1);
        kernel_y.put(1, 0, -1);
        kernel_y.put(1, 1, 0);
        Imgproc.filter2D(src, dst_y, -1, kernel_y, new Point(-1, -1), 0.0);

        HighGui.imshow("Robert運算元 Y", dst_y);
        HighGui.imshow("Robert運算元 X", dst_x);
        Mat dst = new Mat();
        Core.addWeighted(dst_x, 0.5, dst_y, 0.5, 0, dst);
        HighGui.imshow("Robert運算元 融合", dst);
        HighGui.waitKey(10);
    }

    /**
     * OpenCV-4.0.0 自定義濾波(降噪)(Sable運算元)
     *
     * @return: void
     * @date: 2019年5月7日12:16:55
     */
    public static void kernel2() {
        Mat src = Imgcodecs.imread("D:\OneDrive\桌面\1.jpg");
        HighGui.imshow("Sable運算元 原圖", src.clone());
        Mat dst_x = new Mat();
        Mat dst_y = new Mat();

        //Soble運算元-X軸
        Mat kernel_x = new Mat(3, 3, 1);
        kernel_x.put(0, 0, -1);
        kernel_x.put(0, 1, 0);
        kernel_x.put(0, 2, 1);
        kernel_x.put(1, 0, -2);
        kernel_x.put(1, 1, 0);
        kernel_x.put(1, 2, 2);
        kernel_x.put(2, 0, -1);
        kernel_x.put(2, 1, 0);
        kernel_x.put(2, 2, 1);
        Imgproc.filter2D(src, dst_x, -1, kernel_x, new Point(-1, -1), 0.0);

        //Soble運算元-Y軸
        Mat kernel_y = new Mat(3, 3, 1);
        kernel_y.put(0, 0, -1);
        kernel_y.put(0, 1, 2);
        kernel_y.put(0, 2, -1);
        kernel_y.put(1, 0, 0);
        kernel_y.put(1, 1, 0);
        kernel_y.put(1, 2, 0);
        kernel_y.put(2, 0, 1);
        kernel_y.put(2, 1, 2);
        kernel_y.put(2, 2, 1);
        Imgproc.filter2D(src, dst_y, -1, kernel_y, new Point(-1, -1), 0.0);

        HighGui.imshow("Sable運算元 X", dst_x);
        HighGui.imshow("Sable運算元 Y", dst_y);
        Mat dst = new Mat();
        Core.addWeighted(dst_x, 0.5, dst_y, 0.5, 0, dst);
        HighGui.imshow("Sable運算元 融合", dst);
        HighGui.waitKey(1);
    }

    /**
     * OpenCV-4.0.0 自定義濾波(降噪)(Laplace運算元)
     *
     * @return: void
     * @date: 2019年5月7日12:16:55
     */
    public static void kernel3() {
        Mat src = Imgcodecs.imread("D:\OneDrive\桌面\1.jpg");
        HighGui.imshow("Laplace 運算元 原圖", src.clone());
        Mat dst = new Mat();

        //拉普拉斯運算元
        Mat kernel = new Mat(3, 3, 1);
        kernel.put(0, 0, 0);
        kernel.put(0, 1, -1);
        kernel.put(0, 2, 0);
        kernel.put(1, 0, -1);
        kernel.put(1, 1, 4);
        kernel.put(1, 2, -1);
        kernel.put(2, 0, 0);
        kernel.put(2, 1, -1);
        kernel.put(2, 2, 0);
        Imgproc.filter2D(src, dst, -1, kernel, new Point(-1, -1), 0.0);

        HighGui.imshow("Laplace 運算元", dst);
        HighGui.waitKey(0);
    }

}

效果圖

以上就是Java OpenCV影象處理之自定義影象濾波運算元的詳細內容,更多關於Java OpenCV影象濾波運算元的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com