av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

python和C++共享內(nèi)存?zhèn)鬏攬D像的示例

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

原理

python沒有辦法直接和c++共享內(nèi)存交互,需要間接調(diào)用c++打包好的庫來實現(xiàn)

流程

C++共享內(nèi)存打包成庫 python調(diào)用C++庫往共享內(nèi)存存圖像數(shù)據(jù) C++測試代碼從共享內(nèi)存讀取圖像數(shù)據(jù)

實現(xiàn)

1.c++打包庫

創(chuàng)建文件

python和C++共享內(nèi)存?zhèn)鬏攬D像的示例

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++共享內(nèi)存?zhèn)鬏攬D像的示例

2.python調(diào)用C++動態(tài)庫進(jìn)行存圖

#!/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++讀取共享內(nèi)存獲取圖像

#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++共享內(nèi)存?zhèn)鬏攬D像的示例的詳細(xì)內(nèi)容,更多關(guān)于python和c++傳輸圖像的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 啪啪免费网站 | 精品一区二区视频 | 国产精品日韩在线 | 九色在线观看 | 男女网站免费 | 欧美高清hd | 激情福利视频 | 一区二区在线免费观看 | 国产偷久久一级精品60部 | 日韩欧美一级精品久久 | 日韩欧美在线视频观看 | 97av在线| 香蕉久久网 | 国产成人小视频 | 成人h动漫亚洲一区二区 | 国产精品一区二区三区在线 | 狠狠婷婷综合久久久久久妖精 | 国产综合av | 不卡一区 | 91免费福利视频 | 玖玖在线免费视频 | 美女视频黄的免费 | 午夜小电影 | 国产精品波多野结衣 | 成人激情视频在线播放 | 国产精品久久久久9999鸭 | 欧美一二三 | 黄片毛片免费观看 | 久久久精品影院 | 噜啊噜在线 | 国产精品欧美精品日韩精品 | 久久国产日韩 | 亚洲 精品 综合 精品 自拍 | 国产美女精品视频 | 亚洲成人精品一区二区 | www操操| 久久久妇女国产精品影视 | 国产精品久久久久久久久久 | 古装人性做爰av网站 | 国产精品久久久久一区二区三区 | 久久一区二区三区四区 |