Get the App
SLTechnology News&Howtos  ›  Development  › 

How to customize the Image filter operator in Java OpenCV

Shulou Source: shulou.com Published: 2022-06-01 13:45:23 10月03日 Update

Today, I would like to share with you how to customize the image filter operator in Java OpenCV, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

Sample code 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 Test File * @ Package com.xu.test * @ author: hyacinth * @ date: 12:13:13 * @ version: Vmur1.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 Custom filter (noise reduction) (Robert operator) * * @ return: void * @ date: 12:16:55 * / public static void kernel1 () {Mat src = Imgcodecs.imread ("D:\\ OneDrive\\ Desktop\\ 1.jpg"); HighGui.imshow ("original Robert operator", src.clone ()) Mat dst_x = new Mat (); Mat dst_y = new Mat (); / / Robert operator-X axis 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 operator-Y axis 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 operator Y", dst_y); HighGui.imshow ("Robert operator X", dst_x); Mat dst = new Mat () Core.addWeighted (dst_x, 0.5, dst_y, 0.5,0, dst); HighGui.imshow ("Robert operator Fusion", dst); HighGui.waitKey (10) } / * OpenCV-4.0.0 Custom filter (noise reduction) (Sable operator) * * @ return: void * @ date: 12:16:55 * / public static void kernel2 () {Mat src = Imgcodecs.imread ("D:\\ OneDrive\\ Desktop\\ 1.jpg"); HighGui.imshow ("original Sable operator", src.clone ()) Mat dst_x = new Mat (); Mat dst_y = new Mat (); / / Soble operator-X axis 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 operator-Y axis 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 operator X", dst_x) HighGui.imshow ("Sable operator Y", dst_y); Mat dst = new Mat (); Core.addWeighted (dst_x, 0.5, dst_y, 0.5,0, dst); HighGui.imshow ("Sable operator Fusion", dst); HighGui.waitKey (1) } / * OpenCV-4.0.0 Custom filter (noise reduction) (Laplace operator) * * @ return: void * @ date: 12:16:55 * / public static void kernel3 () {Mat src = Imgcodecs.imread ("D:\\ OneDrive\\ Desktop\\ 1.jpg"); HighGui.imshow ("original Laplace operator", src.clone ()) Mat dst = new Mat (); / / Laplace operator Mat kernel = new Mat (3,3,1); kernel.put (0,0,0); kernel.put (0,1,0); 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 operator", dst); HighGui.waitKey (0) }} effect picture

These are all the contents of the article "how to customize the Image filter operator in Java OpenCV". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Operators knowledge articles original images desktops images content different large code most that is Laplace effects effects files more knowledge points examples industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno NVidia MySQL Xiaomi