基本思想:最近在研究RoboMater源码,学习了如何使用共享内存传递cv::Mat 所以记录一下;

send.cpp 读取了一张576*768*3通道的图片

#include <iostream>
#include <sys/shm.h>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"#define image_size_max 576*768*3
struct shareMemory {int pixel_rows;int pixel_cols;int pixel_channels;char pixel[image_size_max];
};using namespace cv;
using namespace std;struct shareMemory *get_shared_memory() {int shmid = shmget((key_t) 2232, sizeof(struct shareMemory), 0666 | IPC_CREAT);if (shmid == -1) {fprintf(stderr, "shmget failed\n");exit(EXIT_FAILURE);}//将共享内存连接到当前进程的地址空间void *shm = shmat(shmid, NULL, 0);if (shm == (void *) -1) {fprintf(stderr, "shmat failed\n");exit(EXIT_FAILURE);}struct shareMemory *shared = (struct shareMemory *) shm;return shared;}char *Mat_to_array(Mat Image) {char *pBuffet = new char[Image.cols * Image.rows * 3];if (Image.isContinuous()) {memcpy(pBuffet, Image.data, Image.cols * Image.rows * 3);} else {for (int i = 0; i < Image.rows; ++i) {memcpy(pBuffet, Image.ptr<uchar>(i), Image.cols * 3);}}return pBuffet;
}int main() {Mat Image = imread("/home/ubuntu/Downloads/dog.jpg");struct shareMemory *shareMemory = get_shared_memory();char *image=(char*)Image.data;shareMemory->pixel_rows = Image.rows;shareMemory->pixel_cols = Image.cols;shareMemory->pixel_channels = Image.channels();memcpy(shareMemory->pixel,image,Image.rows*Image.cols*3);std::cout << "width=" << Image.rows << endl<< "height=" << Image.cols << endl<< "channel=" << Image.channels() << endl;return 0;
}

接收端接收到了共享内存的图片了

rec.cpp

#include <iostream>
#include <sys/shm.h>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"#define image_size_max 576*768*3
struct shareMemory {int pixel_rows;int pixel_cols;int pixel_channels;char pixel[image_size_max];
};using namespace cv;
using namespace std;
struct shareMemory *get_shared_memory()
{int shmid = shmget((key_t)2232, sizeof(struct shareMemory), 0666|IPC_CREAT);if(shmid == -1){fprintf(stderr, "shmget failed\n");exit(EXIT_FAILURE);}//将共享内存连接到当前进程的地址空间void *shm = shmat(shmid, NULL, 0);if(shm == (void*)-1){fprintf(stderr, "shmat failed\n");exit(EXIT_FAILURE);}struct shareMemory *shared = (struct shareMemory*)shm;return shared;}
int main() {struct shareMemory *shareMemory=get_shared_memory();cv::Mat cvoutImg = cv::Mat(shareMemory->pixel_rows,shareMemory->pixel_cols,CV_8UC3,cv::Scalar(255, 255, 255));//bufHight,bufWidthstd::cout << "width=" << shareMemory->pixel_rows<< endl<< "height=" << shareMemory->pixel_cols << endl<< "channel=" << shareMemory->pixel_channels << endl;memcpy((char*)cvoutImg.data, shareMemory->pixel,shareMemory->pixel_rows*shareMemory->pixel_cols*shareMemory->pixel_channels);cv::imshow("xx",cvoutImg);cv::waitKey(0);return 0;
}

来吧 让我看看这只狗

我们愉快的上段视频流吧

send.cpp

#include <iostream>
#include <sys/shm.h>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"#define image_size_max 720*1280*3
struct shareMemory {int pixel_rows;int pixel_cols;int pixel_channels;char pixel[image_size_max];
};using namespace cv;
using namespace std;struct shareMemory *get_shared_memory() {int shmid = shmget((key_t) 2332, sizeof(struct shareMemory), 0666 | IPC_CREAT);if (shmid == -1) {fprintf(stderr, "shmget failed\n");exit(EXIT_FAILURE);}//将共享内存连接到当前进程的地址空间void *shm = shmat(shmid, NULL, 0);if (shm == (void *) -1) {fprintf(stderr, "shmat failed\n");exit(EXIT_FAILURE);}struct shareMemory *shared = (struct shareMemory *) shm;return shared;}char *Mat_to_array(Mat Image) {char *pBuffet = new char[Image.cols * Image.rows * 3];if (Image.isContinuous()) {memcpy(pBuffet, Image.data, Image.cols * Image.rows * 3);} else {for (int i = 0; i < Image.rows; ++i) {memcpy(pBuffet, Image.ptr<uchar>(i), Image.cols * 3);}}return pBuffet;
}int main() {VideoCapture cap;cap.open(0); //打开视频,以上两句等价于VideoCapture cap("E://2.avi");Mat frame;while(1){cap>>frame;//等价于cap.read(frame);if(frame.empty())//如果某帧为空则退出循环break;struct shareMemory *shareMemory = get_shared_memory();char *image=(char*)frame.data;shareMemory->pixel_rows = frame.rows;shareMemory->pixel_cols = frame.cols;shareMemory->pixel_channels = frame.channels();memcpy(shareMemory->pixel,image,frame.rows*frame.cols*3);std::cout << "width=" << frame.rows << endl<< "height=" << frame.cols << endl<< "channel=" << frame.channels() << endl;}cap.release();//释放资源return 0;
}

rec.cpp

#include <iostream>
#include <sys/shm.h>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"#define image_size_max 720*1280*3
struct shareMemory {int pixel_rows;int pixel_cols;int pixel_channels;char pixel[image_size_max];
};using namespace cv;
using namespace std;
struct shareMemory *get_shared_memory()
{int shmid = shmget((key_t)2332, sizeof(struct shareMemory), 0666|IPC_CREAT);if(shmid == -1){fprintf(stderr, "shmget failed\n");exit(EXIT_FAILURE);}//将共享内存连接到当前进程的地址空间void *shm = shmat(shmid, NULL, 0);if(shm == (void*)-1){fprintf(stderr, "shmat failed\n");exit(EXIT_FAILURE);}struct shareMemory *shared = (struct shareMemory*)shm;return shared;}
int main() {while(1){struct shareMemory *shareMemory=get_shared_memory();cv::Mat cvoutImg = cv::Mat(shareMemory->pixel_rows,shareMemory->pixel_cols,CV_8UC3,cv::Scalar(255, 255, 255));//bufHight,bufWidthstd::cout << "width=" << shareMemory->pixel_rows<< endl<< "height=" << shareMemory->pixel_cols << endl<< "channel=" << shareMemory->pixel_channels << endl;memcpy((char*)cvoutImg.data, shareMemory->pixel,shareMemory->pixel_rows*shareMemory->pixel_cols*shareMemory->pixel_channels);if(!cvoutImg.data)break;cv::imshow("xx",cvoutImg);cv::waitKey(1);}return 0;
}

48、Linux共享内存传递cv::Mat相关推荐

  1. linux的共享内存,linux共享内存实际在哪里?

    我只想知道共享内存驻留在Linux系统中的位置?它在物理内存还是虚拟内存中?linux共享内存实际在哪里? 我知道有关进程的虚拟内存发送信箱,他们从不同的工艺处理和流程没有看到对方的记忆,但我们可以利 ...

  2. Linux共享内存(二)

    Linux共享内存编程实例 原文链接:http://blog.csdn.net/pcliuguangtao/article/details/6526119 /*共享内存允许两个或多个进程进程共享同一块 ...

  3. c++ 共享内存_关于Linux共享内存的实验 [二] - 原因

    关于Linux共享内存的实验 [一] 上文采用的"删文件"和"杀进程"的方法主要是为了快速演示实验现象,但这种做法不利于通过调试手段进一步探究其内在的逻辑.为此 ...

  4. 【Linux共享内存】

    Linux共享内存 一.基本概念 二.常用函数 1. shm_open 2. mmap 3. munmap 4. shm_unlink 5. ftruncate 三.使用示例 四.share内存不足解 ...

  5. linux 共享内存操作(shm_open、mmap、编译链接库:-lz -lrt -lm -lc都是什么库)

    文章目录 linux 共享内存操作(shm_open) 一.背景 二.函数使用说明 shm_open ftruncate(改变文件大小) mmap共享内存 三.示例代码 创建内存共享文件并写入数据 打 ...

  6. linux 共享内存_盘点那些linux 后台开发类常见问题及知识点

    一.linux和os: netstat :显示网络状态 tcpdump:主要是截获通过本机网络接口的数据,用以分析.能够截获当前所有通过本机网卡的数据包.它拥有灵活的过滤机制,可以确保得到想要的数据. ...

  7. linux共享内存示例,linux 进程间共享内存示例

    写入端: #include #include #include #include #include using namespace std; struct MappingDataType { int ...

  8. Linux共享内存(二) (转载)

    1 /*共享内存允许两个或多个进程进程共享同一块内存(这块内存会映射到各个进程自己独立的地址空间) 2 从而使得这些进程可以相互通信. 3 在GNU/Linux中所有的进程都有唯一的虚拟地址空间,而共 ...

  9. 世上最好的共享内存(Linux共享内存最透彻的一篇)上集

    共享单车.共享充电宝.共享雨伞,世间的共享有千万种,而我独爱共享内存. 早期的共享内存,着重于强调把同一片内存,map到多个进程的虚拟地址空间(在相应进程找到一个VMA区域),以便于CPU可以在各个进 ...

最新文章

  1. linux设置开机服务自动启动/关闭自动启动命令
  2. java 内存分析之jmap 详细用法完整版(一)
  3. Phalanger---PHP的.NET编译器
  4. 总结:JDK1.5-JDK1.8各个新特性
  5. 阿里前CEO卫哲的万字长文:被马云骂醒,看透B2B 10大核心问题!
  6. arcgis在面内创建随机点
  7. 南方科技大学-计算智能与先进制造方向-博士-博士后-研究助理招聘
  8. 【多线程】LockSupport 使用 原理 源码 分析
  9. luogu_P3345[zjoi2015]幻想乡战略游戏
  10. C++ 推断进程是否存在
  11. CCF 201809-2 买菜
  12. ssm框架中mysql的分页_SSM框架中mapper层,增删改查,如何实现
  13. 创建一个简单的MFC程序
  14. C# 通过api 下载sharepoint中的文件
  15. as常用固定搭配_常用的有以下固定搭配
  16. Laravel将Word文档转化为pdf文件
  17. html字体铺盖颜色,买被子也是有讲究的?这几种颜色的被子,再好看都别往卧室放!...
  18. 学习笔记:Java 并发编程①_基础知识入门
  19. Angular入门学习笔记
  20. 一篇文章普及各种ios基本知识

热门文章

  1. Derek Sivers:别像头驴(译)
  2. Flutter 图片保存到本地
  3. 实例讲解linux用户,组,文件目录权限
  4. 如何在手机上编码python_教你如何使用Python向手机发送通知
  5. redis分布式缓存应用—五大数据类型:key/String/Hash/List/Set/Zset,配置文件redis.conf解析
  6. 论文查重的标准是怎么样的?
  7. Iterator-遍历器(迭代器)
  8. HP服务器p840raid信息导入,HPE Proliant Gen9 Server P840阵列卡 安装Solaris11 加载阵列卡驱动...
  9. webStorm 打包Vue项目
  10. 王者荣耀皮肤获取很难?用Python教你轻松获取