0

I need to write an equivalent Java OpenCV code as this C++ code

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;

When i tried to use the | operator it leads to an error.

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;

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

J.doe
  • 33
  • 6
  • Does [THIS](https://stackoverflow.com/questions/3590677/how-to-do-union-intersect-difference-and-reverse-data-in-java) help? – Jeru Luke Apr 30 '18 at 17:14
  • Unfortunately nope :( – J.doe Apr 30 '18 at 17:17
  • how about [THIS](https://stackoverflow.com/questions/5818057/union-of-2-arrays-in-java)? – Jeru Luke Apr 30 '18 at 17:55
  • The problem is that i am dealing with Matrices not Arrays, it won't work for Mat – J.doe Apr 30 '18 at 18:10
  • After a bit of searching I came across this documentation page: (https://docs.opencv.org/java/2.4.2/org/opencv/core/Core.html#bitwise_or(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat)) – Jeru Luke Apr 30 '18 at 18:31
  • 1
    Thank you for trying to help, i tried that from the documentation you provided `Core.bitwise_or(mask1, mask2, hsvThres)` but i don't know why it didn't provide the same results as the `C++` code, the result is completely far from the right one. – J.doe Apr 30 '18 at 18:45
  • there are two options using the same function. It is present on the same page. Glad I could help. – Jeru Luke Apr 30 '18 at 18:47
  • `Core.bitwise_or` is the way to go. Based on your [follow-up question](https://stackoverflow.com/q/50115325/3962537) I conclude that the different results were due to accidental swap of blue and red channels earlier in the algorithm. – Dan Mašek May 01 '18 at 13:49

2 Answers2

2

Union operator, logic-or (|) for two matrix of the OpenCV:

Code in 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; 

Code in 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);

Code in 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)
Kinght 金
  • 17,681
  • 4
  • 60
  • 74
0

I think what you're mistaking is that you are trying to compare two Mat object refrences and not the values beneath them. I'm not super familiar with openCV but my guess is you are trying to combine the values under them, so you could use the method https://docs.opencv.org/java/2.4.2/org/opencv/core/Mat.html#nativeObj. which that line in question would be re-written as: Mat hsvThres = new Mat(mask1.nativeObj | mask2.nativeObj);

scigs
  • 529
  • 1
  • 5
  • 12
  • Also check the main source of code to better understand [Link](https://stackoverflow.com/questions/32522989/opencv-better-detection-of-red-color) – J.doe Apr 30 '18 at 17:48
  • @scigs `mask1` and `mask2` are two arrays – Jeru Luke Apr 30 '18 at 17:48
  • No `mask1` and `mask2` are NOT arrays they are of type `Mat` smth made for openCV that designates Matrices, `mask1.nativeObj` produces a number of type `long`. `System.out.println(mask1.nativeObj); //140424926904688` – J.doe Apr 30 '18 at 18:15