OpenCV Scharr操作

Scharr也用於檢測水平和垂直方向的圖像的二階導數。可以使用scharr()方法對圖像執行scharr操作。以下是這種方法的語法 -

Scharr(src, dst, ddepth, dx, dy)

該方法接受以下參數 -

  • src - 表示源(輸入)圖像的Mat類的對象。
  • dst - 表示目標(輸出)圖像的Mat類的對象。
  • ddepth - 表示圖像深度的整數變量(-1)。
  • dx - 表示x導數的整數變量(01)。
  • dy - 表示y導數的整數變量(01)。

示例

以下程序演示瞭如何將scharr應用於給定的圖像。

package com.yiibai.sobelderivatives;

import org.opencv.core.Core;
import org.opencv.core.Mat;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ScharrTest {

   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/sample3.jpg";
      Mat src = Imgcodecs.imread(file);

      // Creating an empty matrix to store the result
      Mat dst = new Mat();

      // Applying Box Filter effect on the Image
      Imgproc.Scharr(src, dst, Imgproc.CV_SCHARR, 0, 1);

      // Writing the image
      Imgcodecs.imwrite("F:/worksp/opencv/images/sample3scharr_output.jpg", dst);

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

假定以下是上述程序中指定的輸入圖像sample3.jpg
OpenCV

執行上面示例代碼,得到以下結果 -
OpenCV

更多scharr變體

將不同的值傳遞給最後一個參數(dxdy),在01之間,將得到不同的輸出。

// Applying scharr on the Image
Imgproc.Scharr(src, dst, -1, 1, 1);