如何使用 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