Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make this faster? (C/C++) OpenCV
    primarykey
    data
    text
    <p>I am processing frames in a video and displaying it live (real time). The algorithm is fast, but I am wondering if there's any optimizations that I can do that will make it even more seamless. I don't know what functions in my algorithm take up the most amount of time, my guess is the sqrt() function because apparently it does some look ups, but i am not sure.</p> <p>This is my algorithm:</p> <pre><code>IplImage *videoFrame = cvCreateImage(cvSize(bufferWidth, bufferHeight), IPL_DEPTH_8U, 4); videoFrame-&gt;imageData = (char*)bufferBaseAddress; int channels = videoFrame-&gt;nChannels; int widthStep = videoFrame-&gt;widthStep; int width = videoFrame-&gt;width; int height = videoFrame-&gt;height; for(int i=0;i&lt;height;i++){ uchar *col = ((uchar *)(videoFrame-&gt;imageData + i*widthStep)); for(int j=0;j&lt;width;j++){ double pRed = col[j*channels + 0]; double pGreen = col[j*channels + 1]; double pBlue = col[j*channels + 2]; double dRed = green.val[0] - pRed; double dGreen = green.val[1] - pGreen; double dBlue = green.val[2] - pBlue; double sDRed = dRed * dRed; double sDGreen = dGreen * dGreen; double sDBlue = dBlue * dBlue; double sum = sDRed + sDGreen + sDBlue; double euc = sqrt(sum); //NSLog(@"%f %f %f", pRed, pGreen, pBlue); if (euc &lt; threshold) { col[j*channels + 0] = white.val[0]; col[j*channels + 1] = white.val[1]; col[j*channels + 2] = white.val[2]; } } } </code></pre> <p>Thanks!</p> <p><strong>UPDATE</strong> Ok, so what this does is loop throughout every pixel in the image, and calculator the Euclidean distance between the color of the pixel and green color. So, overall this is a green screen algorithm. </p> <p>I did some benchmarks, and the fps without using this algorithm is 30.0fps. Using this algorithm, it falls down to about 8fps. But, the majority of the for drop comes from <code>col[j*channels + 0];</code> If the algorithm doesn't do anything else and use access the array elects, it drops down to about 10fps.</p> <p><strong>UPDATE 2</strong> Ok this is interesting, I was removing random lines from the stuff inside the double loop to see what causes the bigger overhead and this is what I found: Creating variables on the stack causes HUGE drop in FPS. Consider this example:</p> <pre><code>for(int i=0;i&lt;height;i++){ uchar *col = ((uchar *)(data + i*widthStep)); for(int j=0;j&lt;width;j++){ double pRed = col[j*channels + 0]; double pGreen = col[j*channels + 1]; double pBlue = col[j*channels + 2]; } } </code></pre> <p>This drops the fps to 11-ish.</p> <p>Now this on the other hand:</p> <pre><code>for(int i=0;i&lt;height;i++){ uchar *col = ((uchar *)(data + i*widthStep)); for(int j=0;j&lt;width;j++){ col[j*channels + 0]; col[j*channels + 1]; col[j*channels + 2]; } } </code></pre> <p>doesn't drop the FPS at all! The FPS stays at a pretty 30.0. Thought I should update this and let you guys know what's this is the real bottle neck, making variables not he stack. I wonder if I inline everything I might get a pure 30.0fps.</p> <p>Nvm...maybe the expressions that aren't assigned to a var aren't even evaluated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload