r/learncpp • • Aug 03 '20

Lookup table for position

Hello. I am trying to speed up my program. I need to iterate over an image for multiple times. For every pixel, I need to calculate if it is in a specified region. If not then I ignore the pixel. The region is defined at the start and does not change.

My code looks like:

for (auto i = 0; i < source_image.rows; ++i)
{
  for (auto j = 0; j < source_image.cols; ++j)
  {
    if (!in_region(i, j)) continue;

    // ...
  }
}

I thought this could be sped up by using a lookup table. But it does not improve the execution time so I think my implementation was not right. I tried to create an image with the same shape and instead of colors, I save a 1 if it is in the region and a 0 if not.

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/thegreatunclean Aug 03 '20

OpenCV has a "region of interest" feature that lets you specify sub-regions of an image and perform operations on those sub-regions. AFAIK it's limited to rectangles so if you want something more complex you'll have to implement it yourself, but anything you can do to cut down on the number of pixels you iterate over will make a massive improvement.

I had a project that did operations on circles. I converted the circle to a bounding rectangle, grabbed that ROI, and then looped over the pixels in a way that cut out as many as possible. I recommend you try something similar.

1

u/Moschte09 Aug 03 '20

Yes, I had the same idea but the problem is that they are into each other. Maybe the best thing I can do is to is to resize the image.