一. 背景
- 用pyhton可直接调用C++,减少重写的工作量;
- 部分逻辑运算,C++的执行效率高,可进行加速。
下面就一个简单的C++滤镜(彩色图转灰度图)为例,展示python调用C++
二. 代码实现
代码结构如下:
.
├── build
├── CMakeLists.txt
├── image_processing.cpp # C++头文件
├── image_processing.h # C++源文件
└── image_process.py # python调用C++
各个文件的内容如下
image_processing.h
#ifndef IMAGE_PROCESSING_H
#define IMAGE_PROCESSING_H
#include <opencv2/opencv.hpp>
extern "C" {
void process_image(const unsigned char* input, unsigned char* output, int width, int height, int channels);
}
#endif // IMAGE_PROCESSING_H
im