电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

python和C++共享內存傳輸圖像的示例

瀏覽:4日期:2022-07-07 08:38:42

原理

python沒有辦法直接和c++共享內存交互,需要間接調用c++打包好的庫來實現

流程

C++共享內存打包成庫 python調用C++庫往共享內存存圖像數據 C++測試代碼從共享內存讀取圖像數據

實現

1.c++打包庫

創建文件

python和C++共享內存傳輸圖像的示例

example.cpp

#include <iostream>#include <cassert>#include <stdlib.h>#include <sys/shm.h>#include 'opencv2/core.hpp'#include 'opencv2/imgproc.hpp'#include 'opencv2/highgui.hpp'#include 'opencv2/videoio.hpp' #define key 650#define image_size_max 1920*1080*3 using namespace std;using namespace cv; typedef struct{int rows;int cols;uchar dataPointer[image_size_max];}image_head; int dump(int cam_num,int row_image, int col_image, void* block_data_image){ int shm_id = shmget(key+cam_num,sizeof(image_head),IPC_CREAT); if(shm_id == -1) { cout<<'shmget error'<<endl; return -1; } cout << ' shem id is '<<shm_id<<endl; image_head *buffer_head; buffer_head = (image_head*) shmat(shm_id, NULL, 0); if((long)buffer_head == -1) { cout<<'Share memary can’t get pointer'<<endl; return -1; } assert(row_image*col_image*3<=image_size_max); image_head image_dumper; image_dumper.rows=row_image; image_dumper.cols=col_image; uchar* ptr_tmp_image=(uchar*) block_data_image; for (int i=0;i<row_image*col_image*3;i++) { image_dumper.dataPointer[i] = *ptr_tmp_image; ptr_tmp_image++; } memcpy(buffer_head,&image_dumper,sizeof(image_dumper)); return 1;} extern 'C'{ int dump_(int cam_num,int row_image, int col_image, void* block_data_image) { int result=dump(cam_num,row_image, col_image, block_data_image); return result; }}

CMakeLists.txt 

# cmake needs this linecmake_minimum_required(VERSION 2.8) # Define project nameproject(opencv_example_project) # Find OpenCV, you may need to set OpenCV_DIR variable# to the absolute path to the directory containing OpenCVConfig.cmake file# via the command line or GUIfind_package(OpenCV REQUIRED) # If the package has been found, several variables will# be set, you can find the full list with descriptions# in the OpenCVConfig.cmake file.# Print some message showing some of themmessage(STATUS 'OpenCV library status:')message(STATUS ' version: ${OpenCV_VERSION}')message(STATUS ' libraries: ${OpenCV_LIBS}')message(STATUS ' include path: ${OpenCV_INCLUDE_DIRS}') if(CMAKE_VERSION VERSION_LESS '2.8.11') # Add OpenCV headers location to your include paths include_directories(${OpenCV_INCLUDE_DIRS})endif() # Declare the executable target built from your sourcesadd_library(opencv_example SHARED example.cpp)add_executable(test_example test_run.cpp) # Link your application with OpenCV librariestarget_link_libraries(opencv_example ${OpenCV_LIBS})target_link_libraries(test_example ${OpenCV_LIBS})

最后生成庫

python和C++共享內存傳輸圖像的示例

2.python調用C++動態庫進行存圖

#!/usr/bin/env python import sys #sys.path.append('/usr/lib/python3/dist-packages')#sys.path.append('/home/frank/Documents/215/code/parrot-groundsdk/.python/py3/lib/python3.5/site-packages') import cv2import ctypesimport numpy as npll = ctypes.cdll.LoadLibrarylib = ll('./build/libopencv_example.so')lib.dump_.restype = ctypes.c_int count = 1#path = '/home/frank/Documents/215/2020.10.24/python_ctypes/image/' while count < 30: path = './image/'+str(count)+'.jpg' print(path) image=cv2.imread(path) #cv2.imshow('test',image) #cv2.waitKey(0) image_data = np.asarray(image, dtype=np.uint8) image_data = image_data.ctypes.data_as(ctypes.c_void_p) value = lib.dump_(0,image.shape[0], image.shape[1], image_data) print(value) count += 1 if count == 30:count = 1

3.C++讀取共享內存獲取圖像

#include <iostream>#include <stdlib.h>#include <sys/shm.h>#include 'opencv2/core.hpp'#include 'opencv2/imgproc.hpp'#include 'opencv2/highgui.hpp'#include 'opencv2/videoio.hpp' #define key 650#define image_size_max 1920*1080*3 using namespace cv;using namespace std; typedef struct{int rows;int cols;uchar dataPointer[image_size_max];}image_head; int main(){ int count = 1; while(true) { int shm_id = shmget(key+0,sizeof(image_head) ,IPC_CREAT); if(shm_id == -1) {cout<<'shmget error'<<endl; return -1; } cout << ' shem id is '<<shm_id<<endl; image_head* buffer_head; buffer_head = (image_head*)shmat(shm_id, NULL, 0); if((long)buffer_head == -1) {perror('Share memary can’t get pointern'); return -1; } image_head image_dumper; memcpy(&image_dumper, buffer_head, sizeof(image_head)); cout<<image_dumper.rows<<' '<<image_dumper.cols<<endl; uchar* data_raw_image=image_dumper.dataPointer; cv::Mat image(image_dumper.rows, image_dumper.cols, CV_8UC3); uchar* pxvec =image.ptr<uchar>(0); int count = 0; for (int row = 0; row < image_dumper.rows; row++) { pxvec = image.ptr<uchar>(row); for(int col = 0; col < image_dumper.cols; col++) {for(int c = 0; c < 3; c++){ pxvec[col*3+c] = data_raw_image[count]; count++;} } } cv::imshow('Win',image); cv::waitKey(1); } return 1;}

以上就是python和C++共享內存傳輸圖像的示例的詳細內容,更多關于python和c++傳輸圖像的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | TwistDx恒温扩增-RAA等温-Jackson抗体-默瑞(上海)生物科技有限公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 汽车整车综合环境舱_军标砂尘_盐雾试验室试验箱-无锡苏南试验设备有限公司 | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | 压砖机、液压制砖机、静压砖机、环保砖机生产厂家—杜甫机械 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 海水晶,海水素,海水晶价格-潍坊滨海经济开发区强隆海水晶厂 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 不锈钢水管-不锈钢燃气管-卫生级不锈钢管件-不锈钢食品级水管-广东双兴新材料集团有限公司 | 超声骨密度仪-骨密度检测仪-经颅多普勒-tcd仪_南京科进实业有限公司 | 过跨车_过跨电瓶车_过跨转运车_横移电动平车_厂区转运车_无轨转运车 | 紫外线老化试验箱_uv紫外线老化试验箱价格|型号|厂家-正航仪器设备 | 流量卡中心-流量卡套餐查询系统_移动电信联通流量卡套餐大全 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | 洁净化验室净化工程_成都实验室装修设计施工_四川华锐净化公司 | 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 压砖机、液压制砖机、静压砖机、环保砖机生产厂家—杜甫机械 | 寮步纸箱厂_东莞纸箱厂 _东莞纸箱加工厂-东莞市寮步恒辉纸制品厂 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | 山东石英砂过滤器,除氟过滤器「价格低」-淄博胜达水处理 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 南京泽朗生物科技有限公司-液体饮料代加工_果汁饮料代加工_固体饮料代加工 | 新能源汽车电机定转子合装机 - 电机维修设备 - 睿望达 | 磁棒电感生产厂家-电感器厂家-电感定制-贴片功率电感供应商-棒形电感生产厂家-苏州谷景电子有限公司 | 加热制冷恒温循环器-加热制冷循环油浴-杭州庚雨仪器有限公司 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 | 高博医疗集团上海阿特蒙医院 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 防伪溯源|防窜货|微信二维码营销|兆信_行业内领先的防伪防窜货数字化营销解决方案供应商 | 仿清水混凝土_清水混凝土装修_施工_修饰_保护剂_修补_清水混凝土修复-德州忠岭建筑装饰工程 | 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 影视模板素材_原创专业影视实拍视频素材-8k像素素材网 | 禹城彩钢厂_钢结构板房_彩钢复合板-禹城泰瑞彩钢复合板加工厂 | 新能源汽车电机定转子合装机 - 电机维修设备 - 睿望达 |