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

วิธีทำ Bitwise Not Operation บนภาพด้วย Java OpenCV

คุณสามารถคำนวณ bitwise conjunction ระหว่างสองภาพได้ด้วยเมธอด bitwise_not() ของคลาส org.opencv.core.Core

เมธอดนี้รับอ็อบเจ็กต์ Mat สองตัว แทน source และ destination matrices คำนวณ inverse ของแต่ละ element ใน source matrix และเก็บผลลัพธ์ใน destination matrix

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

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;

public class BitwiseNOTExample {
    public static void main(String[] args) throws Exception {
        // โหลด OpenCV core library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        
        // อ่านภาพ
        String file = "D://images//elephant.jpg";
        Mat src = Imgcodecs.imread(file);
        HighGui.imshow("Grayscale Image", src);
        
        // สร้าง matrix ว่างสำหรับผลลัพธ์
        Mat dst = new Mat(src.rows(), src.cols(), src.type());
        
        // ใช้ bitwise not operation
        Core.bitwise_not(src, dst);
        HighGui.imshow("Bitwise NOT operation", dst);
        HighGui.waitKey();
    }
}

รูปภาพ Input

วิธีทำ Bitwise Not Operation บนภาพด้วย Java OpenCV

ผลลัพธ์

เมื่อรันโปรแกรมจะได้หน้าต่างดังนี้:

วิธีทำ Bitwise Not Operation บนภาพด้วย Java OpenCV