title: opencv matrix into shared memory
body: <p>I want to share between two linux processes a CvMat object (a matrix in the OpenCV library), for that I'm using shared memory. One process (server) will capture a frame (matrix) from the webcam, convert it to gray scale, share it using shared memory and show the frame on the screen. The other process (client) will read the shared frame and perform some operations. See the code below.</p>
  
  <p>The problem seems to be that the client doesn't read the information since 'rows' and 'cols' are zero (or the server is not writing in the shared memory). Anyway I'm not getting any error message and I don't know what I'm doing wrong. Any idea?</p>
  
  <p>Thank you very much!</p>
  
  <hr>
  
  <p>Here is the the server's code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  using namespace std;
  
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdio.h&gt;
  
  #include "2cam.h"
  
  int sizeofmat(CvMat *mat) {
      return mat-&gt;rows * mat-&gt;cols * CV_ELEM_SIZE(mat-&gt;type);
  }
  
  int main() {
      int shmid;
      key_t key = 5678;
  
      CvMat *vdisp = cvCreateMat(240, 320, CV_8U);
      const size_t vdispsize = sizeofmat(vdisp);
      CvMat *s = cvCreateMat(240, 320, CV_8U);
      CvMat stub;
      CvSize imageSize = cvSize(320, 240);
  
      IplImage *color = cvCreateImage(imageSize, 8, 3);
      IplImage *gray = cvCreateImage(imageSize, 8, 1);
  
      /* Create the segment */
      if ((shmid = shmget(key, vdispsize, IPC_CREAT | 0666)) &lt; 0) {
          perror("shmget");
          exit(1);
      }
  
      /* Attach the segment to our data space */
      if ((vdisp = (CvMat *)shmat(shmid, NULL, 0)) == (CvMat *)-1) {
          perror("shmat");
          exit(1);
      }
  
      /* Put CvMat into the memory to be read for other processes */
      s = vdisp;
  
      /* Create camera */
      Camera c("/dev/video0", 320, 240, 30);
  
      while (1) {
          /* Get one frame */
          c.Update();
          c.toIplImage(color);
  
          /* Convert color frame to grayscale */
          cvCvtColor(color, gray, CV_BGR2GRAY);
  
          /* Get matrix from the gray frame and write the matrix in shared memory*/
          s = cvGetMat(gray, &amp;stub, 0, 0);
  
          /* Show frame */
          cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
          cvShowImage("result", s);
  
          /* Wait for escape key */
          if ((cvWaitKey(10) &amp; 255) == 27)
              break;
      }
  
      /* free memory */
      cvDestroyWindow("result");
      cvReleaseImage(&amp;color);
      cvReleaseImage(&amp;gray);
      //cvReleaseMat(&amp;vdisp);
      //cvReleaseMat(&amp;s);
  
      return 0;
  }  
  </code></pre>
  
  <hr>
  
  <p>And here the client's code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  using namespace std;
  
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdio.h&gt;
  
  int sizeofmat(CvMat *mat) {
      return mat-&gt;rows * mat-&gt;cols * CV_ELEM_SIZE(mat-&gt;type);
  }
  
  int main() {
      int shmid;
      key_t key = 5678;
  
      CvMat *vdisp = cvCreateMat(240, 320, CV_8U);
      const size_t vdispsize = sizeofmat(vdisp);
      CvMat *s = cvCreateMat(240, 320, CV_8U);
  
      /* Locate the segment */
      if ((shmid = shmget(key, vdispsize, 0666)) &lt; 0) {
          perror("shmget");
          exit(1);
      }
  
      /* Now we attach the segment to our data space */
      if ((vdisp = (CvMat *)shmat(shmid, NULL, 0)) == (CvMat *) -1) {
          perror("shmat");
          exit(1);
      }
  
      s = vdisp;
  
      cout &lt;&lt; "rows: " &lt;&lt; s-&gt;rows &lt;&lt; endl;
      cout &lt;&lt; "cols: " &lt;&lt; s-&gt;cols &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>