[Tex/LaTex] Tikzpicture scope background image

graphicstikz-pgftikz-pic

I copied and modified the code given on http://www.texample.net/tikz/examples/swan-wave-model/

   \begin{figure}[H]
   \begin{tikzpicture}[scale=.5,every node/.style={minimum size=1cm},on grid]

    \begin{scope}[
            yshift=-83,every node/.append style={
            yslant=0.5,xslant=-1},yslant=0.5,xslant=-1
            ]


        % opacity to prevent graphical interference
        \fill[white,fill opacity=0.9] (0,0) rectangle (6,6);
        \draw[step=5mm, black] (0,0) grid (6,6); %defining grids
        \draw[step=1mm, red!50,thin] (0.5,0.5) grid (2,2);  %Nested Grid
        \draw[black,very thick] (0,0) rectangle (6,6);%marking borders
        \fill[red] (1,1) rectangle (1.5,1.5);
        %\includegraphics[scale=1,opacity=0.8]{Pics/face_patch}
        %Idem as above, for the n-th grid:
    \end{scope}

        \begin{scope}[
        yshift=90,every node/.append style={
        yslant=0.5,xslant=-1},yslant=0.5,xslant=-1
                     ]
        \fill[white,fill opacity=.9] (0,0) rectangle (5,5);
        \draw[step=10mm, black] (1,1) grid (4,4);
        \draw[black,very thick] (1,1) rectangle (4,4);
        \draw[red,dashed] (0,0) rectangle (5,5);
    \end{scope}

        \draw[-latex,thick](5.8,-.3)node[right]{$\mathsf{Skup\ svih\ piksela\ u\ } \boldsymbol{X}$}
        to[out=180,in=90] (3.9,-1);

    \draw[-latex,thick,text=black](5.9,5)node[right]{$\mathsf{Skup\ svih\ piksela\ u\ } \boldsymbol{x}_i$}
        to[out=180,in=90] (3.6,5);
     \draw[-latex,dashed,text=black](5.7,3)node[right]{$x_i$}
        to[out=180,in=90] (0,5.7);


    \draw[-latex,thick,red,text=black](-3.3,-3.2)node[right]{$x_i$}
        to[out=180,in=90] (0,-1.4);

    \draw[-latex,dashed,red,text=black](4.3,-1.9)node[right]{$\mathsf{Skup\ svih\ piksela\ u\ } \boldsymbol{x}_i$}
        to[out=180,in=90] (1,-1.3);

\end{tikzpicture}
\end{figure}

What i want is to insert a background picture with some opacity (so that the grid is visible as well as the underlying colors and grids/borders), so that the picture is a background of the first grid (so the image is slanted so to match the grid)

How can i do that?
Doing a pure /includegraphics didn't work at all (it didn't slate the image as well as the image size didn't match the grid size)

Best Answer

You need to place the \includegraphics inside a \node, as in the code below. I added some comments, but it's quite straightforward, mostly just place the node at the correct location, and set the proper size for the image.

enter image description here

\documentclass[border=5pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}
   \begin{tikzpicture}[scale=.5,every node/.style={minimum size=1cm},on grid]

    \begin{scope}[
            yshift=-83,every node/.append style={
            yslant=0.5,xslant=-1},yslant=0.5,xslant=-1
            ]


        % opacity to prevent graphical interference
        \fill[white,fill opacity=0.9] (0,0) rectangle (6,6);
        \draw[step=5mm, black] (0,0) grid (6,6); %defining grids
        \draw[step=1mm, red!50,thin] (0.5,0.5) grid (2,2);  %Nested Grid
        \draw[black,very thick] (0,0) rectangle (6,6);%marking borders
        \fill[red] (1,1) rectangle (1.5,1.5); 

        % use a \node to add the image
        \node[
          opacity=0.5, % set opacity for node, \includegraphics has no opacity option
          anchor=south west, % which point of the node to place at the specified coordinate
          inner sep=0pt % no padding between image and node border
          ] 
          at (0,0) % actually default, so "at (0,0)" is strictly speaking not needed
          %
          % place the includegraphics in the node
          % your grid goes from 0 to 6 with scale=0.5, so its 3cm x 3cm
          % the image I used is quadratic, so I set just the width to 3cm
           {\includegraphics[width=3cm]{example-image-1x1}}; 

    \end{scope}

        \begin{scope}[
        yshift=90,every node/.append style={
        yslant=0.5,xslant=-1},yslant=0.5,xslant=-1
                     ]
        \fill[white,fill opacity=.9] (0,0) rectangle (5,5);
        \draw[step=10mm, black] (1,1) grid (4,4);
        \draw[black,very thick] (1,1) rectangle (4,4);
        \draw[red,dashed] (0,0) rectangle (5,5);
    \end{scope}

        \draw[-latex,thick](5.8,-.3)node[right]{$\mathsf{Skup\ svih\ piksela\ u\ } \boldsymbol{X}$}
        to[out=180,in=90] (3.9,-1);

    \draw[-latex,thick,text=black](5.9,5)node[right]{$\mathsf{Skup\ svih\ piksela\ u\ } \boldsymbol{x}_i$}
        to[out=180,in=90] (3.6,5);
     \draw[-latex,dashed,text=black](5.7,3)node[right]{$x_i$}
        to[out=180,in=90] (0,5.7);


    \draw[-latex,thick,red,text=black](-3.3,-3.2)node[right]{$x_i$}
        to[out=180,in=90] (0,-1.4);

    \draw[-latex,dashed,red,text=black](4.3,-1.9)node[right]{$\mathsf{Skup\ svih\ piksela\ u\ } \boldsymbol{x}_i$}
        to[out=180,in=90] (1,-1.3);

\end{tikzpicture}
\end{document}