ภาพรวม
คุณสามารถคำนวณการผนวกแบบบิตเวส (bitwise conjunction) ระหว่างสองภาพได้โดยใช้เมธอด bitwise_and() ของคลาส org.opencv.core.Core
เมธอดนี้รับออบเจ็กต์ 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 BitwiseAndExample {
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 AND
Core.bitwise_and(src, threshold, dst);
HighGui.imshow("Bitwise And operation", dst);
HighGui.waitKey();
}
}
ภาพเข้า

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

ภาพไบนารี (Binary Image)

Bitwise AND
