คุณสามารถคำนวณการเลือกได้เพียงอย่างเดียว (bitwise exclusive or) ระหว่างสองภาพได้โดยใช้เมธอด bitwise_xor() ของคลาส org.opencv.core.Core
เมธอดนี้รับออบเจ็กต์ Mat สามตัว แทนเมทริกซ์ต้นทาง ปลายทาง และผลลัพธ์ คำนวณ bitwise exclusive or ของแต่ละองค์ประกอบในเมทริกซ์ต้นทาง และเก็บผลลัพธ์ไว้ในเมทริกซ์ปลายทาง
ตัวอย่างโค้ด
ในตัวอย่าง Java ต่อไปนี้ เราแปลงภาพเป็นไบนารีและเกรสเกล และคำนวณ bitwise exclusive or ของผลลัพธ์
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 {
// โหลดไลบรารีหลักของ OpenCV
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// อ่านภาพ
String file = "D://images//elephant.jpg";
Mat src = Imgcodecs.imread(file, Imgcodecs.IMREAD_GRAYSCALE);
HighGui.imshow("Grayscale Image", src);
// สร้างเมทริกซ์ว่างเพื่อเก็บผลลัพธ์
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());
// แปลงภาพเกรสเกลเป็นภาพไบนารี
Imgproc.threshold(src, threshold, 100, 255, Imgproc.THRESH_BINARY_INV);
HighGui.imshow("Binary Image", threshold);
// ใช้งาน Bitwise XOR
Core.bitwise_xor(src, threshold, dst);
HighGui.imshow("Bitwise XOR operation", dst);
HighGui.waitKey();
}
}
ภาพเข้า

ผลลัพธ์
เมื่อรันโปรแกรม จะแสดงหน้าต่างดังต่อไปนี้:
Gray Scale Image

Binary Image

Bitwise Xor
