【问题标题】:opencv matrix into shared memoryopencv矩阵到共享内存
【发布时间】:2010-10-11 13:03:42
【问题描述】:

我想在两个 linux 进程之间共享一个 CvMat 对象(OpenCV 库中的一个矩阵),因为我正在使用共享内存。一个进程(服务器)将从网络摄像头捕获帧(矩阵),将其转换为灰度,使用共享内存共享它并在屏幕上显示该帧。另一个进程(客户端)将读取共享帧并执行一些操作。请参阅下面的代码。

问题似乎是客户端没有读取信息,因为“行”和“列”为零(或者服务器没有写入共享内存)。无论如何,我没有收到任何错误消息,我不知道我做错了什么。有什么想法吗?

非常感谢!


这是服务器的代码:

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

这里是客户的代码:

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

【问题讨论】:

  • 你不能用线程来代替吗?
  • 是的!没问题,但这是一个个人项目,所以我决定使用共享内存来了解它是如何工作的。顺便谢谢你的建议。
  • 如果你想要它的功能和速度,只需使用 gstreamer 的 shmsrcshmsink(OpenCV 示例:github.com/tik0/mat2gstreamer

标签: c opencv shared-memory


【解决方案1】:

感谢 larsmans,他为我指明了正确的方向。无论如何,我自己回答以防万一有人需要相同的解决方案。


这是服务器的代码:

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

这里是客户的代码:

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

【讨论】:

  • 好的,但是如果发送3通道图像(不是灰度图像),你怎么做呢?
  • 我知道它很旧,但根据shmctl man page,您必须清理您使用 shmget 创建的所有 shm 段。 (例如 shmctl(shmid, IPC_RMID, NULL)
【解决方案2】:

如果您使用的是 Boost Interprocess,应该是这样的。

if(argv[1] = SERVER){

  struct shm_remove{
    shm_remove() {shared_memory_object::remove("SharedMemoryForCvMat"); }
    ~shm_remove(){shared_memory_object::remove("SharedMemoryForCvMat"); }
  }remover;

  shared_memory_object shm (create_only,"SharedMemoryForCvMat",read_write);
  shm.truncate(widht*height*channel);
  mapped_region region(shm, read_write);

  cv::VideoCapture cap(0);
  cv::Mat frame;

  while(everything_is_ok_for_server){
    cap >> frame;
    memcpy(region.get_address(), frame.data, width*height*channel);   
  }
}

else if(argv[1] == CLIENT){

    shared_memory_object shm (open_only, "SharedMemoryForCvMat", read_only);
    mapped_region region(shm, read_only);

    while(everything_is_ok_for_client){

        cv::Mat frame(cv::Size(width, height),
                      CV_8UC3,
                      region.get_address(),
                      cv::Mat::AUTO_STEP);
        cv::imshow("unimportant_string",frame);
        cv::waitKey(2);
    }
}

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 2021-05-11
    • 2017-04-06
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2017-03-29
    • 1970-01-01
    相关资源
    最近更新 更多