如何使用 Java OpenCV 對兩張影像執行按位 OR 運算?


您可以使用 org.opencv.core.Core 類中的 bitwise_or() 方法在兩張影像間計算按位或操作。

該方法接受三個表示源矩陣、目標矩陣和結果矩陣的 Mat 物件,計算源矩陣中每個元素的按位異或,並將結果儲存在目標矩陣中。

示例

在以下 Java 示例中,我們將影像轉換為二進位制和灰度,並計算二者的按位異或。

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class BitwiseORExample {
   public static void main(String args[]) throws Exception {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading the Image
      String file ="D://images//elephant.jpg";
      Mat src = Imgcodecs.imread(file, Imgcodecs.IMREAD_GRAYSCALE );
      HighGui.imshow("Grayscale Image", src);
      //Creating an empty matrix to store the results
      Mat dst = new Mat(src.rows(), src.cols(), src.type());
      Mat threshold = new Mat(src.rows(), src.cols(), src.type());
      //Converting the gray scale image to binary image
      Imgproc.threshold(src, threshold, 100, 255, Imgproc.THRESH_BINARY_INV);
      HighGui.imshow("Binary Image", threshold);
      //Applying bitwise Or operation
      Core.bitwise_or(src, threshold, dst);
      HighGui.imshow("Bitwise OR operation", dst);
      HighGui.waitKey();
   }
}

輸入影像

輸出

執行後,以上程式生成以下視窗 −

灰度影像 −

二進位制影像 −

按位或 −

更新於: 2020-04-08

276 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.