# per-element max of channel 1 with copy of channel 2 ? (java)

I need to implement something like this:

• do a per-element max() for two matrices a, b into a result matrix c [that is: c[i]=max(a[i],b[i])]
• remember (separately for each element) if a or b provided the max value (in a second channel of c or in a new matrix d) (this is the interesting part for me)

Is there a good way to use opencv functionality for this? (NB: I'll implement in java, but I guess my question is language-independent.)

edit retag close merge delete

" per-element max() for two not-equal-sized matrices a, b" -- does not make any sense (at least not without further explanation)

@berak It does not? :) Let's say: a and b are padded to equal size before. Or I redefine:

  c[i] = a[i] if b[i] doesn't exist,
c[i] = b[i] if a[i] doesn't exist,
c[i] = max(a[i],b[i]) else


but I thought that would have made the question more complicate than it needed to be!? Anyway, the tricky part is the second one (remembering if the value originated from a or b), isn't it? For that one especially I'd very much like a pointer to a possible and efficient solution! (Of course I could code that in pure Java. That would work but presumably quite slow.)

no, it does not. if the images don't have the same size, i in one image does not refer to the same position, as in another image, and you're comparing apples to pears.

again, WHY? (we're still waiting for an explanation)

there are matrix operations for all of this, but you have to solve this problem, before.

maybe the problem is your single index , while images are 2d ? and sure, there are submats and padding to solve it.

I edited the question to make it more readable. (@berak)

max and compare is all you need.

and how comes, that i am reading the docs, while you don't ? (that's the really silly part here)

Sort by » oldest newest most voted

The hint in the comments led into the right direction. With a bit of additional decoration the problem could be solved with Core.compare() . Give are matrices a and b and their numerical ids id_a and id_b (id_b>id_a):

Mat id_a_mat = new Mat(a.size(),CvType.CV8UC1);
Mat bigger = new Mat();
Core.compare(a,b,bigger,Core.CMP_LT);  // <----
Mat bigger_id_b = new Mat();
Imgproc.threshold(bigger,bigger_id_b,128,id_b,CvType.CV8UC1);
Mat d = new Mat();
Core.max(id_a_mat,bigger_id_b,d);
Mat c = new Mat();
Core.max(a,b,c);


[NB: The tone around here seems quite rough...]

more

Official site

GitHub

Wiki

Documentation