2D convolution in TikZ: input, kernel and output explained
Quick answer
This figure shows one step of a 2D convolution: a 3 × 3 window of the 7 × 7 input I (red) is multiplied cell by cell with the kernel K (blue) and summed into one cell of the output I ∗ K (green). All three grids are TikZ matrices of nodes, so every cell has a name such as mtr-1-4 that the red and green frames, the dotted lines and the small ×1 and ×0 labels use as anchors. The cells are coloured with the |[fill=red!30]| prefix, and the matrices are placed side by side with the positioning library.
Contents
The figure and its code

% 2D convolution
% Source: https://github.com/PetarV-/TikZ/blob/86aee049f5355d77c598b936399e42caf8941e60/2D%20Convolution/2d_convolution.tex
% Copyright (c) 2016 Petar Veličković. MIT licence: https://github.com/PetarV-/TikZ/blob/master/LICENSE
% The licence’s copyright and permission notice: https://www.labcanvas.com/tikz-library/credits.html
\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}
\matrix (mtr) [matrix of nodes,row sep=-\pgflinewidth, nodes={draw}]
{
0 & 1 & 1 & |[fill=red!30]| 1 & |[fill=red!30]| 0 & |[fill=red!30]| 0 & 0\\
0 & 0 & 1 & |[fill=red!30]| 1 & |[fill=red!30]| 1 & |[fill=red!30]| 0 & 0\\
0 & 0 & 0 & |[fill=red!30]| 1 & |[fill=red!30]| 1 & |[fill=red!30]| 1 & 0\\
0 & 0 & 0 & 1 & 1 & 0 & 0\\
0 & 0 & 1 & 1 & 0 & 0 & 0\\
0 & 1 & 1 & 0 & 0 & 0 & 0\\
1 & 1 & 0 & 0 & 0 & 0 & 0\\
};
\draw[very thick, red] (mtr-1-4.north west) rectangle (mtr-3-6.south east);
\node [below= of mtr-5-4.south] (lm) {$\bf I$};
\node[right = 0.2em of mtr] (str) {$*$};
\matrix (K) [right=0.2em of str,matrix of nodes,row sep=-\pgflinewidth, nodes={draw, fill=blue!30}]
{
1 & 0 & 1 \\
0 & 1 & 0 \\
1 & 0 & 1 \\
};
\node [below = of K-3-2.south] (lk) {$\bf K$};
\node [right = 0.2em of K] (eq) {$=$};
\matrix (ret) [right=0.2em of eq,matrix of nodes,row sep=-\pgflinewidth, nodes={draw}]
{
1 & 4 & 3 & |[fill=green!30]| 4 & 1\\
1 & 2 & 4 & 3 & 3\\
1 & 2 & 3 & 4 & 1\\
1 & 3 & 3 & 1 & 1\\
3 & 3 & 1 & 1 & 0\\
};
\node [below = of ret-4-3.south] (lim) {${\bf I} * {\bf K}$};
\draw[very thick, green] (ret-1-4.north west) rectangle (ret-1-4.south east);
\draw[densely dotted, blue, thick] (mtr-1-4.north west) -- (K-1-1.north west);
\draw[densely dotted, blue, thick] (mtr-3-4.south west) -- (K-3-1.south west);
\draw[densely dotted, blue, thick] (mtr-1-6.north east) -- (K-1-3.north east);
\draw[densely dotted, blue, thick] (mtr-3-6.south east) -- (K-3-3.south east);
\draw[densely dotted, green, thick] (ret-1-4.north west) -- (K-1-1.north west);
\draw[densely dotted, green, thick] (ret-1-4.south west) -- (K-3-1.south west);
\draw[densely dotted, green, thick] (ret-1-4.north east) -- (K-1-3.north east);
\draw[densely dotted, green, thick] (ret-1-4.south east) -- (K-3-3.south east);
\matrix (K) [right=0.2em of str,matrix of nodes,row sep=-\pgflinewidth, nodes={draw, fill=blue!10}]
{
1 & 0 & 1 \\
0 & 1 & 0 \\
1 & 0 & 1 \\
};
\draw[very thick, blue] (K-1-1.north west) rectangle (K-3-3.south east);
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-1-4.south east) (xx) {\scalebox{.5}{$\times 1$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-1-5.south east) (xx) {\scalebox{.5}{$\times 0$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-1-6.south east) (xx) {\scalebox{.5}{$\times 1$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-2-4.south east) (xx) {\scalebox{.5}{$\times 0$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-2-5.south east) (xx) {\scalebox{.5}{$\times 1$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-2-6.south east) (xx) {\scalebox{.5}{$\times 0$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-3-4.south east) (xx) {\scalebox{.5}{$\times 1$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-3-5.south east) (xx) {\scalebox{.5}{$\times 0$}};
\node[anchor=south east, inner sep=0.01em, blue] at (mtr-3-6.south east) (xx) {\scalebox{.5}{$\times 1$}};
\end{tikzpicture}
\end{document}
The input as a matrix of nodes
Lines 5–22 of the code
The document class is standalone with the tikz and crop options, so the page is cropped to the picture; the drawing needs the matrix library for matrix of nodes and positioning for the placement that follows. In \matrix (mtr) [matrix of nodes,row sep=-\pgflinewidth, nodes={draw}], every cell’s text becomes a node, nodes={draw} gives each of them a border, and the cells are named after the matrix: row 1, column 4 is mtr-1-4. Cells are separated by & and every row ends with \\.
row sep=-\pgflinewidth moves the rows together by one line width, so the horizontal borders of neighbouring cells lie on top of each other. There is no matching column sep, so between columns two borders sit side by side and the vertical lines come out twice as thick as the horizontal ones; adding column sep=-\pgflinewidth would even them out. The nine window cells get their colour from the prefix |[fill=red!30]|, which passes options to a single cell.
The window frame and the I label
Lines 24–28 of the code
Because the cells are nodes, their corners are anchors. \draw[very thick, red] (mtr-1-4.north west) rectangle (mtr-3-6.south east); frames the window from the north-west corner of its first cell to the south-east corner of its last. The label I is placed with below= of mtr-5-4.south: the of-part is an anchor, a point rather than a node, so the label goes one node distance (1 cm by default) below the bottom of cell (5, 4), which happens to clear the two rows under it. The ∗ is a node right = 0.2em of mtr, 0.2em from the matrix’s right border.
The kernel, the result and their labels
Lines 30–50 of the code
The kernel is a second matrix named K, placed right=0.2em of str (right of the ∗) with nodes={draw, fill=blue!30} for every cell, and the output matrix ret follows the = sign the same way. Placing a whole matrix with right=… of works because a matrix is a node. Each label sits below = of a middle cell’s south anchor, and the output’s highlighted cell gets both |[fill=green!30]| and a green frame from its own corners.
The highlighted value is the sum of the window times the kernel: the window holds 1 0 0, 1 1 0 and 1 1 1, and multiplying cell by cell with 1 0 1, 0 1 0, 1 0 1 leaves 1 + 1 + 1 + 1 = 4. Strictly, a convolution flips the kernel and what CNNs compute is a cross-correlation, but this kernel is the same after a half turn, so both give the same picture.
Dotted lines between the corners
Lines 52–60 of the code
Eight densely dotted lines connect matching corners: from the window’s corners to the kernel’s, such as (mtr-1-4.north west) -- (K-1-1.north west), and from the output cell’s corners to the kernel’s. Using the same anchor names at both ends is what makes the lines form two clean “projection cones”.
The kernel drawn a second time
Lines 62–69 of the code
Lines 62 to 67 repeat the kernel matrix with the same name and the same placement, right=0.2em of str, but with the lighter fill=blue!10. The second copy lands exactly on the first and is drawn after the dotted lines, so it covers the ends of the lines and hides the darker first copy entirely: the kernel you see is the light one. The first copy is still needed, because the K label and the = sign were placed from it. Drawing the dotted lines first, or in a backgrounds scope, would make the second matrix unnecessary. Line 69 frames the kernel in blue.
The small ×1 and ×0 labels
Lines 71–79 of the code
Each window cell gets the kernel value it is multiplied by, in its corner: \node[anchor=south east, inner sep=0.01em, blue] at (mtr-1-4.south east) puts a node’s south-east corner on the cell’s south-east corner, and \scalebox{.5}{$\times 1$} (from graphicx, which TikZ loads) shrinks the text to half size. All nine nodes are named xx, which is harmless because none of them is referred to. A \foreach over the nine cells reading from the kernel would replace the nine lines, as in the first variation.
Techniques used
- Matrix of nodes three grids whose cells are named nodes, coloured one by one with
|[fill=…]|. - Anchors of named nodes
(mtr-1-4.north west)and(mtr-3-6.south east)place frames, lines and labels. - Placing nodes with positioning whole matrices placed
right=0.2em ofeach other. - Dotted lines
densely dottedconnectors between matching corners. - Neural network diagrams convolution, kernels and feature maps in CNN figures.
Variations
Our own code, written for this page and typeset with TeX Live 2026 like the figure above. Each one is a complete example: copy it, or open it in the TikZ editor and change it. This code is CC0 1.0, free to use without conditions.
The output computed from the input and the kernel
Here the numbers are data, not typed cells: the input and the kernel are pgfmath arrays, every output cell is the sum of nine products computed in nested \foreach loops, and the window position is two macros, \def\wr{2}\def\wc{1}. Change the input, the kernel or the window and the picture stays correct. Square cells 6 mm wide placed 6 mm apart share their borders exactly, so all lines have the same width.

\usepackage{tikz}\begin{tikzpicture}[x=6mm, y=-6mm, font=\small,
cell/.style={draw, minimum size=6mm, inner sep=0pt}]
% the input and the kernel as pgfmath arrays; the output is computed from them
\def\I{{{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,0,0,1,1,1,0},{0,0,0,1,1,0,0},
{0,0,1,1,0,0,0},{0,1,1,0,0,0,0},{1,1,0,0,0,0,0}}}
\def\K{{{1,0,1},{0,1,0},{1,0,1}}}
\def\wr{2}\def\wc{1} % the window's top-left cell (row, column), counted from 0
% input, with the window shaded
\fill[red!20] ({\wc-0.5},{\wr-0.5}) rectangle +(3,3);
\foreach \i in {0,...,6}
\foreach \j in {0,...,6} {
\pgfmathtruncatemacro\v{\I[\i][\j]}
\node[cell] at (\j,\i) {\v};
}
\draw[red, very thick] ({\wc-0.5},{\wr-0.5}) rectangle +(3,3);
% kernel
\foreach \a in {0,1,2}
\foreach \b in {0,1,2} {
\pgfmathtruncatemacro\v{\K[\a][\b]}
\node[cell, fill=blue!12] at (8+\b, 2+\a) {\v};
}
% output: each cell is the sum of the window times the kernel
\fill[green!25] ({12+\wc-0.5},{1+\wr-0.5}) rectangle +(1,1);
\foreach \i in {0,...,4}
\foreach \j in {0,...,4} {
\gdef\acc{0}
\foreach \a in {0,1,2}
\foreach \b in {0,1,2} {
\pgfmathtruncatemacro\ii{\i+\a}
\pgfmathtruncatemacro\jj{\j+\b}
\pgfmathtruncatemacro\tmp{\acc + \I[\ii][\jj]*\K[\a][\b]}
\global\let\acc\tmp
}
\node[cell] at (12+\j, 1+\i) {\acc};
}
\draw[green!50!black, very thick] ({12+\wc-0.5},{1+\wr-0.5}) rectangle +(1,1);
\node at (3, 7.4) {$\mathbf{I}$};
\node at (7, 3) {$*$};
\node at (9, 5.4) {$\mathbf{K}$};
\node at (11, 3) {$=$};
\node at (14, 6.4) {$\mathbf{I} * \mathbf{K}$};
\end{tikzpicture}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=6mm, y=-6mm, font=\small,
cell/.style={draw, minimum size=6mm, inner sep=0pt}]
% the input and the kernel as pgfmath arrays; the output is computed from them
\def\I{{{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,0,0,1,1,1,0},{0,0,0,1,1,0,0},
{0,0,1,1,0,0,0},{0,1,1,0,0,0,0},{1,1,0,0,0,0,0}}}
\def\K{{{1,0,1},{0,1,0},{1,0,1}}}
\def\wr{2}\def\wc{1} % the window's top-left cell (row, column), counted from 0
% input, with the window shaded
\fill[red!20] ({\wc-0.5},{\wr-0.5}) rectangle +(3,3);
\foreach \i in {0,...,6}
\foreach \j in {0,...,6} {
\pgfmathtruncatemacro\v{\I[\i][\j]}
\node[cell] at (\j,\i) {\v};
}
\draw[red, very thick] ({\wc-0.5},{\wr-0.5}) rectangle +(3,3);
% kernel
\foreach \a in {0,1,2}
\foreach \b in {0,1,2} {
\pgfmathtruncatemacro\v{\K[\a][\b]}
\node[cell, fill=blue!12] at (8+\b, 2+\a) {\v};
}
% output: each cell is the sum of the window times the kernel
\fill[green!25] ({12+\wc-0.5},{1+\wr-0.5}) rectangle +(1,1);
\foreach \i in {0,...,4}
\foreach \j in {0,...,4} {
\gdef\acc{0}
\foreach \a in {0,1,2}
\foreach \b in {0,1,2} {
\pgfmathtruncatemacro\ii{\i+\a}
\pgfmathtruncatemacro\jj{\j+\b}
\pgfmathtruncatemacro\tmp{\acc + \I[\ii][\jj]*\K[\a][\b]}
\global\let\acc\tmp
}
\node[cell] at (12+\j, 1+\i) {\acc};
}
\draw[green!50!black, very thick] ({12+\wc-0.5},{1+\wr-0.5}) rectangle +(1,1);
\node at (3, 7.4) {$\mathbf{I}$};
\node at (7, 3) {$*$};
\node at (9, 5.4) {$\mathbf{K}$};
\node at (11, 3) {$=$};
\node at (14, 6.4) {$\mathbf{I} * \mathbf{K}$};
\end{tikzpicture}
\end{document}Padding and stride
The two words that change the output size. A 5 × 5 input gets a ring of zero padding (dashed cells, found with a pgfmath test, (\i<0) || (\i>4) || …), and the 3 × 3 kernel moves two cells at a time: the red and blue frames are its first two positions, and they fill the first two cells of the 3 × 3 output. The formula beside it gives the output size for any input size n, padding p, kernel size k and stride s.

\usepackage{tikz}\begin{tikzpicture}[x=5mm, y=-5mm, font=\footnotesize,
cell/.style={draw, minimum size=5mm, inner sep=0pt}]
% a 5 x 5 input padded with one ring of zeros (rows and columns -1 to 5)
\foreach \i in {-1,...,5}
\foreach \j in {-1,...,5} {
\pgfmathtruncatemacro\pad{(\i<0) || (\i>4) || (\j<0) || (\j>4)}
\ifnum\pad=1
\node[cell, dashed, gray] at (\j,\i) {0};
\else
\node[cell, fill=gray!10] at (\j,\i) {};
\fi
}
% the first two positions of a 3 x 3 kernel moved with stride 2
\draw[red, very thick] (-1.5,-1.5) rectangle +(3,3);
\draw[blue, very thick, dashed] (0.5,-1.5) rectangle +(3,3);
% the 3 x 3 output: one cell per kernel position (the first two shaded)
\fill[red!30] (8.5,0.5) rectangle +(1,1);
\fill[blue!30] (9.5,0.5) rectangle +(1,1);
\foreach \i in {0,1,2}
\foreach \j in {0,1,2}
\node[cell] at (9+\j, 1+\i) {};
\node[align=left, anchor=west] at (7.6, 5) {$o = \left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1$\\[2pt]
$n=5,\ p=1,\ k=3,\ s=2$: $o = 3$};
\node at (2, 6.6) {input with padding $p=1$};
\node at (10, -0.9) {output};
\end{tikzpicture}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=5mm, y=-5mm, font=\footnotesize,
cell/.style={draw, minimum size=5mm, inner sep=0pt}]
% a 5 x 5 input padded with one ring of zeros (rows and columns -1 to 5)
\foreach \i in {-1,...,5}
\foreach \j in {-1,...,5} {
\pgfmathtruncatemacro\pad{(\i<0) || (\i>4) || (\j<0) || (\j>4)}
\ifnum\pad=1
\node[cell, dashed, gray] at (\j,\i) {0};
\else
\node[cell, fill=gray!10] at (\j,\i) {};
\fi
}
% the first two positions of a 3 x 3 kernel moved with stride 2
\draw[red, very thick] (-1.5,-1.5) rectangle +(3,3);
\draw[blue, very thick, dashed] (0.5,-1.5) rectangle +(3,3);
% the 3 x 3 output: one cell per kernel position (the first two shaded)
\fill[red!30] (8.5,0.5) rectangle +(1,1);
\fill[blue!30] (9.5,0.5) rectangle +(1,1);
\foreach \i in {0,1,2}
\foreach \j in {0,1,2}
\node[cell] at (9+\j, 1+\i) {};
\node[align=left, anchor=west] at (7.6, 5) {$o = \left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1$\\[2pt]
$n=5,\ p=1,\ k=3,\ s=2$: $o = 3$};
\node at (2, 6.6) {input with padding $p=1$};
\node at (10, -0.9) {output};
\end{tikzpicture}
\end{document}Frequently asked questions
How do I draw a convolution in TikZ?
Draw the input, the kernel and the output as matrices of nodes (\usetikzlibrary{matrix}), colour the window cells with |[fill=red!30]| and frame them with a rectangle from the first cell’s north west anchor to the last cell’s south east anchor. The computed variation on this page draws the whole figure from two arrays.
Why are the vertical lines thicker than the horizontal ones in my grid?
Neighbouring cells each draw their own border. row sep=-\pgflinewidth makes the horizontal borders overlap; add column sep=-\pgflinewidth for the vertical ones, or use cells of a fixed size placed exactly one cell width apart.
Is this a convolution or a cross-correlation?
What CNNs compute, and what the figure shows, is a cross-correlation: the kernel is not flipped. This kernel is symmetric under a half turn, so a true convolution gives the same values.
References
- PetarV-/TikZ at commit 86aee04: 2D Convolution/2d_convolution.tex (MIT licence)github.com
- PGF/TikZ manual, §20 Matrices and Alignmenttikz.dev
- PGF/TikZ manual, §20.3.2 Setting and Adjusting Column and Row Spacingtikz.dev
- PGF/TikZ manual, §59.1 Matrices of Nodestikz.dev
- PGF/TikZ manual, §14.4 The Rectangle Operationtikz.dev
- PGF/TikZ manual, §15.3.2 Graphic Parameters: Dash Pattern (densely dotted)tikz.dev
- PGF/TikZ manual, §17.5.3 Advanced Placement Options (positioning)tikz.dev
- PGF/TikZ manual, §88 Repeating Things: The Foreach Statementtikz.dev
- PGF/TikZ manual, §94.2 Operators (||, comparisons)tikz.dev
- PGF/TikZ manual, §94.3.8 Miscellaneous Functions (array)tikz.dev
The walkthrough was checked against these sources and the pictures are the code typeset with TeX Live 2026. The original figure is © Petar Veličković, under the MIT licence; the variations on this page are ours, CC0 1.0.
Change this figure in your browserOpen the figure or any variation in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.
Open the figure in the TikZ editor