Java OpenCV图像处理之自定义图像滤波算子
程序员文章站
2022-10-29 14:37:29
目录示例代码效果图示例代码package com.xu.image;import java.io.file;import org.opencv.core.core;import org.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图像处理之自定义图像滤波算子的详细内容,更多关于java opencv图像滤波算子的资料请关注其它相关文章!