Exemple #1
0
    def test_get_methods(self):

        tmp_arr = np.ones((5, 5), dtype=np.uint8)
        vox = [1.0, 1.0]
        orig = [0, 0]
        img = SpatialImage(tmp_arr)
        metadata = {
            'dim': 2,
            'extent': [5.0, 5.0],
            'shape': (5, 5),
            'type': 'uint8',
            'voxelsize': [1.0, 1.0],
            'origin': [0, 0],
            'max': 1,
            'mean': 1.0,
            'min': 1
        }

        self.assertEqual(img.get_type(), tmp_arr.dtype)
        self.assertEqual(img.get_voxelsize(), vox)
        self.assertEqual(img.get_shape(), tmp_arr.shape)
        self.assertEqual(img.get_origin(), orig)
        self.assertEqual(img.get_extent(),
                         [tmp_arr.shape[0], tmp_arr.shape[1]])
        self.assertEqual(img.get_dim(), 2)
        self.assertDictEqual(img.get_metadata(), metadata)
    def test_get_methods(self):

        tmp_arr = np.ones((5,5),dtype=np.uint8)
        vox = [1.0, 1.0]
        orig = [0, 0]
        img = SpatialImage(tmp_arr)
        metadata = {'dim': 2, 'extent': [5.0, 5.0], 'shape': (5, 5), 'type': 'uint8',
                    'voxelsize': [1.0, 1.0], 'origin': [0, 0], 'max': 1, 'mean': 1.0,
                    'min': 1}

        self.assertEqual(img.get_type(), tmp_arr.dtype)
        self.assertEqual(img.get_voxelsize(), vox)
        self.assertEqual(img.get_shape(), tmp_arr.shape)
        self.assertEqual(img.get_origin(), orig)
        self.assertEqual(img.get_extent(), [tmp_arr.shape[0], tmp_arr.shape[1]])
        self.assertEqual(img.get_dim(), 2)
        self.assertDictEqual(img.get_metadata(), metadata)
Exemple #3
0
def vt_img_to_sp_img(vt_image):
    """
    _VT_IMAGE structure to SpatialImage
    """
    dt = vt_type_to_c_type(vt_image.type)
    x, y, z, v = vt_image.dim.x, vt_image.dim.y, vt_image.dim.z, vt_image.dim.v
    size = x * y * z * v
    vx, vy, vz = vt_image.siz.x, vt_image.siz.z, vt_image.siz.z
    _ct_array = (dt * size).from_address(vt_image.buf)
    _np_array = np.ctypeslib.as_array(_ct_array)
    # -- This used to be  arr =  np.array(_np_array.reshape(z,x,y).transpose(2,1,0)).
    # but that is wrong. first the shape is x,y,z. Then the transposition
    # doesn't fix the byte ordering which for some reason must be read in
    # Fortran order --
    out_arr = np.array(_np_array.reshape(x, y, z, order="F"))
    out_sp_img = SpatialImage(out_arr, voxelsize=[vx, vy, vz])
    if 1 in out_sp_img.get_shape():  # 2D management
        out_sp_img = out_sp_img.to_2D()
    return out_sp_img
Exemple #4
0
def vt_img_to_sp_img(vt_image):
    """
    _VT_IMAGE structure to SpatialImage
    """
    dt = vt_type_to_c_type(vt_image.type)
    x, y, z, v = vt_image.dim.x, vt_image.dim.y, vt_image.dim.z, vt_image.dim.v
    size = x * y * z * v
    vx, vy, vz = vt_image.siz.x, vt_image.siz.z, vt_image.siz.z
    _ct_array = (dt * size).from_address(vt_image.buf)
    _np_array = np.ctypeslib.as_array(_ct_array)
    # -- This used to be  arr =  np.array(_np_array.reshape(z,x,y).transpose(2,1,0)).
    # but that is wrong. first the shape is x,y,z. Then the transposition
    # doesn't fix the byte ordering which for some reason must be read in
    # Fortran order --
    out_arr = np.array(_np_array.reshape(x, y, z, order="F"))
    out_sp_img = SpatialImage(out_arr, voxelsize=[vx, vy, vz])
    if 1 in out_sp_img.get_shape(): # 2D management
        out_sp_img = out_sp_img.to_2D()
    return out_sp_img