def ReadWlzTr(): #{
  try: #{
    errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
    if args.input== '-': #{
      f = libc.stdin
    else: #}{
      f = libc.fopen(args.input, 'rb')
    #}
    if not bool(f): #{
      raise IOError()
    #}
    obj = w.WlzAssignObject( \
          w.WlzReadObj(f, c.byref(errNum)), None)
    if not args.input == '-': #{
      libc.fclose(f)
    #}
    if not bool(obj): #{
      errNum = w.enum__WlzErrorNum(w.WLZ_ERR_OBJECT_NULL)
      raise WlzError()
    #}
    if ((not int(obj.contents.type) == int(w.WLZ_AFFINE_TRANS)) or \
        (not int(obj.contents.domain.core.contents.type) ==  \
             int(w.WLZ_TRANSFORM_3D_AFFINE))): #{
      errNum = w.enum__WlzErrorNum(w.WLZ_ERR_OBJECT_TYPE)
      raise WlzError()
    #}
  except IOError: #}{
    ErrorMsg('Failed to open ' + args.input)
  except WlzError: #}{
    ErrorMsg('Failed to read affine transform from ' + args.input +
             '(' + w.WlzStringFromErrorNum(errNum, None) + ')')
  #}
  mat = WlzTrToMat(obj.contents.domain.t)
  return(mat)
def ReadWoolzFile(filename):
    obj = None
    errNum = Wlz.enum__WlzErrorNum(Wlz.WLZ_ERR_FILE_OPEN)
    fp = libc.fopen(filename, 'rb')
    if (bool(fp)):
        obj = Wlz.WlzReadObj(fp, ctypes.byref(errNum))
        libc.fclose(fp)
    return (obj)
Exemple #3
0
 def readWlzObj(self, fname):  #{
     logging.debug('readWlzObj()')
     obj = None
     logging.debug('attempting to read object from ' + fname)
     self.errnum = c.c_int(w.WLZ_ERR_FILE_OPEN)
     fp = c.cast(libc.fopen(fname, 'rb'), c.POINTER(w.FILE))
     if bool(fp):  #{
         obj = w.WlzAssignObject( \
               w.WlzReadObj(fp, c.byref(self.errnum)), None)
         libc.fclose(fp)
         if bool(self.errnum):  #{
             self.warnWlzError('Failed to read object from ' + fname)
         #}
     #}
     return (obj)
Exemple #4
0
import matplotlib.pyplot as pt
import numpy as np

libc = ctypes.CDLL("libc.so.6")
fopen = libc.fopen
fclose = libc.fclose

errNum = Wlz.enum__WlzErrorNum(Wlz.WLZ_ERR_NONE)

fopen.restype = ctypes.POINTER(Wlz.FILE)

f = 'test.wlz'

print('Read test object from the file ' +  f);
fp = fopen(f, 'rb')
obj = Wlz.WlzAssignObject(Wlz.WlzReadObj(fp, ctypes.byref(errNum)), None)
fclose(fp)
if(bool(errNum)):
  print('Failed to read object from ' + f + \
        ' (' + Wlz.WlzStringFromErrorNum(errNum, None) + ').')
  exit(1)


if((not bool(obj.contents)) or
   ((obj.contents.type != Wlz.WlzObjectType(Wlz.WLZ_3D_DOMAINOBJ).value) and
    (obj.contents.type != Wlz.WlzObjectType(Wlz.WLZ_2D_DOMAINOBJ).value))): 
  print('Either 2 or 3D domain object with values required.')
  exit(1)

print('Computing histogram')
hobj = Wlz.WlzAssignObject( \
def ExportDomainsToIndexObj():  #{
    # Get domain indices and names from the label description file
    # adding the domain at each step.
    cpd_obj = None
    idx_obj = None
    max_idx = 0
    obj_type = w.WLZ_NULL
    dom_objs = []
    dom_idxs = []
    with open(args.ldf) as ldf:  #{
        for rec in ldf:  #{
            VerbMsg('rec = ' + rec)
            if not re.match(r'^[ \t]*#', rec):  #{
                rec = rec.lstrip()
                dom_idx = int(re.split('\s+', rec)[0])
                dom_name = re.split('"', rec)[1]
                VerbMsg('idx = ' + str(dom_idx) + ' dom = ' + dom_name)
                if (dom_idx > 0):  #{
                    try:  #{
                        if dom_idx > max_idx:  #{
                            max_idx = dom_idx
                        #}
                        dom_idxs.append(dom_idx)
                        errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
                        dom_file = args.domaindir + '/' + dom_name + '.wlz'
                        fp = libc.fopen(dom_file, 'rb')
                        if not bool(fp):  #{
                            raise IOError()
                        #}
                        obj = w.WlzAssignObject( \
                              w.WlzReadObj(fp, c.byref(errNum)), None)
                        if (bool(errNum)):  #{
                            raise WlzError()
                        #}
                        p_lst = w.WlzMakePropertyList(None)
                        p_nam = w.WlzMakeNameProperty(dom_name,
                                                      c.byref(errNum))
                        if (bool(errNum)):  #{
                            raise WlzError()
                        #}
                        ft = c.CFUNCTYPE(c.c_void_p, c.c_void_p)\
                                        (w.WlzFreePropertyListEntry)
                        alcerr = w.AlcDLPListEntryAppend(p_lst.contents.list, None, \
                                       p_nam, ft)
                        if (bool(alcerr)):  #{
                            raise WlzError()
                        #}
                        obj.contents.plist = w.WlzAssignPropertyList(
                            p_lst, None)
                        dom_objs.append(obj)
                        libc.fclose(fp)
                        if (bool(errNum)):  #{
                            raise WlzError()
                        #}
                        VerbMsg('domain object type (idx == ' + str(dom_idx) + ') ' + \
                                str(w.WlzStringFromObjTypeValue( \
                                obj.contents.type, None)))
                        if (obj_type == w.WLZ_NULL):  #{
                            if((obj.contents.type == w.WLZ_2D_DOMAINOBJ) or \
                               (obj.contents.type == w.WLZ_3D_DOMAINOBJ)): #{
                                obj_type = obj.contents.type
                            #}
                        #}
                    except IOError:  #}{
                        ErrorMsg('Failed to read domain from file ' + dom_file + \
                                 ' (' + w.WlzStringFromErrorNum(errNum, None) + ')')
                    except WlzError:  #}{
                        ErrorMsg('Failed to process domain from file ' + dom_file + \
                                 ' (' + w.WlzStringFromErrorNum(errNum, None) + ')')
                    #}
                #}
            #}
        #}
    #}
    VerbMsg('Object type ' + str(w.WlzStringFromObjTypeValue(obj_type, None)))
    if ((max_idx < 1) or ((not obj_type == w.WLZ_2D_DOMAINOBJ) and
                          (not obj_type == w.WLZ_3D_DOMAINOBJ))):  #{
        ErrorMsg('Require at least one 2 or 3D domain with a positive index.')
    #}
    try:  #{
        VerbMsg('Creating compound array object of domains.')
        cpd_obj = w.WlzMakeCompoundArray( \
                    w.enum__WlzObjectType(w.WLZ_COMPOUND_ARR_2), \
                    1, max_idx + 1, None, obj_type, c.byref(errNum))
        if (bool(errNum)):  #{
            raise WlzError()
        #}
        for i in range(0, len(dom_objs)):  #{
            obj = dom_objs[i]
            idx = dom_idxs[i]
            cpd_obj.contents.o[idx] = obj
        #}
    except:  #}{
        ErrorMsg('Failed to create compound object from domains (' + \
                 w.WlzStringFromErrorNum(errNum, None) + ')')
    #}
    if not args.compound:  #{
        try:  #{
            VerbMsg('Creating index object from compound of domains.')
            idx_obj = w.WlzAssignObject( \
                      w.WlzIndexObjFromCompound(cpd_obj, c.byref(errNum)), None)
            if (bool(errNum)):  #{
                raise WlzError()
            #}
        except:  #}{
            ErrorMsg('Failed to create index object from compound of domains (' + \
                     w.WlzStringFromErrorNum(errNum, None) + ')')
        #}
    #}
    if bool(args.reference) and (not args.compound):  #{
        try:  #{
            VerbMsg('Cutting index object to the same bounding box as the\n' + \
                    'reference object (read from ' + args.reference + ').')
            fp = None
            errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
            if args.reference == '-':  #{
                fp = libc.stdin
            else:  #}{
                fp = libc.fopen(args.reference, 'rb')
            #}
            if not bool(fp):  #{
                raise IOError()
            #}
            obj = w.WlzAssignObject( \
                  w.WlzReadObj(fp, c.byref(errNum)), None)
            if not args.reference == '-':  #{
                libc.fclose(fp)
            #}
            if (bool(errNum)):  #{
                raise WlzError()
            #}
            box = w.WlzBoundingBox3I(obj, c.byref(errNum))
            if (not bool(errNum)):  #{
                gtype = w.WlzGreyTypeFromObj(idx_obj, c.byref(errNum))
            #}
            w.WlzFreeObj(obj)
            if (bool(errNum)):  #{
                raise WlzError()
            #}
            VerbMsg('Using bounding box ' + \
                    '(' + str(box.xMin) + ',' + str(box.xMax) + '),' + \
                    '(' + str(box.yMin) + ',' + str(box.yMax) + '),' + \
                    '(' + str(box.zMin) + ',' + str(box.zMax) + ')')
            VerbMsg('Preserving grey type ' + \
                    w.WlzStringFromGreyType(gtype, None) + \
                    '.')
            obj = w.WlzCutObjToBox3D(idx_obj, box, gtype, 0, 0.0, 0.0, \
                                       c.byref(errNum))
            if (bool(errNum)):  #{
                raise WlzError()
            #}
            w.WlzFreeObj(idx_obj)
            idx_obj = obj
        except IOError:  #}{
            ErrorMsg('Failed to read the reference object read from ' + \
                     args.reference + '.')
        except WlzError:  #}{
            ErrorMsg('Failed to cut index object to the bounding box of the\n' + \
                     'reference object read from (' + args.reference + ') (' + \
                     w.WlzStringFromErrorNum(errNum, None) + ')')
        #}
    #}
    try:  #{
        ft = 'index'
        if args.compound:  #{
            ft = 'compound'
        #}
        VerbMsg('Writing ' + ft + ' object to output file (' + args.output +
                ').')
        errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
        if args.output == '-':  #{
            fp = libc.stdout
        else:  #}{
            fp = libc.fopen(args.output, 'wb')
        #}
        if not bool(fp):  #{
            raise IOError()
        #}
        if args.compound:  #{
            errNum = w.WlzWriteObj(fp, cpd_obj)
        else:  #}{
            errNum = w.WlzWriteObj(fp, idx_obj)
        #}
        if not args.output == '-':  #{
            libc.fclose(fp)
        #}
        if (bool(errNum)):  #{
            raise WlzError()
        #}
    except:  #}{
        ErrorMsg('Failed to write ' + ft + ' object to file (' + \
                 w.WlzStringFromErrorNum(errNum, None) + ')')
   #}
   MsgVerbose('Voxel size is ' + str(voxel_sz) + '.')
 #}
 #
 # Copy the grey scale Woolz reference object setting name and
 # text properties
 if(not args.nofiles): #{
   fple = c.CFUNCTYPE(c.c_void_p, c.c_void_p) \
          (w.WlzFreePropertyListEntry)
   # Read the Woolz grey scale reference object and write it
   try: #{
     MsgVerbose('Reading voxel image from file ' + vox_file)
     errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
     fp = libc.fopen(vox_file.encode('utf-8'), 'rb')
     ref_obj = w.WlzAssignObject( \
               w.WlzReadObj(fp, c.byref(errNum)), None)
     libc.fclose(fp)
     ChkWoolzError(errNum, 0)
     MsgVerbose('Setting voxel image voxel size to ' + str(voxel_sz))
     w.WlzSetVoxelSize(ref_obj, voxel_sz[0], voxel_sz[1], voxel_sz[2])
     MsgVerbose('Creating reference object property list.')
     p_lst = w.WlzMakePropertyList(None)
     p_str = stage + '_' + model  + '_reference'
     p_nam = w.WlzMakeNameProperty(p_str.encode('utf-8'),
                                   c.byref(errNum))
     p_txt = w.WlzMakeTextProperty('Origin'.encode('utf-8'),
                                   origin.encode('utf-8'),
                                   c.byref(errNum))
     alcerr = w.AlcDLPListEntryAppend(p_lst.contents.list, None,
                                      p_nam, fple)
     alcerr = w.AlcDLPListEntryAppend(p_lst.contents.list, None,