Skip to content

Commit 739df0d

Browse files
committed
Safely copy data via pointer got via PyArray_BYTES
At the end of the segmentation, the data is copied directly from a vector to the data array of the numpy ndarray. When using the latest versions of numpy, a segfault can be thrown. This change introduces a safer handling of this copy using PyArray_BYTES.
1 parent 2c7871a commit 739df0d

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

densecrf_python/densecrf3d.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ dense_crf_wrapper(PyObject *self, PyObject *args)
141141
(unsigned char *)arr_I->data);
142142
MatrixXf probMapsMatrix = crf3d.inference(MaxIterations);
143143
VectorXs segmentationVector = crf3d.currentMap(probMapsMatrix);
144-
int outshape[3];
145-
outshape[0] = shape_P[0];
146-
outshape[1] = shape_P[1];
147-
outshape[2] = shape_P[2];
148-
PyArrayObject * labels = (PyArrayObject*) PyArray_FromDims(3, outshape, NPY_INT8);
144+
145+
npy_intp shape_labels[] = {shape_P[0], shape_P[1], shape_P[2]};
146+
PyArrayObject * labels = (PyArrayObject*) PyArray_SimpleNew(3, shape_labels, NPY_INT8);
147+
npy_int8 * c_labels = (npy_int8*) PyArray_BYTES(labels);
148+
149149
for(int i = 0; i < num_voxel; i++)
150150
{
151-
*(labels->data + i) = segmentationVector(i);
151+
c_labels[i] = (npy_int8) segmentationVector(i);
152152
}
153153

154154
Py_DECREF(arr_I);

0 commit comments

Comments
 (0)