中位数模糊操作类似于其他平均方法。 这里,图像的中心元素被内核区域中所有像素的中值所代替。该操作在消除噪音的同时处理边缘。
可以使用imgproc
类的medianBlur()
方法在图像上执行此操作。 以下是这种方法的语法 -
medianBlur(src, dst, ksize)
该方法接受以下参数 -
Mat
对象。Mat
对象。Size
对象。以下程序演示如何对图像执行中位模糊操作。
package com.zyiz.blur; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class MedianBlurTest { public static void main(String args[]) { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Reading the Image from the file and storing it in to a Matrix object String file ="F:/worksp/opencv/images/sample2.jpg"; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Applying MedianBlur on the Image Imgproc.medianBlur(src, dst, 15); // Writing the image Imgcodecs.imwrite("F:/worksp/opencv/images/sample2median.jpg", dst); System.out.println("Image Processed"); } }
假定以下是上述程序中指定的输入图像sample2.jpg
。
执行上面示例代码,得到以下结果 -