Javaexamples-convert-image-into-grayscale

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

画像をグレースケールに変換

問題の説明

Javaを使用して画像をグレースケールに変換する方法。

溶液

以下は、Javaを使用して画像をグレースケールに変換するプログラムです。

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

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

public class ConvertingImageToGrayScale {
   public static void main(String args[]) throws Exception {

     //Loading the OpenCV core library
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME );
      String input = "C:/opencv/sample.jpg";

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

     //Creating the empty destination matrix
      Mat dst = new Mat();

     //Converting the image to gray scale and saving it in the dst matrix
      Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY);

     //Writing the image
      Imgcodecs.imwrite("C:/opencv/imageToGrayScale.jpg", dst);
      System.out.println("Converted to Grayscale");
   }
}

入力

画像変換

出力

グレースケール画像