Maybe it should be changed: 'Mat* r_1 = (Mat*)malloc(len * sizeof(Mat))' and 'new (r_1+i) Mat(...)'.
Preprocessing an array of Python images OpenCV C++
Андрей Денисевич
1
Reputation point
Hi, I have a Python array of images called args that is fed into a preprocessing function. In addition, the function gets an empty Python array res_1 of the same size as args. I just need to pre-process the images from the args array and store them in the rest_1 array. But every time I encounter the error "Segmentation fault (Core reset)". Can you tell me what you need to do to make this work?
#include <iostream>
#include <boost/python.hpp>
#include <omp.h>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv4/opencv2/opencv.hpp>
#include <boost/python/numpy.hpp>
#include <numpy/arrayobject.h>
using namespace cv;
namespace py = boost::python;
namespace np = boost::python::numpy;
#define WIDTH 2048
#define HEIGHT 2896
void bold_preprocess(PyObject* args, PyObject* res_1)
{
cv::Size newImageSize, source_ImageSize;
Mat img_resized;
Py_Initialize();
import_array();
npy_intp shape [2] = {WIDTH, HEIGHT};
Mat MORPH_RECT_KERNEL = getStructuringElement(MORPH_RECT, Size(6, 6), Point(-1,-1));
Mat MORPH_RECT_KERNEL_H = getStructuringElement(MORPH_RECT, Size(6, 1), Point(-1,-1));
Mat MORPH_RECT_KERNEL_V = getStructuringElement(MORPH_RECT, Size(1, 6), Point(-1,-1));
Mat MORPH_RECT_KERNEL_4 = getStructuringElement(MORPH_RECT, Size(4, 4), Point(-1,-1));
Mat MORPH_RECT_KERNEL_HOR = getStructuringElement(MORPH_RECT, Size(1, 30), Point(-1,-1));
source_ImageSize = cv::Size(static_cast<int>(WIDTH), static_cast<int>(HEIGHT));
newImageSize = cv::Size(static_cast<int>(WIDTH*3), static_cast<int>(HEIGHT*3));
int len = PyList_Size(args);
Mat images[len];
#pragma omp parallel
{
char* x_1 = (char*)malloc(HEIGHT*3*WIDTH*3*8);
Mat* r_1 = (Mat*)malloc(len);
PyArrayObject* tmp;
void* tmp_v;
#pragma omp parallel for
for (int i = 0; i < len; i++) {
images[i] = Mat(Size(WIDTH, HEIGHT), CV_64FC1, PyArray_DATA((PyArrayObject*)PyList_GetItem(args, i)));
new (r_1+1) Mat(Size(WIDTH*3, HEIGHT*3), CV_64FC1, (unsigned char*)(x_1));
resize(images[i], r_1[i], newImageSize, 0, 0);
morphologyEx(r_1[i], r_1[i], MORPH_OPEN, MORPH_RECT_KERNEL, Point(-1,-1), 1);
morphologyEx(r_1[i], r_1[i], MORPH_OPEN, MORPH_RECT_KERNEL_H, Point(-1,-1), 1);
morphologyEx(r_1[i], r_1[i], MORPH_OPEN, MORPH_RECT_KERNEL_V, Point(-1,-1), 1);
dilate(r_1[i], r_1[i], MORPH_RECT_KERNEL_HOR);
tmp = (PyArrayObject*)PyList_GetItem(res_1, i);
tmp_v = PyArray_DATA(tmp);
resize(r_1[i], Mat(Size(WIDTH, HEIGHT), CV_64FC1, (unsigned char*)(tmp_v)), source_ImageSize, 0, 0);
}
}
}