Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

วิธีทำ Bitwise OR บนภาพสองภาพด้วย Java OpenCV

เมธอด bitwise_or() ของคลาส org.opencv.core.Core คำนวณ bitwise disjunction (OR) ของภาพสองภาพ รับ 3 เมทริกซ์ Mat: src1, src2, dst

ตัวอย่างโค้ด

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 {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        
        // อ่านภาพเป็น Grayscale
        Mat src = Imgcodecs.imread("D://images//elephant.jpg", Imgcodecs.IMREAD_GRAYSCALE);
        HighGui.imshow("Grayscale", src);
        
        Mat dst = new Mat(src.rows(), src.cols(), src.type());
        Mat threshold = new Mat(src.rows(), src.cols(), src.type());
        
        // แปลงเป็น Binary Image (Threshold)
        Imgproc.threshold(src, threshold, 100, 255, Imgproc.THRESH_BINARY_INV);
        HighGui.imshow("Binary", threshold);
        
        // Bitwise OR
        Core.bitwise_or(src, threshold, dst);
        HighGui.imshow("Bitwise OR", dst);
        
        HighGui.waitKey();
    }
}

ผลลัพธ์

แสดง 3 หน้าต่าง: Grayscale, Binary (Threshold), และ Bitwise OR ระหว่างทั้งสอง