opencv矩阵存入共享内存

2024-03-08

我想在两个 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;
}

感谢拉斯曼斯,他为我指明了正确的方向。不管怎样,我自己回答,以防万一有人需要相同的解决方案。


这是服务器的代码:

#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

opencv矩阵存入共享内存 的相关文章

随机推荐

  • 为变量名称添加前缀以指示它们各自的范围或来源?

    在我工作过的公司中 我见过很多使用前缀来指示变量的范围或来源的情况 例如m对于班级成员来说 i对于方法内部变量和a or p 对于方法参数 public class User private String mUserName public
  • Android 媒体播放器错误 (-19, 0)

    我尝试在单击按钮时重播声音 但我得到错误 19 0 这意味着什么 My code final Button xxx Button findViewById R id xxx xxx setOnClickListener new View O
  • Azure 网站和 Sass

    我一直在尝试寻找是否有一种方法可以通过 windows azure 网站支持 sass 有人可以向我指出一些文档吗 或者如果可能的话现在就让我知道 我特别希望支持祖布基金会 http foundation zurb com index ht
  • Pandas:如果预定义列表中不存在,则将列值替换为空

    我有一个清单 X 其中包含列的一组合法值 说吧 我有专栏A 我想替换 设置 为空字符串 中的元素df A 如果它们的值不在 X 中 我怎样才能在 Pandas 中有效地做到这一点 我知道有isin 但这只是检查值是否存在并返回一系列 Tru
  • 通过Windows中的命令行工具合并两个png?

    我正在寻找一个可以通过命令行 Windows 7 使用的工具来合并两个相同大小的 png 到目前为止 我在搜索方面运气不佳 因为我见过的大多数工具都是相当笨重的应用程序 它们似乎不针对简单的操作 我想用覆盖图像中的非透明像素覆盖不透明基本图
  • 情节 orca 无法在 aws ec2 实例上工作

    我跟着this https github com plotly orca安装conda plotly orca在 AWS EC2 实例上 构建于anaconda python3图像 但击中Cannot open shared object调
  • WIX如何从自定义操作访问源文件

    我有一个 WIX 安装应用程序和许多源文件
  • 如何防止行高在顶部添加边距?

    每当我使用大line height like 1 6em它总是在文本的最顶部添加我不想要的边距 例子 http jsfiddle net EstpJ 1 http jsfiddle net EstpJ 1 我希望文本的边框清晰 并且没有任何
  • 分布式erlang安全如何?

    我想要有 2 个独立的 erlang 节点可以相互通信 so node a myhost将能够发送消息至b myhost 有没有办法限制节点a myhost 所以只有来自 a 的函数安全模块可以被召唤b myhost 它应该是这样的 a m
  • twitter bootstrap - 背景颜色

    我正在尝试更改引导程序的背景颜色和不透明度 我有以下 CSS well opacity 0 9 opacity 0 1 moz opacity 0 9 opacity 0 1 webkit opacity 0 9 opacity 0 1 b
  • CodeIgniter 中视图中的 $this 关键字

    我试图理解如何 this gt load gt view works insideCodeIgniter 中的视图文件的一部分 core Controller php 正在调用 core Loader php 然后调用 ci load 后者
  • 读取thunderbird地址mab文件内容

    我的 TBIRD 地址簿上有几个地址列表 每次我需要编辑包含在多个列表中的地址时 查找哪个列表包含要修改的地址都是一件很痛苦的事情 作为一个帮助工具 我想阅读几个文件 并只给用户一个列表 xxx MAB 文件仅包含一次搜索的搜索地址 有了生
  • 在 R 中将字符串拆分为新行[重复]

    这个问题在这里已经有答案了 我有一个如下数据集 Country Region Molecule Item Code IND NA PB102 FR206985511 THAI AP PB103 BA 107603 F000113361 10
  • PHPUnit、接口和命名空间 (Symfony2)

    我目前正在为 Symfony2 开发一个开源包 并且真的希望它在单元测试覆盖率和一般可靠性方面成为最优秀的 但是由于我缺乏 PHPUnit 知识 或复杂的场景 谁知道 目前 我有一个 Mailer 类 用于处理个人邮件场景 它看起来有点像这
  • 找不到 docker 命令

    我在Mac上安装了docker 安装成功 它还正在运行 并尝试在终端中执行以下命令 docker v 请参阅以下错误 bash docker command not found 我该如何解决这个问题 在 Windows 中这些命令运行良好
  • 需要指南针陀螺仪帮助

    我需要一个游戏对象指向北方 并且我想将其与 gyro attitude 输入结合起来 我曾尝试一步完成此任务 但没有成功 也就是说 我无法制作任何我在网上找到的陀螺仪脚本 以满足始终指向北方的额外要求 相信我 我已经尝试了所有能找到的关于这
  • Angular2 RC6 - 在我的子模块中导入 BrowserModule

    I have DashboardModule导入到我的根目录中AppModule 在组件模板中DashboardModule I use ngFor 它声明于BrowserModule 正是在CommonModule进口者BrowserMo
  • database.yml 中的 pool 选项有什么用

    database yml 中最广泛使用的选项如下 adapter encoding database pool username password socket host port timeout 上面大部分的用法我都知道 除了pool 所
  • 如何将一个数字分成多个部分,使结果之和等于输入?

    我试图将一个数字分成多个部分 以便各部分的总和等于输入数字 如果我有 3 99 并且需要分成两部分 则预期输出为 2 和 1 99 2 1 99 3 99 如果我需要将 3 99 分成 3 部分 则预期输出为 1 3 1 3 和 1 39
  • opencv矩阵存入共享内存

    我想在两个 Linux 进程之间共享一个 CvMat 对象 OpenCV 库中的矩阵 为此我使用共享内存 一个进程 服务器 将从网络摄像头捕获一帧 矩阵 将其转换为灰度 使用共享内存共享它并在屏幕上显示该帧 另一个进程 客户端 将读取共享帧