1

I am trying to convert some of my MATLAB files to Python. I generated C code using the MATLAB Coder, then compiled everything in a library. I am now trying to make the wrapping for Python.

It looks as follows: data_in is an image and the output of the makePyramid function is a structure of length level with 3 fields img, gradX and gradY.

def TestEmxAPI(data_in):

    #class Opaque(ctypes.Structure):
    #    pass

    sz = (data_in.shape)

    class Pyr(ctypes.Structure): 
        _fields_ = [('img', (ctypes.c_double*sz[0])*sz[1]),
                    ('gradX', (ctypes.c_double*sz[0])*sz[1]),
                    ('gradY', (ctypes.c_double*sz[0])*sz[1])] 

    nrows = ctypes.c_int(sz[0])
    ncols = ctypes.c_int(sz[1])
    level = 1
    blur = -1
    win = np.array([10.,10.,10.])
    winc = (ctypes.c_double *3)(*win)

    pp=Pyr()

    in_emx = EMX.emxCreateWrapper_real_T(ctypes.c_int(data_in.ctypes.data), 
                                         nrows, ncols) 

    EMX.emxCreateWrapper_struct0_T.argtypes = (ctypes.POINTER(Pyr), 
                                               ctypes.c_int,
                                               ctypes.c_int)
    EMX.emxCreateWrapper_struct0_T.restype = ctypes.POINTER(Pyr)
    ou_emx = EMX.emxCreateWrapper_struct0_T(pp,sz[0], sz[1])

    EMX.makePyramid_2D(in_emx, 
                       ctypes.c_double(level), 
                       ctypes.c_double(blur), 
                       winc, 
                       ou_emx)

    return pp

I now get a nasty error:

WindowsError: exception: access violation reading 0x0000000000000014

How can I make this work?

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Use the debugger to identify the line causing the problem. – Daniel Nov 02 '19 at 15:16
  • https://stackoverflow.com/questions/24640817/python-ctypes-definition-for-c-struct covers examples of doing this. You can also use a wrapper generator like SWIG to generate the wrappers. Here's a repo of [examples](https://github.com/mathworks/coder-swig) showing how to use SWIG with MATLAB Coder – Ryan Livingston Nov 02 '19 at 20:11
  • Thanks Ryan for your comment. I got very much inspired by the solution proposed by the stakoverflow link you sent. I thus wanted to use the wrapper already generated by the matlab coder. But I guess I am using it in a wrong way. Just in case the project with all the details can be found here: https://github.com/heleneayari/testmatcpython/tree/master – Helene Ayari Nov 03 '19 at 14:46

0 Answers0