快活林资源网 Design By www.csstdc.com
原理
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++传输图像的资料请关注其它相关文章!
快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年12月24日
2024年12月24日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]