如何在java opencv中执行联合操作

时间:2018-04-30 17:05:06

标签: java opencv image-processing bitwise-operators

我需要编写与此Java OpenCV代码

相同的C++代码
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);

Mat1b mask = mask1 | mask2;

当我尝试使用|运算符时,会导致错误。

Mat mask1 = new Mat();
Mat mask2 = new Mat();
Core.inRange(hsv, new Scalar(0, 70, 50), new Scalar(10, 255, 255), mask1);
Core.inRange(hsv, new Scalar(170, 70, 50), new Scalar(180, 255, 255), mask2);

Mat hsvThres = mask1 | mask2;

错误:The operator | is undefined for the argument type(s) org.opencv.core.Mat

2 个答案:

答案 0 :(得分:1)

Union运算符,logic-or (|)用于OpenCV的两个矩阵:

C ++中的代码:

inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);

Mat mask = mask1 | mask2; 

Java代码:

Mat mask1 = new Mat();
Mat mask2 = new Mat();
Core.inRange(hsv, new Scalar(0, 70, 50), new Scalar(10, 255, 255), mask1);
Core.inRange(hsv, new Scalar(170, 70, 50), new Scalar(180, 255, 255), mask2);

Mat mask= new Mat();
Core.bitwise_or(mask1, mask2, mask);

Python中的代码:

mask1 = cv2.inRange(hsv, (0, 70, 50), (10, 255, 255))
mask2 = cv2.inRange(hsv, (170, 70, 50), (180, 255, 255))
mask = cv2.bitwise_or(mask1, mask2)

答案 1 :(得分:0)

我认为你错误的是你试图比较两个Mat对象引用而不是它们下面的值。我对openCV并不是很熟悉,但我的猜测是你试图将它们下面的值组合在一起,所以你可以使用方法https://docs.opencv.org/java/2.4.2/org/opencv/core/Mat.html#nativeObj。哪一行将被重写为: Mat hsvThres = new Mat(mask1.nativeObj | mask2.nativeObj);