最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年10月27日 15:26:02   作者:小蝸牛嘰咕往前  
這篇文章主要介紹了python和C++共享內(nèi)存?zhèn)鬏攬D像的示例,幫助大家利用python處理圖片,感興趣的朋友可以了解下

原理

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)建文件

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調(diào)用C++動態(tài)庫進行存圖

#!/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++讀取共享內(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 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++共享內(nèi)存?zhèn)鬏攬D像的示例的詳細內(nèi)容,更多關(guān)于python和c++傳輸圖像的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:

相關(guān)文章

  • 基于Python爬取京東雙十一商品價格曲線

    基于Python爬取京東雙十一商品價格曲線

    這篇文章主要介紹了基于Python爬取雙十一商品價格曲線,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • Django結(jié)合使用Scrapy爬取數(shù)據(jù)入庫的方法示例

    Django結(jié)合使用Scrapy爬取數(shù)據(jù)入庫的方法示例

    這篇文章主要介紹了Django結(jié)合使用Scrapy爬取數(shù)據(jù)入庫的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • python的tkinter、socket庫開發(fā)tcp的客戶端和服務(wù)端詳解

    python的tkinter、socket庫開發(fā)tcp的客戶端和服務(wù)端詳解

    本文介紹了TCP通訊流程和開發(fā)步驟,包括客戶端和服務(wù)端的實現(xiàn),客戶端使用Python的tkinter庫實現(xiàn)圖形化界面,服務(wù)端使用socket庫監(jiān)聽連接并處理消息,文章還提供了客戶端和服務(wù)端的代碼示例
    2025-01-01
  • Python中format格式化的用法及說明

    Python中format格式化的用法及說明

    這篇文章主要介紹了Python中format格式化的用法及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 解析Python中的eval()、exec()及其相關(guān)函數(shù)

    解析Python中的eval()、exec()及其相關(guān)函數(shù)

    本篇文章主要介紹了解析Python中的eval()、exec()及其相關(guān)函數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 一文掌握Python爬蟲XPath語法

    一文掌握Python爬蟲XPath語法

    這篇文章主要介紹了一文掌握Python爬蟲XPath語法,xpath是一門在XML和HTML文檔中查找信息的語言,可用來在XML和HTML文檔中對元素和屬性進行遍歷,XPath 通過使用路徑表達式來選取 XML 文檔中的節(jié)點或者節(jié)點集。下面會更學習的介紹,需要的朋友可以參考一下
    2021-11-11
  • python包裝和授權(quán)學習教程

    python包裝和授權(quán)學習教程

    包裝是指對一個已經(jīng)存在的對象進行系定義加工,實現(xiàn)授權(quán)是包裝的一個特性,下面這篇文章主要給大家介紹了關(guān)于python包裝和授權(quán)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • Python中*args和**kwargs的作用

    Python中*args和**kwargs的作用

    *args和**kwargs,以及單獨的*,**到底是啥作用呢?原理是啥呢?讀完這篇文章你就徹底明白了,感興趣的朋友跟隨小編一起看看吧
    2023-11-11
  • Python視頻處理之噪聲矩陣與并行計算

    Python視頻處理之噪聲矩陣與并行計算

    這篇文章主要為大家詳細介紹了Python視頻處理中噪聲矩陣與并行計算的完美融合,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • Python3.7 版本 lxml 模塊無法導入etree 問題及解決方法

    Python3.7 版本 lxml 模塊無法導入etree 問題及解決方法

    這篇文章主要介紹了Python3.7 版本 lxml 模塊無法導入etree 問題及解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01

最新評論

上饶县| 古浪县| 富民县| 马龙县| 长治县| 琼结县| 出国| 湘阴县| 包头市| 巫山县| 吴忠市| 渝中区| 乐亭县| 铜鼓县| 景洪市| 习水县| 瓮安县| 闸北区| 嘉义市| 安多县| 凌海市| 华蓥市| 偏关县| 永胜县| 阆中市| 濮阳县| 武宁县| 天全县| 辽中县| 新邵县| 邳州市| 阜康市| 衡南县| 九寨沟县| 临漳县| 红桥区| 晋江市| 察雅县| 温宿县| 马尔康县| 安达市|