Strange Segmentation Fault in PyArray_SimpleNewFromData
Segmentation fault in PyArray_SimpleNewFromData
在尝试了上述两个帖子中的所有可能解决方案后,我正在发布此问题,但我仍然面临细分。我的代码非常简单明了,但我不知道该如何摆脱。任何对此的解决方案都会有很大帮助。
我的代码是:
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
#include <numpy/arrayobject.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <vector>
using namespace boost::python;
using namespace boost::python::numpy;
using namespace cv;
PyObject* process(PyObject* arr,int rows,int cols)
{
void* img_arr = PyArray_DATA((PyArrayObject*)arr);
Mat image(rows, cols , CV_8UC3, img_arr);
Mat median_blur;
medianBlur(image, median_blur, 15);
npy_intp dimensions[3] = {median_blur.rows, median_blur.cols, median_blur.channels()};
return PyArray_SimpleNewFromData(median_blur.dims + 1, &dimensions[0], NPY_UINT8, median_blur.data);
}
BOOST_PYTHON_MODULE(PyWrap_CPPM)
{
boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
def("process", process);
}
如果我尝试添加import_array(),则会收到另一个错误,指出以下内容:
In file included from /usr/include/signal.h:316:0,
from /usr/include/numpy/npy_interrupt.h:84,
from /usr/include/numpy/arrayobject.h:5,
from /home/adminspin/srcs/Harish_Programs/numpy_pass_c++/PyWrap_CppMod.cpp:5:
/home/adminspin/srcs/Harish_Programs/numpy_pass_c++/PyWrap_CppMod.cpp: In function ‘void init_module_PyWrap_CPPM()’:
/home/adminspin/srcs/Harish_Programs/numpy_pass_c++/PyWrap_CppMod.cpp:27:2: error: return-statement with a value, in function returning 'void' [-fpermissive]
import_array();
^
CMakeFiles/PyWrap_CPPM.dir/build.make:62: recipe for target 'CMakeFiles/PyWrap_CPPM.dir/PyWrap_CppMod.cpp.o' failed
make[2]: *** [CMakeFiles/PyWrap_CPPM.dir/PyWrap_CppMod.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/PyWrap_CPPM.dir/all' failed
make[1]: *** [CMakeFiles/PyWrap_CPPM.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
我的Cmake文件是:
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++11")
set(_Boost_NUMPY_DEPENDENCIES python${component_python_version})
project(boost_try_ex)
set(Boost_LIBS
filesystem
thread
date_time
chrono
system
timer
serialization
python3
numpy3
)
find_package(Boost REQUIRED COMPONENTS "${Boost_LIBS}")
add_definitions(-DBOOST_ALL_DYN_LINK)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIR})
find_package(PythonLibs 3 REQUIRED)
include_directories(${PYTHON_INCLUDE_PATH})
FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )
add_library(PyWrap_CPPM SHARED PyWrap_CppMod.cpp)
target_link_libraries(PyWrap_CPPM ${Boost_LIBRARIES} ${OpenCV_LIBRARIES})
set_target_properties(PyWrap_CPPM PROPERTIES PREFIX "" COMPILE_FLAGS "-DPYTHON_INTERFACE")