金字塔是对图像的一种操作,
在金字塔操作期间,图像的平滑度增加并且分辨率(尺寸)减小。
在金字塔上,图像最初被上采样然后模糊。可以使用imgproc
类的pyrUP()
方法对图像执行金字塔向上操作。 以下是这种方法的语法 -
pyrUp(src, dst, dstsize, borderType)
该方法接受以下参数 -
Mat
对象。Mat
的对象。Size
类的对象,表示图像增加或减少的大小。以下程序演示了如何在图像上执行Pyramid Up操作。
package com.zyiz.filtering; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class PyramidUp { 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 pyrUp on the Image Imgproc.pyrUp(src, dst, new Size(src.cols()*2, src.rows()*2), Core.BORDER_DEFAULT); // Writing the image Imgcodecs.imwrite("F:/worksp/opencv/images/sample2pyrUp_output.jpg", dst); System.out.println("Image Processed"); } }
假定以下是上述程序中指定的输入图像sample2.jpg
。
执行上面示例代码,得到以下结果 -
在金字塔向下,图像最初是模糊的,然后向下采样。可以使用imgproc
类的pyrDown()
方法对图像执行金字塔向下操作。 以下是这种方法的语法 -
pyrDown(src, dst, dstsize, borderType)
该方法接受以下参数 -
Mat
对象。Mat
的对象。Size
类的对象,表示图像增加或减少的大小。下面的程序演示如何在图像上执行Pyramid Down操作。
package com.zyiz.filtering; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class PyramidDown { 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 pyrDown on the Image Imgproc.pyrDown(src, dst, new Size(src.cols() / 2, src.rows() / 2), Core.BORDER_DEFAULT); // Writing the image Imgcodecs.imwrite("F:/worksp/opencv/images/sample2pyrDown_output.jpg", dst); System.out.println("Image Processed"); } }
假定以下是上述程序中指定的输入图像sample2.jpg
。
执行上面示例代码,得到以下结果 -
在均值偏移金字塔操作中,执行图像的均值偏移分割的初始步骤。
可以使用imgproc
类的pyrDown()
方法对图像执行金字塔均值移位滤镜操作。以下是此方法的语法。
pyrMeanShiftFiltering(src, dst, sp, sr)
该方法接受以下参数 -
Mat
类的对象。Mat
类的对象。double
的空间窗口半径变量。double
的变量,表示颜色窗口半径。示例
以下程序演示如何在给定图像上执行Mean Shift Filtering操作。
package com.zyiz.filtering; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class PyramidMeanShift { 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 meanShifting on the Image Imgproc.pyrMeanShiftFiltering(src, dst, 200, 300); // Writing the image Imgcodecs.imwrite("F:/worksp/opencv/images/sample2meanShift_output.jpg", dst); System.out.println("Image Processed"); } }
假定以下是上述程序中指定的输入图像sample2.jpg
。
执行上面示例代码,得到以下结果 -