Even-core image convolution

I want to do a simple convolution of 2D images, but my core has a dimension. What indexes should I choose for my kernel? I tried a google search to answer and search for existing codes. People usually center their core, so there will be another pattern before the new 0. So, if we have a 4x4 core, the centered indices should be -2 -1 0 +1 . It's right? And if so, why is it so? Can someone explain why -2 -1 0 +1 correct, but -1 0 +1 +2 is not? Keep in mind that I want to perform convolution without using FFT.

+4
source share
2 answers

If I understand your question correctly, then for even cores you are right that this is an agreement to center the core so that there is another pattern in front of the new zero.

So, for a kernel with a width of 4, the centered indices will be -2 -1 0 +1 , as you say above.

However, this is really just a convention - asymmetric convolution is very rarely used in any case, and the exact nature of the asymmetry (left / right, etc.) has nothing to do with the โ€œcorrectโ€ result. I would suggest that the reason most implementations behave this way is because they can give comparable results with the same inputs.

When performing convolution in the frequency domain, the core is filled in the same way as the image size, and you have already stated that you are performing convolution in the spatial domain.

I am much more intrigued why you need to use an even sized kernel.

+4
source

The correct answer is to return a pixel of the results in the upper left corner, regardless of whether your matrix is โ€‹โ€‹uniform or not. Then you can simply perform the operation in a simple scan line, and they do not require memory.

 private static void applyBlur(int[] pixels, int stride) { int v0, v1, v2, r, g, b; int pos; pos = 0; try { while (true) { v0 = pixels[pos]; v1 = pixels[pos+1]; v2 = pixels[pos+2]; r = ((v0 >> 16) & 0xFF) + ((v1 >> 16) & 0xFF) + ((v2 >> 16) & 0xFF); g = ((v0 >> 8 ) & 0xFF) + ((v1 >> 8) & 0xFF) + ((v2 >> 8) & 0xFF); b = ((v0 ) & 0xFF) + ((v1 ) & 0xFF) + ((v2 ) & 0xFF); r/=3; g/=3; b/=3; pixels[pos++] = r << 16 | g << 8 | b; } } catch (ArrayIndexOutOfBoundsException e) { } pos = 0; try { while (true) { v0 = pixels[pos]; v1 = pixels[pos+stride]; v2 = pixels[pos+stride+stride]; r = ((v0 >> 16) & 0xFF) + ((v1 >> 16) & 0xFF) + ((v2 >> 16) & 0xFF); g = ((v0 >> 8 ) & 0xFF) + ((v1 >> 8) & 0xFF) + ((v2 >> 8) & 0xFF); b = ((v0 ) & 0xFF) + ((v1 ) & 0xFF) + ((v2 ) & 0xFF); r/=3; g/=3; b/=3; pixels[pos++] = r << 16 | g << 8 | b; } } catch (ArrayIndexOutOfBoundsException e) { } } 
+1
source

Source: https://habr.com/ru/post/1485541/


All Articles