OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Wed, 01 Aug 2018 14:36:35 -0500Efficient calculation with multiple cv::Mathttp://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/ Hello,
I need to do some math with my matrices and I would like to know the most performant way.
I have the following vectors:
std::vector<cv::Mat> b //200 matrices with 1 row and 1000 elements per row
std::vector<cv::Mat> w //200 matrices with 1 row and 1000 elements per row
std::vector <cv::Mat>image //200 matrices with 500 rows and 1000 elements per row
All vectors with the same index belong together.
I need to calculate the following for every Mat of the vectors with the same index and every pixel in the image:
image(x, y) = (image(x,y) – b(x)) / (w(x) – b(x)) * 0,9
I would start like the following:
for(int i=0; i<image.size(); i++)
{
//And here?
//Usage of the “at”-method or pointer arithmetic?
//Or is there an operation which can calculate everything automatically line by line?
}
Thank you very much :-)
Tue, 31 Jul 2018 15:33:44 -0500http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/Comment by opalmirror for <p>Hello,
I need to do some math with my matrices and I would like to know the most performant way.
I have the following vectors:</p>
<pre><code>std::vector<cv::Mat> b //200 matrices with 1 row and 1000 elements per row
std::vector<cv::Mat> w //200 matrices with 1 row and 1000 elements per row
std::vector <cv::Mat>image //200 matrices with 500 rows and 1000 elements per row
</code></pre>
<p>All vectors with the same index belong together.
I need to calculate the following for every Mat of the vectors with the same index and every pixel in the image: </p>
<pre><code>image(x, y) = (image(x,y) – b(x)) / (w(x) – b(x)) * 0,9
</code></pre>
<p>I would start like the following:</p>
<pre><code>for(int i=0; i<image.size(); i++)
{
//And here?
//Usage of the “at”-method or pointer arithmetic?
//Or is there an operation which can calculate everything automatically line by line?
}
</code></pre>
<p>Thank you very much :-)</p>
http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196701#post-id-196701you could precalculate Mat mwb = 0.9 / (w - b), which is used to scale the results of every line, then for each row of an image you could calculate new_image_row = mwb * (image_row - b), where '*' is an element by element multiply. Parallelism (CPU core or thread) is a possible thing to exploit, see TBB and threads. SIMD vector arithmetic might allows you to accelerate the cell arithmetic for specific Mat data cell types. Good luck!Tue, 31 Jul 2018 20:13:23 -0500http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196701#post-id-196701Comment by robro for <p>Hello,
I need to do some math with my matrices and I would like to know the most performant way.
I have the following vectors:</p>
<pre><code>std::vector<cv::Mat> b //200 matrices with 1 row and 1000 elements per row
std::vector<cv::Mat> w //200 matrices with 1 row and 1000 elements per row
std::vector <cv::Mat>image //200 matrices with 500 rows and 1000 elements per row
</code></pre>
<p>All vectors with the same index belong together.
I need to calculate the following for every Mat of the vectors with the same index and every pixel in the image: </p>
<pre><code>image(x, y) = (image(x,y) – b(x)) / (w(x) – b(x)) * 0,9
</code></pre>
<p>I would start like the following:</p>
<pre><code>for(int i=0; i<image.size(); i++)
{
//And here?
//Usage of the “at”-method or pointer arithmetic?
//Or is there an operation which can calculate everything automatically line by line?
}
</code></pre>
<p>Thank you very much :-)</p>
http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196707#post-id-196707Thanks for the idea! I just had another one. I could make the number of rows of "b" and "w" dynamically the same as image and then use direct methods like "cv::subtract". Is this a good idea?Wed, 01 Aug 2018 00:23:25 -0500http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196707#post-id-196707Answer by berak for <p>Hello,
I need to do some math with my matrices and I would like to know the most performant way.
I have the following vectors:</p>
<pre><code>std::vector<cv::Mat> b //200 matrices with 1 row and 1000 elements per row
std::vector<cv::Mat> w //200 matrices with 1 row and 1000 elements per row
std::vector <cv::Mat>image //200 matrices with 500 rows and 1000 elements per row
</code></pre>
<p>All vectors with the same index belong together.
I need to calculate the following for every Mat of the vectors with the same index and every pixel in the image: </p>
<pre><code>image(x, y) = (image(x,y) – b(x)) / (w(x) – b(x)) * 0,9
</code></pre>
<p>I would start like the following:</p>
<pre><code>for(int i=0; i<image.size(); i++)
{
//And here?
//Usage of the “at”-method or pointer arithmetic?
//Or is there an operation which can calculate everything automatically line by line?
}
</code></pre>
<p>Thank you very much :-)</p>
http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?answer=196713#post-id-196713you should not do anything per pixel, but process whole rows at a time:
for(int i=0; i<image.size(); i++)
{
Mat wb = (w[i] - b[i]) * 0.9;
for (int j=0; j<images[i].rows; j++)
{
divide( image[i].row(j) - b[i], wb, image[i].row(j) );
}
}Wed, 01 Aug 2018 02:04:06 -0500http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?answer=196713#post-id-196713Comment by opalmirror for <p>you should not do anything per pixel, but process whole rows at a time:</p>
<pre><code>for(int i=0; i<image.size(); i++)
{
Mat wb = (w[i] - b[i]) * 0.9;
for (int j=0; j<images[i].rows; j++)
{
divide( image[i].row(j) - b[i], wb, image[i].row(j) );
}
}
</code></pre>
http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196777#post-id-196777Full answer from @berak is great; it has one little bug in that 0.9 needs to be used to multiply elements of the j'th row of image, not divide it. So the Mat line could be rewritten:
Mat wb = (w[i] - b[i]) / 0.9;Wed, 01 Aug 2018 13:54:12 -0500http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196777#post-id-196777Comment by berak for <p>you should not do anything per pixel, but process whole rows at a time:</p>
<pre><code>for(int i=0; i<image.size(); i++)
{
Mat wb = (w[i] - b[i]) * 0.9;
for (int j=0; j<images[i].rows; j++)
{
divide( image[i].row(j) - b[i], wb, image[i].row(j) );
}
}
</code></pre>
http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196778#post-id-196778^^ ofc. right. but i think - let's rather keep everything "as is" (not update the answer), it'll probably be much easier to understand this wayWed, 01 Aug 2018 14:36:35 -0500http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196778#post-id-196778Comment by robro for <p>you should not do anything per pixel, but process whole rows at a time:</p>
<pre><code>for(int i=0; i<image.size(); i++)
{
Mat wb = (w[i] - b[i]) * 0.9;
for (int j=0; j<images[i].rows; j++)
{
divide( image[i].row(j) - b[i], wb, image[i].row(j) );
}
}
</code></pre>
http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196774#post-id-196774Thank you very much! This is exactly what I was looking for :-)Wed, 01 Aug 2018 12:27:18 -0500http://answers.opencv.org/question/196698/efficient-calculation-with-multiple-cvmat/?comment=196774#post-id-196774