开发者

correctness of in-place convolution filter?

开发者 https://www.devze.com 2023-02-22 17:26 出处:网络
I have a simple box blur function that takes an ImageData object, returning it when done. However, I have just realised that this implementation may be incorrect because the ImageData object is edited

I have a simple box blur function that takes an ImageData object, returning it when done. However, I have just realised that this implementation may be incorrect because the ImageData object is edited in place and convolution filters depend on surrounding pixels. Should I be reading from the original ImageData and writing to a new one so that each pixel doesn't depend on already-changed surrounding pixels? If so, I'll have to rework my web worker manager to supply a new ImageData for the convolution functions to write to.

expressive.boxBlur = function(data, options) {
    var w = data.width, h = data.height, dataReal = data.data;
    for (var i = 0; i < w; i++)
        for (var j = 0; j < h; j++)
            for (var k = 0; k < 4; k++) {
                var total = 0, values = 0, temp = 0;
                if (!(i == 0 && j == 0)) {
                    temp = dataReal[4 * w * (j - 1) + 4 * (i - 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == w - 1 && j == 0)) {
                    temp = dataReal[4 * w * (j - 1) + 4 * (i + 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == 0 && j == h - 1)) {
                    temp = dataReal[4 * w * (j + 1) + 4 * (i - 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == w - 1 && j == h - 1)) {
                    temp = dataReal[4 * w * (j + 1) + 4 * (i + 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(j == 0)) {
                    temp = dataReal[4 * w * (j - 1) + 4 * (i + 0) + k];
                    if (temp !== undefined) 开发者_如何学Cvalues++, total += temp;
                }
                if (!(j == h - 1)) {
                    temp = dataReal[4 * w * (j + 1) + 4 * (i + 0) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == 0)) {
                    temp = dataReal[4 * w * (j + 0) + 4 * (i - 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == w - 1)) {
                    temp = dataReal[4 * w * (j + 0) + 4 * (i + 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                values++, total += dataReal[4 * w * j + 4 * i + k];
                total /= values;
                dataReal[4 * w * j + 4 * i + k] = total;
            }
    return data;
};


You're right, you need a separate image to put the convoluted result in. Unless the impuls response is a scaled dirac function. (i.e. it has only 1 point in the center)

However, you could do with a cache for only a few scanlines, saving a lot of memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消