<s>
In	O
image	B-Algorithm
processing	I-Algorithm
,	O
a	O
kernel	B-Algorithm
,	O
convolution	B-Language
matrix	B-Architecture
,	O
or	O
mask	O
is	O
a	O
small	O
matrix	B-Architecture
used	O
for	O
blurring	O
,	O
sharpening	O
,	O
embossing	O
,	O
edge	B-Algorithm
detection	I-Algorithm
,	O
and	O
more	O
.	O
</s>
<s>
This	O
is	O
accomplished	O
by	O
doing	O
a	O
convolution	B-Language
between	O
the	O
kernel	B-Algorithm
and	O
an	O
image	O
.	O
</s>
<s>
Or	O
more	O
simply	O
,	O
when	O
each	O
pixel	O
in	O
the	O
output	O
image	O
is	O
a	O
function	O
of	O
the	O
nearby	O
pixels	O
(	O
including	O
itself	O
)	O
in	O
the	O
input	O
image	O
,	O
the	O
kernel	B-Algorithm
is	O
that	O
function	O
.	O
</s>
<s>
where	O
is	O
the	O
filtered	O
image	O
,	O
is	O
the	O
original	O
image	O
,	O
is	O
the	O
filter	O
kernel	B-Algorithm
.	O
</s>
<s>
Every	O
element	O
of	O
the	O
filter	O
kernel	B-Algorithm
is	O
considered	O
by	O
and	O
.	O
</s>
<s>
Depending	O
on	O
the	O
element	O
values	O
,	O
a	O
kernel	B-Algorithm
can	O
cause	O
a	O
wide	O
range	O
of	O
effects	O
.	O
</s>
<s>
The	O
above	O
are	O
just	O
a	O
few	O
examples	O
of	O
effects	O
achievable	O
by	O
convolving	O
kernels	B-Algorithm
and	O
images	O
.	O
</s>
<s>
The	O
origin	O
is	O
the	O
position	O
of	O
the	O
kernel	B-Algorithm
which	O
is	O
above	O
(	O
conceptually	O
)	O
the	O
current	O
output	O
pixel	O
.	O
</s>
<s>
This	O
could	O
be	O
outside	O
of	O
the	O
actual	O
kernel	B-Algorithm
,	O
though	O
usually	O
it	O
corresponds	O
to	O
one	O
of	O
the	O
kernel	B-Algorithm
elements	O
.	O
</s>
<s>
For	O
a	O
symmetric	O
kernel	B-Algorithm
,	O
the	O
origin	O
is	O
usually	O
the	O
center	O
element	O
.	O
</s>
<s>
Convolution	B-Language
is	O
the	O
process	O
of	O
adding	O
each	O
element	O
of	O
the	O
image	O
to	O
its	O
local	O
neighbors	O
,	O
weighted	O
by	O
the	O
kernel	B-Algorithm
.	O
</s>
<s>
This	O
is	O
related	O
to	O
a	O
form	O
of	O
mathematical	B-Language
convolution	I-Language
.	O
</s>
<s>
The	O
matrix	B-Architecture
operation	O
being	O
performedconvolutionis	O
not	O
traditional	O
matrix	B-Architecture
multiplication	O
,	O
despite	O
being	O
similarly	O
denoted	O
by	O
*	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
we	O
have	O
two	O
three-by-three	O
matrices	O
,	O
the	O
first	O
a	O
kernel	B-Algorithm
,	O
and	O
the	O
second	O
an	O
image	O
piece	O
,	O
convolution	B-Language
is	O
the	O
process	O
of	O
flipping	O
both	O
the	O
rows	O
and	O
columns	O
of	O
the	O
kernel	B-Algorithm
and	O
multiplying	O
locally	O
similar	O
entries	O
and	O
summing	O
.	O
</s>
<s>
The	O
element	O
at	O
coordinates	O
[	O
2	O
,	O
2 ]	O
(	O
that	O
is	O
,	O
the	O
central	O
element	O
)	O
of	O
the	O
resulting	O
image	O
would	O
be	O
a	O
weighted	O
combination	O
of	O
all	O
the	O
entries	O
of	O
the	O
image	O
matrix	B-Architecture
,	O
with	O
weights	O
given	O
by	O
the	O
kernel	B-Algorithm
:	O
</s>
<s>
The	O
other	O
entries	O
would	O
be	O
similarly	O
weighted	O
,	O
where	O
we	O
position	O
the	O
center	O
of	O
the	O
kernel	B-Algorithm
on	O
each	O
of	O
the	O
boundary	O
points	O
of	O
the	O
image	O
,	O
and	O
compute	O
a	O
weighted	O
sum	O
.	O
</s>
<s>
The	O
values	O
of	O
a	O
given	O
pixel	O
in	O
the	O
output	O
image	O
are	O
calculated	O
by	O
multiplying	O
each	O
kernel	B-Algorithm
value	O
by	O
the	O
corresponding	O
input	O
image	O
pixel	O
values	O
.	O
</s>
<s>
for	O
each	O
kernel	B-Algorithm
row	O
in	O
kernel	B-Algorithm
:	O
</s>
<s>
for	O
each	O
element	O
in	O
kernel	B-Algorithm
row	O
:	O
</s>
<s>
*	O
corresponding	O
input	O
image	O
pixels	O
are	O
found	O
relative	O
to	O
the	O
kernel	B-Algorithm
's	O
origin	O
.	O
</s>
<s>
If	O
the	O
kernel	B-Algorithm
is	O
symmetric	O
then	O
place	O
the	O
center	O
(	O
origin	O
)	O
of	O
the	O
kernel	B-Algorithm
on	O
the	O
current	O
pixel	O
.	O
</s>
<s>
The	O
kernel	B-Algorithm
will	O
overlap	O
the	O
neighboring	O
pixels	O
around	O
the	O
origin	O
.	O
</s>
<s>
Each	O
kernel	B-Algorithm
element	O
should	O
be	O
multiplied	O
with	O
the	O
pixel	O
value	O
it	O
overlaps	O
with	O
and	O
all	O
of	O
the	O
obtained	O
values	O
should	O
be	O
summed	O
.	O
</s>
<s>
This	O
resultant	O
sum	O
will	O
be	O
the	O
new	O
value	O
for	O
the	O
current	O
pixel	O
currently	O
overlapped	O
with	O
the	O
center	O
of	O
the	O
kernel	B-Algorithm
.	O
</s>
<s>
If	O
the	O
kernel	B-Algorithm
is	O
not	O
symmetric	O
,	O
it	O
has	O
to	O
be	O
flipped	O
both	O
around	O
its	O
horizontal	O
and	O
vertical	O
axis	O
before	O
calculating	O
the	O
convolution	B-Language
as	O
above	O
.	O
</s>
<s>
Kernel	B-Algorithm
convolution	B-Language
usually	O
requires	O
values	O
from	O
pixels	O
outside	O
of	O
the	O
image	O
boundaries	O
.	O
</s>
<s>
There	O
are	O
a	O
variety	O
of	O
methods	O
for	O
handling	O
image	B-Algorithm
edges	I-Algorithm
.	O
</s>
<s>
The	O
nearest	O
border	O
pixels	O
are	O
conceptually	O
extended	O
as	O
far	O
as	O
necessary	O
to	O
provide	O
values	O
for	O
the	O
convolution	B-Language
.	O
</s>
<s>
Move	O
kernel	B-Algorithm
so	O
that	O
values	O
from	O
outside	O
of	O
image	O
is	O
never	O
required	O
.	O
</s>
<s>
Example	O
:	O
Kernel	B-Algorithm
size	O
10x10	O
,	O
image	O
size	O
32x32	O
,	O
result	O
image	O
is	O
23x23	O
.	O
</s>
<s>
Any	O
pixel	O
in	O
the	O
kernel	B-Algorithm
that	O
extends	O
past	O
the	O
input	O
image	O
is	O
n't	O
used	O
and	O
the	O
normalizing	O
is	O
adjusted	O
to	O
compensate	O
.	O
</s>
<s>
Normalization	B-Algorithm
is	O
defined	O
as	O
the	O
division	O
of	O
each	O
element	O
in	O
the	O
kernel	B-Algorithm
by	O
the	O
sum	O
of	O
all	O
kernel	B-Algorithm
elements	O
,	O
so	O
that	O
the	O
sum	O
of	O
the	O
elements	O
of	O
a	O
normalized	B-Algorithm
kernel	B-Algorithm
is	O
unity	O
.	O
</s>
<s>
Fast	O
convolution	B-Language
algorithms	O
include	O
:	O
</s>
<s>
2D	O
convolution	B-Language
with	O
an	O
M	O
×	O
N	O
kernel	B-Algorithm
requires	O
M	O
×	O
N	O
multiplications	O
for	O
each	O
sample	O
(	O
pixel	O
)	O
.	O
</s>
<s>
If	O
the	O
kernel	B-Algorithm
is	O
separable	O
,	O
then	O
the	O
computation	O
can	O
be	O
reduced	O
to	O
M	O
+	O
N	O
multiplications	O
.	O
</s>
<s>
Using	O
separable	O
convolutions	B-Language
can	O
significantly	O
decrease	O
the	O
computation	O
by	O
doing	O
1D	O
convolution	B-Language
twice	O
instead	O
of	O
one	O
2D	O
convolution	B-Language
.	O
</s>
<s>
Here	O
a	O
concrete	O
convolution	B-Language
implementation	O
done	O
with	O
the	O
GLSL	B-Language
shading	O
language	O
:	O
</s>
