OpenCV - 影像金字塔



金字塔是對影像的一種操作,其中:

  • 輸入影像首先使用特定的平滑濾波器(例如:高斯濾波器、拉普拉斯濾波器)進行平滑處理,然後對平滑後的影像進行子取樣。

  • 此過程重複多次。

在金字塔操作過程中,影像的平滑度增加,而解析度(大小)減小。

向上金字塔

在向上金字塔中,影像首先進行上取樣,然後進行模糊處理。您可以使用`imgproc`類的`pyrUP()`方法對影像執行向上金字塔操作。以下是此方法的語法:

pyrUp(src, dst, dstsize, borderType)

此方法接受以下引數:

  • src - `Mat`類的一個物件,表示源(輸入)影像。

  • dst - `Mat`類的一個物件,表示目標(輸出)影像。

  • size - `Size`類的一個物件,表示影像要增加或減小的尺寸。

  • borderType - 整型變數,表示要使用的邊界型別。

示例

以下程式演示瞭如何在影像上執行向上金字塔操作。

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 ="E:/OpenCV/chap13/pyramid_input.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("E:/OpenCV/chap13/pyrUp_output.jpg", dst);

      System.out.println("Image Processed");
   }
}

假設上述程式中指定了以下輸入影像`pyramid_input.jpg`。

Pyramid Input

輸出

執行程式後,您將獲得以下輸出:

Image Processed

如果開啟指定的路徑,您可以觀察到輸出影像如下:

Pyramid Up Output

向下金字塔

在向下金字塔中,影像首先進行模糊處理,然後進行下采樣。您可以使用`imgproc`類的`pyrDown()`方法對影像執行向下金字塔操作。以下是此方法的語法:

pyrDown(src, dst, dstsize, borderType)

此方法接受以下引數:

  • src - `Mat`類的一個物件,表示源(輸入)影像。

  • dst - `Mat`類的一個物件,表示目標(輸出)影像。

  • size - `Size`類的一個物件,表示影像要增加或減小的尺寸。

  • borderType - 整型變數,表示要使用的邊界型別。

示例

以下程式演示瞭如何在影像上執行向下金字塔操作。

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 ="E:/OpenCV/chap13/pyramid_input.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("E:/OpenCV/chap13/pyrDown_output.jpg", dst);

      System.out.println("Image Processed");
   } 
}

假設上述程式中指定了以下輸入影像`pyramid_input.jpg`。

Pyramid Input

輸出

執行程式後,您將獲得以下輸出:

Image Processed

如果開啟指定的路徑,您可以觀察到輸出影像如下:

Pyramid Down Output

均值漂移濾波

在均值漂移金字塔操作中,首先對影像進行均值漂移分割。

您可以使用`imgproc`類的`pyrMeanShiftFiltering()`方法對影像執行均值漂移濾波操作。以下是此方法的語法。

pyrMeanShiftFiltering(src, dst, sp, sr)

此方法接受以下引數:

  • src - `Mat`類的一個物件,表示源(輸入)影像。

  • dst - `Mat`類的一個物件,表示目標(輸出)影像。

  • sp - 雙精度型變數,表示空間視窗半徑。

  • sr - 雙精度型變數,表示顏色視窗半徑。

示例

以下程式演示瞭如何在給定影像上執行均值漂移濾波操作。

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 ="E:/OpenCV/chap13/pyramid_input.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("E:/OpenCV/chap13/meanShift_output.jpg", dst);
      
      System.out.println("Image Processed");
   } 
}

假設上述程式中指定了以下輸入影像`pyramid_input.jpg`。

Pyramid Input

輸出

執行程式後,您將獲得以下輸出:

Image Processed

如果開啟指定的路徑,您可以觀察到輸出影像如下:

Mean Shift Filtering Output
廣告
© . All rights reserved.