How to run IPC for two application using single camera for Iplimage [closed]

asked 2019-01-02 07:58:04 -0600

prabhakarMP gravatar image

updated 2019-01-02 07:59:21 -0600

Pls find my Server and client and please let me know how to do the same for 3 channel image in Iplimage format.

Server code to store in SHM

#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;
}

Client code to retrieve from SHM

#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;
}

This code is working for grayscale and size of 320X240. Please help me to do the same for 3 channel image of 640X480.

Thanks and regards, Prabhakar M

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by berak
close date 2019-01-02 08:11:25.038719

Comments

1

the c-api you're trying to use was deprecated in 2010 already. you must NOT write any new code like this.

berak gravatar imageberak ( 2019-01-02 08:12:13 -0600 )edit

Absouletly, berak.

supra56 gravatar imagesupra56 ( 2019-01-02 09:21:12 -0600 )edit