Ask Your Question
0

Apply function to each Mat element

asked 2016-03-17 17:14:24 -0600

mark92m gravatar image

Does anyone know of a more efficient OpenCV practice to perform the following operation on each Mat element ? Each element undergoes the same function.

Mat calcDepthValues(Mat &depth){

Mat realDepth= depth.clone();

Mat_<double>::iterator it = realDepth.begin<double>();
Mat_<double>::iterator itend = realDepth.end<double>();

for (; it != itend; ++it) {

    (*it) = 448.251214 +  ((1- (*it)/255)  * 10758.02914);

}
return realDepthValues;
edit retag flag offensive close merge delete

Comments

1

You should take a deep look at the basic matrix operations.

LorenaGdL gravatar imageLorenaGdL ( 2016-03-17 17:22:17 -0600 )edit

So you suggest first dividing all elements by 255, then negating it, then adding 1, then using the mul() function , and so on ... for each step of the algorithm ?

mark92m gravatar imagemark92m ( 2016-03-17 17:25:36 -0600 )edit

Wow, that's the fastest "deep look" I've ever seen in 3 minutes time... I'm suggesting you to understand how to take advantage of OpenCV's overloaded functions that operate on complete matrices, instead of coding poor per-pixel loops on your own.

LorenaGdL gravatar imageLorenaGdL ( 2016-03-17 17:29:09 -0600 )edit

... cause I've been looking "deeply" at it for quite some time today, but I'm still getting used to how OpenCV operates. I think i understand what you mean.

mark92m gravatar imagemark92m ( 2016-03-17 17:34:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-03-17 20:45:51 -0600

Agree with @LorenaGdL about using basic matrix operations which are optimized by SSE, AVX codes and other parallel techniques (if needed). Your code should be:

void calcDepthValues(const Mat &depth, Mat & realDepth){ // should not return a Mat, it can be a very huge object
        realDepth= depth.clone();
        float a = -10758.02914f/255; // fix it if you already knew it 
        float b = 448.251214f+10758.02914f; // fix it if you already knew it

        realDepth *= a;
        realDepth += b;
    }
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-17 17:14:24 -0600

Seen: 1,693 times

Last updated: Mar 17 '16