title: Raspberry Pi - Autostart OpenCv-Script - Error with cv::imshow()
body: <p><strong>Short Description:</strong></p>
  
  <p>I want to auto-start an executable (opencv binary file, generate via c++) via a systemd service-script after booting, but I am unsuccessful. </p>
  
  <p>I narrowed down the error to the code statement "cv::imshow(....)" which opens a window and displays an image. At this point, the code throws the error: "QXcbConnection: Could not connect to display"</p>
  
  <p>However, if I manually execute the sh-script or the binary, both work fine. I searched around stackoverflow for the most common errors, and I tried to fix all I could found. I am quite sure, that:</p>
  
  <ol>
  <li>My service file actually runs at start (until the error occurred)</li>
  <li>Manually execution of the binary file works fine</li>
  <li>Manually execution of the .sh-script works fine</li>
  <li>I do not have runtime-linking errors (see .sh-script)</li>
  </ol>
  
  <p>I would appreciate any help. Please help me fix the error, and please explain to me, why this error even occurs in the first place. Thanks a lot :)</p>
  
  <p>.</p>
  
  <p><strong>My system:</strong> </p>
  
  <pre><code>Machine: Raspberry Pi 3 Model B
  Architecture: arm32 / ARMv7
  OS: NOOBS
  </code></pre>
  
  <p>.</p>
  
  <p><strong>My script in /etc/systemd/system/ (test.service):</strong></p>
  
  <pre><code>[Unit]
  Description=lalala
  
  [Service]
  Type=oneshot
  ExecStart=/bin/bash "/home/pi/Desktop/test.sh" start
  ExecStop=/bin/bash "/home/pi/Desktop/test.sh" stop
  RemainAfterExit=yes
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>Moreover, I did execute the following commands:</p>
  
  <pre><code>sudo chmod u+rwx /etc/systemd/system/test.service
  
  sudo systemctl enable test
  </code></pre>
  
  <p>And if I start the service manually, it runs with the same error output as while autostarting during the boot process:</p>
  
  <pre><code>sudo systemctl enable test
  </code></pre>
  
  <p>.</p>
  
  <p><strong>My shell script (test.sh):</strong></p>
  
  <pre><code>#!/bin/sh -e
  
  exec 2&gt; /tmp/test.sh.log       # send stderr to a log file
  exec 1&gt;&amp;2                      # send stdout to the same log file
  set -x                         # tell sh to display commands before execution
  
  
  echo "in script"
  
  
  start() 
  {
      echo "in start"
  
      sleep 30
  
      LD_LIBRARY_PATH=/usr/local/OpenCV/lib:/usr/local/SFML/lib:/usr/local/curl/lib:$LD_LIBRARY_PATH
      export LD_LIBRARY_PATH
  
      /home/pi/Desktop/test/main -e &amp;
  }
  
  
  # THE OTHER CASES, NOT PUT IN HERE (stop, status)
  
  
  case "$1" in
      start)
          start
          ;;
      stop)
          stop
          ;;
      status)
          status 
          ;;
      restart)
          stop
          start
          ;;
      *)
          echo "Usage:  {start|stop|status|restart}"
          exit 1
          ;;
  esac
  
  exit 0
  </code></pre>
  
  <p>.</p>
  
  <p><strong>Minimal example of my source code: (executable)</strong></p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2/core.hpp&gt;
  #include &lt;opencv2/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  
  int main()
  {
      cv::Mat frame;
      cv::namedWindow("result", cv::WINDOW_NORMAL);
  
      ## CRASH
  
      return 0;
  }
  </code></pre>
  
  <p>.</p>
  
  <p><strong>P.S:</strong></p>
  
  <p>I am aware that there is a similar thread like this (<a href="https://stackoverflow.com/questions/42890467/run-opencv-script-on-start-with-imshow">Run OpenCV script on start with imshow</a>). But as there is no solution for this question, and as I have more information to share, I thought it would be more appropriate to start a new thread.</p>