Preprocessing an array of Python images OpenCV

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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);
    }
  }
}
I don't know about the omp interface, but it seems that you are leaking everywhere (¿do you need to use dynamic allocation at all?)

debugger, backtrace
valgrind, check invalid access

> new (r_1+1) Mat(Size(WIDTH*3, HEIGHT*3), CV_64FC1, (unsigned char*)(x_1));
¿did you mean r_l + i ?
Last edited on
ne555
I have little experience in C++. Yes, you are right to use r_1+i. But now I see the Error double free or corruption (out): Aborted (core dumped). Perhaps you have an idea how to fix this?
double free is fixed in 2 ways.
1) stop doing it. one allocation, one release.
2) if the program is too big and you lost track, you can set to null after release and if you accidentally free it again, it will do nothing. But this is not efficient, you wasted time doing nothing because you failed to do #1 ...

corruption usually means you went out of bounds. Do not do that either, you must keep track of what your bounds are and stay inside them. This is not fixable with any kind of hand waving like above, you just need to figure out what you did wrong and fix it. Thankfully you write just a little code and then test it, so its probably in the most recent set of small changes, right?
Registered users can post here. Sign in or register to post.