Java?OpenCV圖像處理之自定義圖像濾波算子
更新時間:2022年02月19日 09:13:08 作者:深色風(fēng)信子
這篇文章主要為大家介紹了如何利用Java?OpenCV實現(xiàn)自定義圖像濾波(降噪)?算子,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
示例代碼
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圖像處理之自定義圖像濾波算子的詳細(xì)內(nèi)容,更多關(guān)于Java OpenCV圖像濾波算子的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring 整合Shiro 并擴展使用EL表達(dá)式的實例詳解
Shiro是一個輕量級的權(quán)限控制框架,應(yīng)用非常廣泛。本文的重點是介紹Spring整合Shiro,并通過擴展使用Spring的EL表達(dá)式。需要的朋友可以參考下2018-03-03
SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因
spring-boot-maven-plugin是spring boot提供的maven打包插件,本文主要介紹了SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因,具有一定的參考價值,感興趣的可以了解一下2024-03-03
java實現(xiàn)刪除某條信息并刷新當(dāng)前頁操作
這篇文章主要介紹了java實現(xiàn)刪除某條信息并刷新當(dāng)前頁操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
SpringBoot部署到Linux讀取resources下的文件及遇到的坑
本文主要給大家介紹SpringBoot部署到Linux讀取resources下的文件,在平時業(yè)務(wù)開發(fā)過程中,很多朋友在獲取到文件內(nèi)容亂碼或者文件讀取不到的問題,今天給大家分享小編遇到的坑及處理方案,感興趣的朋友跟隨小編一起看看吧2021-06-06

