Ask Your Question
-1

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

asked 2017-10-30 06:09:13 -0500

kaba gravatar image

updated 2017-10-30 07:29:45 -0500

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 flag offensive close merge delete

Comments

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

berak gravatar imageberak ( 2017-10-30 06:29:33 -0500 )edit

@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.)

kaba gravatar imagekaba ( 2017-10-30 07:06:45 -0500 )edit

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.

berak gravatar imageberak ( 2017-10-30 07:08:48 -0500 )edit

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

kaba gravatar imagekaba ( 2017-10-30 07:30:19 -0500 )edit

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)

berak gravatar imageberak ( 2017-10-30 08:57:07 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-11-07 07:45:40 -0500

kaba gravatar image

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);
Core.add(id_a_mat,new Scalar(id_a),id_a_mat);
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...]

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-10-30 06:09:13 -0500

Seen: 152 times

Last updated: Nov 07 '17