backgrounds:
python直接调用c++最大的目的是为了传图片快,所以需要保持c++和python的数据格式同步
代码功能:python传递图片给c++,c++转化为灰度图后传回给python
1、库文件 library.h
#ifdef __cplusplus
extern "C" {
#endif
#ifndef EXCAPP_LIBRARY_H
#define EXCAPP_LIBRARY_H
#include
//using namespace cv;
typedef void (*FUNP)();
typedef void (*FUNP1)(char *ch);
void hello();
void echo(char *text);
u_char* cpp_canny(int height, int width, u_char* data) ;
void release(u_char* data);
#endif
#ifdef __cplusplus
}
#endif
2、c++文件,test.cpp,release函数用于释放内存
#include
#include "library.h"
#include
#include
#include
using namespace std;
using namespace cv;
//int height = 2048;
//int width = 1088;
typedef void (*FUNP)();
typedef void (*FUNP1)(char *ch);
void hello() {
printf("%s\n", "Hello, World!");
}
void echo(char *text) {
printf("%s\n", text);
}
u_char* cpp_canny(int height, int width, u_char* data) {
cv::Mat src(height, width, CV_8UC1, data);
cv::Mat dst;
Canny(src, dst, 100, 200);
uchar* buffer = (uchar*)malloc(sizeof(uchar)*height*width);
memcpy(buffer, src.data, height*width);
}
void release(u_char* data) {
free(data);
}
3、CMakeList.txt文件
一般来说,调用opencv库文件有两种方式:find_packages和link_directories。下面我注释掉的部分就是第二种方法,因为报错了。虽然已经给了库路径,但是程序还是找不到文件,奇奇怪怪。
编译过程:新建build,终端cmake .. make
cmake_minimum_required(VERSION 3.10)
project(excapp)
set(CMAKE_CXX_STANDARD 11)
set(EXCAPPLIB test.cpp library.h)
SET(CMAKE_BUILD_TYPE "Debug")
#set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11" )
# 找到opencv库文件
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
#add_executable( demo demo.cpp )
# 创建共享库
add_library(excapp SHARED ${EXCAPPLIB})
# 添加可执行文件所需的库
target_link_libraries( excapp ${OpenCV_LIBS} )
# 添加头文件路径
# include_directories("/usr/local/include/opencv2/")
# # 添加动态库路径
# link_directories("/usr/local/lib")
# link_libraries(
# libopencv_calib3d
# libopencv_core
# libopencv_photo
# libopencv_dnn
# libopencv_shape
# libopencv_features2d
# libopencv_stitching
# libopencv_flann
# libopencv_superres
# libopencv_highgui
# libopencv_videoio
# libopencv_imgcodecs
# libopencv_video
# libopencv_imgproc
# libopencv_videostab
# libopencv_ml
# libopencv_objdetect
# )
# # 创建共享库
# add_library(excapp SHARED ${EXCAPPLIB})
4、编写python文件,读取本地图片
import ctypes
import cv2
import numpy as np
lib = ctypes.CDLL("build/libexcapp.so")
# 调用带参数的接口
lib.echo("This is echo method.".encode("utf8"))
def cpp_canny(input):
if len(img.shape)>=3 and img.shape[-1]>1:
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
h,w=gray.shape[0],gray.shape[1]
# 获取numpy对象的数据指针
frame_data = np.asarray(gray, dtype=np.uint8)
frame_data = frame_data.ctypes.data_as(ctypes.c_char_p)
# 设置输出数据类型为uint8的指针
lib.cpp_canny.restype = ctypes.POINTER(ctypes.c_uint8)
# 调用dll里的cpp_canny函数
pointer = lib.cpp_canny(h,w,frame_data)
# 从指针指向的地址中读取数据,并转为numpy array
np_canny = np.array(np.fromiter(pointer, dtype=np.uint8, count=h*w))
return pointer,np_canny.reshape((h,w))
img=cv2.imread('test.bmp')
ptr,canny=cpp_canny(img)
cv2.imshow('canny',canny)
cv2.waitKey(9000)
#将内存释放
lib.release(ptr)
目前代码还有点问题,python拿回来的图片不是原图片大小。目前还在调试,代码复制过程中可能有疏漏 @_@
部分代码是copy的,但是实在找不到原博主的网址了。。