views:

39

answers:

2

Hi!

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.

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?

Thank you very much!


Here is the the server's code:

#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#include "2cam.h"

int sizeofmat(CvMat *mat) {
    return mat->rows * mat->cols * CV_ELEM_SIZE(mat->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)) < 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, &stub, 0, 0);

        /* Show frame */
        cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
        cvShowImage("result", s);

        /* Wait for escape key */
        if ((cvWaitKey(10) & 255) == 27)
            break;
    }

    /* free memory */
    cvDestroyWindow("result");
    cvReleaseImage(&color);
    cvReleaseImage(&gray);
    //cvReleaseMat(&vdisp);
    //cvReleaseMat(&s);

    return 0;
}  

And here the client's code:

#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

int sizeofmat(CvMat *mat) {
    return mat->rows * mat->cols * CV_ELEM_SIZE(mat->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)) < 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 << "rows: " << s->rows << endl;
    cout << "cols: " << s->cols << endl;

    return 0;
}
A: 

AFAICT, this is the error:

/* Get matrix from the gray frame and write the matrix in shared memory*/
s = cvGetMat(gray, &stub, 0, 0);

This is not putting anything in shared memory. This stores a new pointer value in s, forgetting its old value.

But fixing that probably won't solve the problem. What you're trying to share seems to be the CvMat structure. You should be sharing both that and the area its data member points to.

larsmans
thanks dude! you pointed me the problem and now I understand a lit better shared memory. Anyway, right now the code just shares the data segment, the matrix header is not shared since I don't really care about it (its gonna be always the same).
Victor Gaspar
Please vote for this answer if it was useful. Voting is considered courtesy, and you have voted 0 times so far.
larsmans
A: 

Thanks to larsmans, that he pointed me in the right direction. Anyway, I answered myself just in case someone need the same solution.


Here it is the server'S code:

#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#include "2cam.h"

int sizeofmat(CvMat *mat) {
    return mat->rows * mat->step;
}

int main() {
    int shmid;
    key_t key = 5678;

    uchar *vdisp;
    CvMat *s = cvCreateMat(240, 320, CV_8U);
    CvMat *tmp = cvCreateMat(240, 320, CV_8U);
    const size_t vdispsize = sizeofmat(s);
    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)) < 0) {
        perror("shmget");
        exit(1);
    }

    /* Attach the segment to our data space */
    if ((vdisp = (uchar *) shmat(shmid, NULL, 0)) == (uchar *) -1) {
        perror("shmat");
        exit(1);
    }

    s->data.ptr = 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*/
        tmp = cvGetMat(gray, &stub, 0, 0);

        for (int row = 0; row < tmp->rows; row++) {
            const uchar* ptr = (const uchar*) (tmp->data.ptr + row * tmp->step);
            memcpy((uchar*)(s->data.ptr + row * s->step), ptr, tmp->step);
        }

        /* Show frame */
        cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
        cvShowImage("result", s);

        /* Wait for escape key */
        if ((cvWaitKey(10) & 255) == 27)
            break;
    }

    /* free memory */
    cvDestroyWindow("result");
    cvReleaseImage(&color);
    cvReleaseImage(&gray);

    return 0;
}

Here the client'S code:

#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

int sizeofmat(CvMat *mat) {
    return mat->rows * mat->step;
}

int main() {
    int shmid;
    key_t key = 5678;

    uchar *vdisp;
    CvMat *s = cvCreateMat(240, 320, CV_8U);
    const size_t vdispsize = sizeofmat(s);

    /* Locate the segment */
    if ((shmid = shmget(key, vdispsize, 0666)) < 0) {
        perror("shmget");
        exit(1);
    }

    /* Now we attach the segment to our data space */
    if ((vdisp = (uchar *)shmat(shmid, NULL, 0)) == (uchar *) -1) {
        perror("shmat");
        exit(1);
    }

    s->data.ptr = vdisp;

    cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
    cvShowImage("result", s);

    cvWaitKey(10000);

    cvDestroyWindow("result");

    return 0;
}
Victor Gaspar