Ask Your Question

Java API - Core.add() method not working!? How to add 2 Mats?

asked 2013-06-25 13:15:57 -0500

agforte gravatar image

updated 2013-06-25 13:57:45 -0500

When trying to add two Mat elements by using the Core.add() method, these are added only if src1 is an EXACT copy of src2 (or viceversa). As soon as I change the value of an element in one of them, the addition does not happen and the original unmodified value is returned.


Mat dst = new Mat(src1.rows(), src1.cols(), CvType.CV_32F);

The following works correctly:

src2 = src1.clone();
Core.add(src1, src2, dst);

This, instead, does not work:

src2 = src1.clone();
src2.put(0,0,1.0); // Changing element in position 0,0 of src2
Core.add(src1, src2, dst);

Element (0,0) of dst has the same value of element (0,0) of src1. No addition takes place.

This is true also if the two matrices (src1 and src2) are completely different, if I use a Scalar instead of a matrix, etc. I also tried using the Core.add() method with a mask but with no success either.

I am using Android 4.2.2 on ARM.

Anybody has any idea or suggestion??? Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-07-30 14:30:24 -0500

ttux gravatar image

updated 2015-07-30 14:46:58 -0500

I have a similar issue I believe and I don't know why it doesn't work. Using OpenCV 3.0. I tried the following:

Mat thresholdBW = new Mat(original.rows(),original.cols(), CvType.CV_8UC1);
Core.inRange(hsv, new Scalar(0, 0, 0), new Scalar(360, 70, 70), thresholdBW);

Mat thresholdBLUE = new Mat(original.rows(),original.cols(), CvType.CV_8UC1);
Core.inRange(hsv, new Scalar(90, 90, 90), new Scalar(130, 255, 255), thresholdBLUE);

Mat threshold = new Mat(original.rows(),original.cols(), CvType.CV_8UC1);
Core.add(thresholdBW, thresholdBLUE, threshold);

thresholdBLUE and thresholdBW have what I want but then after the Core.add call, threshold is just empty. Is there something I am missing?

I found a way to get the result I want by using addWeighted:

Core.addWeighted(thresholdBW, 1, thresholdBLUE, 1, 0, threshold);

But I don't know why Core.add doesn't work.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-06-25 13:15:57 -0500

Seen: 2,633 times

Last updated: Jul 30 '15