Java?OpenCV圖像處理之自定義圖像濾波算子
示例代碼
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圖像濾波算子的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java實(shí)現(xiàn)dijkstra最短路徑尋路算法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)dijkstra最短路徑尋路算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Spring 整合Shiro 并擴(kuò)展使用EL表達(dá)式的實(shí)例詳解
Shiro是一個(gè)輕量級(jí)的權(quán)限控制框架,應(yīng)用非常廣泛。本文的重點(diǎn)是介紹Spring整合Shiro,并通過擴(kuò)展使用Spring的EL表達(dá)式。需要的朋友可以參考下2018-03-03Spring Bean實(shí)例化實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Bean實(shí)例化實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Java語言的11大特點(diǎn)(Java初學(xué)者必知)
Java是一種簡單的,面向?qū)ο蟮?,分布式的,解釋型的,健壯安全的,結(jié)構(gòu)中立的,可移植的,性能優(yōu)異、多線程的靜態(tài)語言。這篇文章主要介紹了Java語言的11大特點(diǎn),需要的朋友可以參考下2020-07-07SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因
spring-boot-maven-plugin是spring boot提供的maven打包插件,本文主要介紹了SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03java實(shí)現(xiàn)刪除某條信息并刷新當(dāng)前頁操作
這篇文章主要介紹了java實(shí)現(xiàn)刪除某條信息并刷新當(dāng)前頁操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11SpringBoot部署到Linux讀取resources下的文件及遇到的坑
本文主要給大家介紹SpringBoot部署到Linux讀取resources下的文件,在平時(shí)業(yè)務(wù)開發(fā)過程中,很多朋友在獲取到文件內(nèi)容亂碼或者文件讀取不到的問題,今天給大家分享小編遇到的坑及處理方案,感興趣的朋友跟隨小編一起看看吧2021-06-06