OpenCV - 彩色影像轉灰度



在前面的章節中,我們討論瞭如何以不同的型別(二進位制、灰度、BGR 等)讀取輸入影像。在本節中,我們將學習如何將一種型別的影像轉換為另一種型別。

包 **org.opencv.imgproc** 中名為 **Imgproc** 的類提供了將影像從一種顏色轉換為另一種顏色方法。

將彩色影像轉換為灰度

名為 **cvtColor()** 的方法用於將彩色影像轉換為灰度。以下是此方法的語法。

cvtColor(Mat src, Mat dst, int code)

此方法接受以下引數:

  • **src** - 表示源的矩陣。

  • **dst** - 表示目標的矩陣。

  • **code** - 表示轉換型別的整數程式碼,例如,RGB 到灰度。

可以透過將程式碼 **Imgproc.COLOR_RGB2GRAY** 以及源和目標矩陣作為引數傳遞給 **cvtColor()** 方法,將彩色影像轉換為灰度。

示例

以下程式演示瞭如何將彩色影像讀取為灰度影像並使用 JavaFX 視窗顯示它。

import java.awt.image.BufferedImage;
  
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;

import javafx.stage.Stage;

public class ColorToGrayscale extends Application {
   @Override
   public void start(Stage stage) throws Exception {
      WritableImage writableImage = loadAndConvert();
       
      // Setting the image view
      ImageView imageView = new ImageView(writableImage);

      // Setting the position of the image
      imageView.setX(10);
      imageView.setY(10);

      // setting the fit height and width of the image view
      imageView.setFitHeight(400);
      imageView.setFitWidth(600);

      // Setting the preserve ratio of the image view
      imageView.setPreserveRatio(true);

      // Creating a Group object
      Group root = new Group(imageView);

      // Creating a scene object
      Scene scene = new Scene(root, 600, 400);

      // Setting title to the Stage
      stage.setTitle("Colored to grayscale image");

      // Adding scene to the stage
      stage.setScene(scene);

      // Displaying the contents of the stage
      stage.show();
   } 
   public WritableImage loadAndConvert() throws Exception {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      String input = "C:/EXAMPLES/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 sacle and saving it in the dst matrix
      Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY);
      
      //Extracting data from the transformed image (dst)
      byte[] data1 = new byte[dst.rows() * dst.cols() * (int)(dst.elemSize())];
      dst.get(0, 0, data1);

      //Creating Buffered image using the data
      BufferedImage bufImage = new BufferedImage(dst.cols(),dst.rows(), 
         BufferedImage.TYPE_BYTE_GRAY);

      //Setting the data elements to the image
      bufImage.getRaster().setDataElements(0, 0, dst.cols(), dst.rows(), data1);

      //Creating a WritableImage
      WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
      System.out.println("Converted to Grayscale");
      return writableImage;
   }
   public static void main(String args[]) throws Exception {
      launch(args);
   }
}

輸入影像

假設以上程式中指定的輸入影像是 **sample.jpg** 。

Sample Image

輸出影像

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

Colored Images to GrayScale Output
廣告