Optimized float Blur variations

后端 未结 2 1985
野性不改
野性不改 2021-01-25 19:13

I am looking for optimized functions in c++ for calculating areal averages of floats. the function is passed a source float array, a destination float array (same size as source

2条回答
  •  悲哀的现实
    2021-01-25 19:38

    To add to sellibitze's answer, you can use a summed area table for your O, S and + kernels (not for the X one though). That way you can convolve a pixel in constant time, and it's probably the fastest method to do it for kernel shapes that allow it.

    Basically, a SAT is a data structure that lets you calculate the sum of any axis-aligned rectangle. For the O kernel, after you've built a SAT, you'd take the sum of the outer rect's pixels and subtract the sum of the inner rect's pixels. The S and + kernels can be implemented similarly.

    For the X kernel you can use a different approach. A skewed box filter is separable:

    Skewed box filter

    You can convolve with two long, thin skewed box filters, then add the two resulting images together. The center of the X will be counted twice, so will you need to convolve with another skewed box filter, and subtract that.

    Apart from that, you can optimize your box blur in many ways.

    • Remove the two ifs from the inner loop by splitting that loop into three loops - two short loops that do checks, and one long loop that doesn't. Or you could pad your array with extra elements from all directions - that way you can simplify your code.
    • Calculate values like h * 2 + 1 outside the loops.
    • An expression like f_temp[ky*fwidth + x] does two adds and one multiplication. You can initialize a pointer to &f_temp[ky*fwidth] outside the loop, and just increment that pointer in the loop.
    • Don't do the division by h * 2 + 1 in the horizontal step. Instead, divide by the square of that in the vertical step.

提交回复
热议问题