原理
python没有办法直接和c++共享内存交互,需要间接调用c++打包好的库来实现
流程
- C++共享内存打包成库
- python调用C++库往共享内存存图像数据
- C++测试代码从共享内存读取图像数据
实现
1.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 line cmake_minimum_required(VERSION 2.8) # Define project name project(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 GUI find_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 them message(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 sources add_library(opencv_example SHARED example.cpp) add_executable(test_example test_run.cpp) # Link your application with OpenCV libraries target_link_libraries(opencv_example ${OpenCV_LIBS}) target_link_libraries(test_example ${OpenCV_LIBS})
最后生成库
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 cv2 import ctypes import numpy as np ll = ctypes.cdll.LoadLibrary lib = 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 pointer\n"); 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++传输图像的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python和C++共享内存传输图像的示例”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
2024年11月24日
2024年11月24日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]