基本思想:最近在研究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. 面试官问你MyBatis SQL是如何执行的?把这篇文章甩给他
  2. linux fls函数,linux常用操作命令
  3. 掌握这10个Python小技巧,让你敲代码速度快5倍不止
  4. Spring整合Redis时报错:java.util.NoSuchElementException: Unable to validate object
  5. python3 getopt用法
  6. IntelliJ IDEA给Serializable类加上自动的serialVersionUID
  7. Java定义全局变量的方法
  8. 关于在Winphone中使用Google Map的问题(徐林峰)
  9. java 2015001_hbase的Java操作
  10. 微信小程序框架介绍以及项目目录结构
  11. 如何选择深度学习的GPU
  12. 【算法导论-36】并查集(Disjoint Set)具体解释
  13. 台式计算机没有声音怎么办,台式电脑没声音怎么回事_台式机电脑没有声音如何解决...
  14. No version of NDK matched the requested version xxx 问题解决
  15. ear的英语怎么念_ears用英语怎么读?
  16. rtx3080和rtx3080ti性能差距 rtx3080和rtx3080ti 参数对比哪个好
  17. 几百块的投影仪靠谱吗?性能怎么样?
  18. 英国经济学专业哪些院校比较好?
  19. 利用临时二维码实现在电脑浏览器上的微信扫码登陆功能 - EasyWeChat版
  20. DirectX游戏开发之3D角色动起(下)

热门文章

  1. GitHub在Visual Studio 2015中获得TFS/VSO同等地位
  2. 跳转dialog主题activity 界面屏幕抖动解决
  3. 学计算机 数学日记,我的数学日记
  4. AUTODESK.SIMULATION.MOLDFLOW.DESIGN.LINK.2013-MAGNiTUDE
  5. 网络爬虫CSS选择器详细讲解
  6. 集合 List 分片的 5 种实现
  7. java恶魔之怒太平洋_RTX2060黑金之恶魔狩猎 怒闯地狱斩恶魔
  8. 2017年8月前端开发者超实用干货大合集
  9. 从互联网访问公司内网
  10. 数据驱动从方法到实践pdf_我从数据驱动设计方法中学到的东西