Getting Houndsfield or Gray Value data from an image stack

I am currently attempting to access the HU data directly from python. I found the get_voxel_buffer() method in the mimics.ImageData class, which returns a multi-dimensional memoryview of gray values. I planned to convert these to HU next, however I found that the returned object was quite difficult to convert to a convenient form (numpy array would be preferred). I thought I’d ask the group while I continued to try and solve the problem on my end. I can return a 1D list of integers, but making an array that allows for fast processing keeps eluding me. To be clear, this is the code I’m running to get to a list.

import mimics
import numpy as np

images =
buffer = images.get_voxel_buffer()
casted = buffer.cast('b').cast('i')
lst = casted.tolist()

I think part of the problem is that the obj attribute of buffer is None. I’m guessing that hinders my ability to easily access that data. I’ve tried everything I could think of and most everything I could find online. This iteration process using grouper technically works, it just takes a long, long, long, long time.

import mimics
import numpy as np
from itertools import zip_longest

use_actual_array = False
images =
buffer = images.get_voxel_buffer()
flat_list = buffer.cast('b').cast(buffer.format)
test_arr = np.empty(shape=buffer.shape)

def grouper(n, iterable, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return zip_longest(fillvalue=fillvalue, *args)

if use_actual_array:
    arr = buffer
    arr = np.array(range(2*3*4)).reshape((4, 3, 2))
    flat_list = arr.flatten().tolist()
    test_arr = np.zeros_like(arr)

for row_idx, row in enumerate(grouper(arr.shape[-1], flat_list)):
    test_arr[row_idx // arr.shape[1],row_idx % arr.shape[1]] = row

Hello Nathaniel,

You can easily transform the memoryview into a numpy array using the function asarray from numpy.

Here is the example of the code I use:

import numpy as np

im =["fixed"]  #selecting the image
mem_view = im.get_voxel_buffer()  #get the buffer in the memory view
buffer = np.asarray(mem_view)   #transform the memory view in a numpy array


Regarding the conversion, we have the build-in mimics functions mimics.segment.GV2HU() and mimics.segment.HU2GV() that you can use to make the conversion between HU and Gray Value if needed.

Hope this helps.


I had planned on using the conversion functions once I got to that point.

I tried using np.asarray (first thing I tried yesterday) and I get this error:
I’m using numpy version 1.18.4 with python 3.7.6.

arr = np.asarray(buffer)
Traceback (most recent call last):
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\IPython\core\", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-92-3c9f6e397906>", line 1, in <module>
    arr = np.asarray(buffer)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\numpy\core\", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\", line 220, in method
    return syncreq(_self, consts.HANDLE_CALLATTR, name, args, kwargs)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\", line 75, in syncreq
    return conn.sync_request(handler, proxy, *args)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\", line 471, in sync_request
    return self.async_request(handler, *args, timeout=timeout).value
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\", line 97, in value
    raise self._obj
_get_exception_class.<locals>.Derived: multi-dimensional sub-views are not implemented
========= Remote Traceback (1) =========
Traceback (most recent call last):
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\", line 329, in _dispatch_request
    res = self._HANDLERS[handler](self, *args)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\", line 603, in _handle_callattr
    return self._handle_call(obj, args, kwargs)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\", line 590, in _handle_call
    return obj(*args, **dict(kwargs))
NotImplementedError: multi-dimensional sub-views are not implemented

I am currently looking at updating rpyc to see if that helps. I’m using 4.0.2 and the latest version is 4.1.5

Well, I just learned about the new instructions to install the mimics wheel, per your python installation instructions. I upgraded that to mimics 23 and upgraded rpyc to 4.1.5, and I’m still getting the same error.

Hey Nathaniel,

This issue might be linked to Conda and/or the use of the Script Listener. Are your using the Mimics Script Listener to run your code ?
(In general the Script Listener has difficulties working with large objects like pixel buffer)

I can reproduce the same error by running my code from an external IDE (with the Script Listener) while it works if ran directly from the Mimics Editor. That might do the trick for now.

We are going to investiguate this further.



I have been using the Script Listener to run from Pycharm. I made myself a work-around macro to save the data as a .npy file from mimics without the listener, allowing me to import it quickly later from anywhere.

I can send y’all a YAML file with my conda environment, if you would like! I can’t upload it here, but I suppose I could just paste it in as text.