6

Like the fig 2. on page 3 of Relations in Mathematical Morphology with applications to Graphs and Rough Sets, John G. Stell

enter image description here

Thank you!

8

Another solution with a matrix of nodes.

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[
    X/.style={draw=gray, minimum size=4mm, outer sep=0pt},
    G/.style={X, path picture={\fill[gray] (path picture bounding box.center) circle[radius=1.5mm]; }},
    BG/.style={G, fill},
    B/.style={X, fill},
    mymatrix/.style={matrix of nodes, row sep=-\pgflinewidth,
    column sep=-\pgflinewidth, nodes={X}, nodes in empty cells}
    ]

    \matrix [mymatrix, label=below:Dilation]{
    &&&&&&\\
    &&&|[G]|&|[G]|&|[G]|&\\
    &&|[G]|&|[BG]|&|[BG]|&|[G]|&\\
    &&|[BG]|&|[BG]|&|[BG]|&|[G]|&\\
    &&|[B]|&|[BG]|&|[G]|&|[G]|&\\
    &&&|[BG]|&|[BG]|&|[G]|&\\
    &&|[G]|&|[BG]|&|[BG]|&|[G]|&\\
    &&|[B]|&|[BG]|&|[BG]|&|[G]|&\\
    &&&&&&\\
    &&&&&&\\
};
\end{tikzpicture}
\end{document}

enter image description here

7

Ol' stackengine to the rescue.

Here I define \grd and \blkgrid as the basic grid cells in white and black, with gray border (I use a leading negative \kern on the grids so that adjacent cells don't produce a double-rule thickness). I give them the shorthand \w and \b, respectively. Then I create 2 stackinsets - one of a gray circle on white cell (and call it \G) and the other a grey circle on black cell (and call it \B). Then I just create a stack of the proper elements in the proper sequence.

EDITED to simplify the definitions of \grid and \blkgrid into simple \fbox definitions.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\usepackage{xcolor, graphicx}
\newsavebox{\Bbox}\newsavebox{\Gbox}
\def\thk{.5pt}                     % RULE THICKNESS
\def\gsize{.4cm}                 % GRID SIZE
\def\coresize{\dimexpr\gsize-2\dimexpr\thk}
\def\grid{\kern-\thk\fboxsep=.5\coresize\relax%
  \fboxrule=\thk\relax\textcolor{gray}{\fbox{}}}
\def\blkgrid{\kern-\thk\fboxsep=0pt\relax\fboxrule=\thk\relax%
  \textcolor{gray}{\fbox{\textcolor{black}{\rule[-.5\coresize]{\coresize}{\coresize}}}}}
\sbox\Gbox{\stackinset{c}{.0pt}{c}{-.4pt}{\scalebox{2}{\color{gray}$\bullet$}}{\grid}}
\sbox\Bbox{\stackinset{c}{.0pt}{c}{-.4pt}{\scalebox{2}{\color{gray}$\bullet$}}{\blkgrid}}
\def\G{\usebox{\Gbox}}\def\B{\usebox{\Bbox}}\def\w{\grid}\def\b{\blkgrid}
\setstackgap{S}{-\thk}
\begin{document}
\stackengine{3pt}{\Shortstack{%
      \w\w\w\w\w\w\w\\
      \w\w\w\G\G\G\w\\
      \w\w\G\B\B\G\w\\
      \w\w\B\B\B\G\w\\
      \w\w\b\B\G\G\w\\
      \w\w\w\B\B\G\w\\
      \w\w\G\B\B\G\w\\
      \w\w\b\B\B\G\w\\
      \w\w\w\w\w\w\w\\
      \w\w\w\w\w\w\w      
    }}{Dilation}{U}{c}{F}{F}{S}
\end{document}

enter image description here

Incidentally, I stole virtually all of my code from Any package for matrix diagram?, and adapted it.

6

Here is an approach using tikz:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\cellsize}{0.5}
\newcommand{\circlesize}{0.35}

\newcommand{\printwhitecell}[2][]{\node[draw=gray, semithick, fill=white, minimum width=\cellsize cm, minimum height=\cellsize cm] at #2 {#1};} 
\newcommand{\printblackcell}[1]{\node[draw=gray, semithick, fill=black, minimum width=\cellsize cm, minimum height=\cellsize cm] at #1 {};} 
\newcommand{\printcircle}[1]{\draw[gray, semithick, fill=gray] #1 circle (\circlesize cm/2);} 

\newcommand{\printcell}[2]{%
    \if#1w%
        \printwhitecell{#2}%
    \else%
        \if#1b%
            \printblackcell{#2}%
        \else%
            \if#1c%
                \printwhitecell{#2}%
                \printcircle{#2}%
            \else%
                \if#1C%
                    \printblackcell{#2}%
                    \printcircle{#2}%
                \else
                    \printwhitecell[#1]{#2}%
                \fi%
            \fi%
        \fi%
    \fi%
}

\newcommand{\printmatrix}[2]{
    \begin{tikzpicture}
    \foreach \line[count=\i] in #2 {
        \foreach \cell[count=\j] in \line {
            \printcell{\cell}{(\j*\cellsize,-\i*\cellsize)}
        }
        \xdef\width{\j}
    }
    \node at ({0.5*(\width+1)*\cellsize}, {(-\i -1)*\cellsize}) {#1};
    \end{tikzpicture}   
}

\begin{document}
    \printmatrix{Dilation}{{
            {w,w,w,w,w,w,w},
            {w,w,w,c,c,c,w},
            {w,w,c,C,C,c,w},
            {w,w,C,C,C,c,w},
            {w,w,b,C,c,c,w},
            {w,w,w,C,C,c,w},
            {w,w,c,C,C,c,w},
            {w,w,b,C,C,c,w},
            {w,w,w,w,w,w,w},
            {w,w,w,w,w,w,w}% this % is needed
        }}
    \printmatrix{E}{{{b,b},{0,b}}}
\end{document}

The matrix

Edit: An slightly different approach:

\def\forlines#1 #2{%
    \i=0
    \forcells#1\relax\advance\j 1\relax%
\ifx\relax#2\else%
\expandafter\expandafter\expandafter\forlines\expandafter#2\fi%
}

\def\forcells#1{%
    \ifx\relax#1\else\printcell{#1}{(\i*\cellsize,-\j*\cellsize)}\advance\i 1\relax\expandafter\forcells\fi
}

\newcommand{\printmatrixb}[2]{
    \begin{tikzpicture} 
        \newcount\i
        \newcount\j
        \i=0
        \j=0
        \forlines#2 \relax

    \node at ({0.5*(\i-1)*\cellsize}, {(-\j)*\cellsize}) {#1};
    \end{tikzpicture}   
}

Now, the matrix can be defined with a less noisy syntax:

\printmatrixb{Dilation}{
            wwwwwww
            wwwcccw
            wwcCCcw
            wwCCCcw
            wwbCccw
            wwwCCcw
            wwcCCcw
            wwbCCcw
            wwwwwww
            wwwwwww
    }
    \printmatrixb{E}{bb 0b}

Of course one could introduce arbitrary delimiters instead of whitespace.

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.