Opencv-canny-edge-detection

提供:Dev Guides
移動先:案内検索

OpenCV-キャニーエッジ検出

Canny Edge Detectionは、画像のエッジを検出するために使用されます。 入力としてグレースケール画像を受け入れ、多段アルゴリズムを使用します。

*imgproc* クラスの* Canny()*メソッドを使用して、この操作を画像で実行できます。このメソッドの構文は次のとおりです。
Canny(image, edges, threshold1, threshold2)

このメソッドは、次のパラメータを受け入れます-

  • image -この操作のソース(入力画像)を表す Mat オブジェクト。
  • edges -この操作の宛先(エッジ)を表す Mat オブジェクト。
  • threshold1 -ヒステリシス手順の最初のしきい値を表すdouble型の変数。
  • threshold2 -ヒステリシス手順の2番目のしきい値を表すdouble型の変数。

次のプログラムは、特定の画像でCanny Edge Detection操作を実行する方法を示す例です。

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

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

public class CannyEdgeDetection {
   public static void main(String args[]) throws Exception {
     //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/chap17/canny_input.jpg";

     //Reading the image
      Mat src = Imgcodecs.imread(file);

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

     //Converting the image from color to Gray
      Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
      Mat edges = new Mat();

     //Detecting the edges
      Imgproc.Canny(gray, edges, 60, 60*3);

     //Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap17/canny_output.jpg", edges);
      System.out.println("Image Loaded");
   }
}

上記のプログラムで指定された入力画像 canny_input.jpg が次のものであると仮定します。

キャニー入力

出力

上記のプログラムを実行すると、次の出力が得られます-

Image Processed

指定したパスを開くと、次のように出力画像を観察できます-

キャニー出力