Ask Your Question
0

Best way to apply a function to each element of Mat (Android)

asked 2018-03-03 13:58:37 -0500

oralb gravatar image

updated 2018-03-04 10:57:58 -0500

Hello,

I've seen the thread here with the same request but the two main answers were to use pointers and to use parallel methods. Neither are really available on the Android implementation of OpenCV.

What would be the fastest way to apply a function like sin(pi/k * element^2) on Android OpenCV and then insert that value into another matrix?

edit: related question: is it faster to collect elements in a Java array first and then insert all of them at once as a row instead of individually with .put()

edit retag flag offensive close merge delete

Comments

1

" is it faster to collect elements in a Java array first and then insert all of them at once " -- definitely !

berak gravatar imageberak ( 2018-03-04 10:59:20 -0500 )edit

@berak, confirming that you mean collect as a row right? Or can I collect all elements in some kind of multi-dimensional array and then do only one operation to stick it in to the matrix?

oralb gravatar imageoralb ( 2018-03-04 11:40:14 -0500 )edit
1

I've just seen this answer which answers some of my questions

oralb gravatar imageoralb ( 2018-03-04 11:43:45 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-03-04 11:01:55 -0500

berak gravatar image

updated 2018-03-04 12:15:34 -0500

something like:

Mat m = ...
byte pixels[] = new byte[m.total() * m.channels()];
m.get(0,0, pixels); // copy to array
// do your operation
m.put(0,0, pixels); // copy to mat
edit flag offensive delete link more

Comments

Is this get operation retrieving an entire row/column or just one element? Would I stick the .get() in a loop that iterates over each element, do my operation, and then stick back in? Additionally, how comes pixels is a byte array, is that what I should use or is it an example (because I thought my pixels matrix is just in doubles)? Sorry for so many questions

oralb gravatar imageoralb ( 2018-03-04 11:39:19 -0500 )edit

"Is this get operation retrieving an entire row/column or just one element?" -- the whole image.

berak gravatar imageberak ( 2018-03-04 12:23:24 -0500 )edit
1

I've got it now, it gets the whole image starting from 0,0 and sticks it in the array. For my case I needed a double[] and was able to get and put. It's roughly 3x faster on a small matrix. Thanks!

oralb gravatar imageoralb ( 2018-03-04 12:26:58 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-03-03 13:58:37 -0500

Seen: 94 times

Last updated: Mar 04 '18