How to represent the calculation in this image mathematically?

enter image description here

For example: With the discrete convolution and Fourier Transform.

It tries to do a calculation on the original image (image $A$/input) and save the result of the calculation in image $B$/output. It tries to multiply the pixel you are dealing with, with $5$ and it tries to multiply one pixel at the left, one pixel at the right, one pixel above of it and one pixel under it with $-1$ and adds the value of all the $5$ pixels you just multiplied together. After that, it will save the value of the new pixel in picture $B$. This happens to every single pixel in the image. Since there are no surrounding pixel on the corners, it simply takes them from somewhere else.

Which makes more sense?

Solution A:

$c(i,j) = \displaystyle\sum_{k_1 \in \Bbb N} \displaystyle\sum_{k_2 \in \Bbb N} a(k_1, k_2) b(i - k_1, j - k_2)$ where $a$ and $b$ are your matrices.

Supposing $a$ is your picture, by setting $b(0,0)=5$, $b(±1,0)=−1$, $b(0,±1)=−1$ and $b=0$ elsewhere.

Solution B:

$$ (f \ast g)(x,y) = \sum_{i=-\infty}^\infty \sum_{j=-\infty}^\infty f(i,j) g(x-i,y-j). $$ $f$ is the filter, $g$ is the image, and $f \ast g$ is the filtered image. We have $f(0,0) = 5$, $f(-1,0) = f(1,0) = f(0,-1) = f(0,1) = -1$, and $f(i,j) = 0$ otherwise.

Note to answerer: Please try not to overcomplicate your answer...

  • $\begingroup$ nice animation interesting.+1 $\endgroup$ Apr 7, 2013 at 9:11
  • $\begingroup$ i think it is called Walsh-Hadamard Transform,is not it? $\endgroup$ Apr 7, 2013 at 9:12
  • $\begingroup$ en.wikipedia.org/wiki/Kernel_(image_processing) $\endgroup$ Apr 7, 2013 at 9:16
  • $\begingroup$ @dato That doesn't explain how te represent it with a discrete convolution? $\endgroup$ Apr 7, 2013 at 9:17
  • 2
    $\begingroup$ That website I linked to shows a mathematical description of the formula. You don't need to break it up into a series of two one dimensional convolutions. $$c(i,j) = \displaystyle\sum_{k_1 \in \Bbb N} \displaystyle\sum_{k_2 \in \Bbb N} a(k_1, k_2) b(i - k_1, j - k_2)$$ where $a$ and $b$ are your matrices. Supposing $a$ is your picture, by setting $b(0,0) = 5, b(\pm 1, 0) = -1$ and $b(0, \pm 1) = -1$ and $b = 0$ elsewhere, you will get the desired effect. $\endgroup$
    – muzzlator
    Apr 7, 2013 at 10:39

1 Answer 1


Let $A$ be an $m\times n$ matrix representing the input image, and $K$ be an $s\times t$ matrix representing the kernel, with $s$ and $t$ odd (since we need it to have a central entry). We will index $A$ with $$(i,j)\in \{1,\dotsc,m\}\times\{1,\dotsc,n\}$$ and we will index $K$ with $$(h,k)\in\left\{-\sigma, \dotsc, \sigma\right\}\times\left\{-\tau, \dotsc,\tau\right\}$$ where $\sigma=\left\lfloor \frac{s}{2}\right\rfloor$, $\tau=\left\lfloor \frac{t}{2}\right\rfloor$, and with $\lfloor \cdot \rfloor$ we mean the floor function. We do this so that the central entry of $K$ will have index $(h,k)=(0,0)$.

To take care of the edge entries we add two rows and two columns to $A$ by repeating the first and last row and column. That is, we define the extra entries $$ \begin{align} A(0,0)&:=A(1,1) & A(0,j)&:=A(1,j) & A(0,n+1)&:=A(1,n)\\ A(i,0)&:=A(i,1) & & & A(i,n+1)&:=A(i,n) \\ A(m+1,0)&:=A(m+1,1) & A(m+1,j)&:=A(m,j) & A(m+1,n+1)&:=A(m,n) \end{align} $$ This corresponds to the "extension" edge handling method. You can easily change those extra rows and columns to adapt to other methods.

Then for any fixed $(i,j)\in\{1,\dotsc,m\}\times\{1,\dotsc,n\}$ the entry $(i,j)$ of $K*A$ will be $$ (K*A)(i,j) = \sum_{h=-\sigma}^{\sigma}\sum_{k=-\tau}^{\tau} K(h,k)\;A(i+h,j+k) $$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.