如何使用 Java OpenCV 对两张图像执行按位 OR 运算?
您可以使用 org.opencv.core.Core 类中的 bitwise_or() 方法在两张图像间计算按位或操作。
该方法接受三个表示源矩阵、目标矩阵和结果矩阵的 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 BitwiseORExample {
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());
//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_or(src, threshold, dst);
HighGui.imshow("Bitwise OR operation", dst);
HighGui.waitKey();
}
}输入图像

输出
执行后,以上程序生成以下窗口 −
灰度图像 −

二进制图像 −

按位或 −

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP