如何使用 Java OpenCV 對兩幅影像進行按位異或運算?
你可以使用org.opencv.core.Core 類的bitwise_xor()方法計算兩幅影像的按位異或。
此方法接受三個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 BitwiseXORExample {
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());
Mat gray = 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_xor(src, threshold, dst);
HighGui.imshow("Bitwise XOR operation", dst);
HighGui.waitKey();
}
}輸入影像

輸出
執行後,上述程式生成以下視窗 -
灰度影像 -

二進位制影像 -

按位異或 -

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP