예제 #1
0
 def addObj(self, o):  #{
     logging.debug('addObj()')
     t = int(o.contents.type)
     self.errnum = c.c_int(w.WLZ_ERR_OBJECT_TYPE)
     if (t == int(w.WLZ_2D_DOMAINOBJ)) or (t == int(
             w.WLZ_3D_DOMAINOBJ)):  #{
         if t == int(w.WLZ_2D_DOMAINOBJ):  #{
             self.errnum = c.c_int(w.WLZ_ERR_NONE)
         else:  #}{
             logging.debug('making view struct')
             v = w.WlzMake3DViewStruct(c.c_int(w.WLZ_3D_VIEW_STRUCT), \
                                       c.byref(self.errnum))
             if not bool(self.errnum):  #{
                 f = w.WlzDVertex3()
                 v.vtX = c.c_double(0.0)
                 v.vtY = c.c_double(0.0)
                 v.vtZ = c.c_double(0.0)
                 v.contents.theta = self.yaw * m.pi / 180.0
                 v.contents.phi = self.pitch * m.pi / 180.0
                 v.contents.zeta = self.roll * m.pi / 180.0
                 v.contents.dist = self.dist
                 v.contents.fixed = f
                 v.contents.view_mode = c.c_int(w.WLZ_UP_IS_UP_MODE)
                 v.contents.scale = c.c_double(1.0)
                 v.contents.voxelRescaleFlg = c.c_int(0)
                 w.WlzFree3DViewStruct(self.view)
                 self.view = w.WlzAssign3DViewStruct(v, None)
             #}
         #}
     #}
     if not bool(self.errnum):  #{
         w.WlzFreeObj(self.obj)
         self.obj = w.WlzAssignObject(o, None)
         self.errnum = self.setObj2D()
     #}
     if not bool(self.errnum):  #{
         ary = self.wlz2DToNP()
         logging.debug('setting image')
         self.img = ary.astype(np.float64).T
         self.img_itm.setImage(self.img)
         self.setROIType('N')
     #}
     if bool(self.errnum):  #{
         self.warnWlzError('Failed to add object.')
예제 #2
0
 def setObj2D(self):  #{
     logging.debug('setObj2D')
     errnum = c.c_int(w.WLZ_ERR_NONE)
     t = self.obj.contents.type
     if t == w.WLZ_2D_DOMAINOBJ:  #{
         self.obj2d = w.WlzAssignObject(self.obj, None)
     elif t == w.WLZ_3D_DOMAINOBJ:  #}{
         logging.debug('cutting section from 3D object')
         w.WlzInit3DViewStruct(self.view, self.obj)
         o2d = w.WlzGetSubSectionFromObject(
             self.obj, None, self.view,
             c.c_int(w.WLZ_INTERPOLATION_NEAREST), None, c.byref(errnum))
         print(o2d)
         print(errnum)
         if not bool(errnum):  #{
             w.WlzFreeObj(self.obj2d)
             self.obj2d = w.WlzAssignObject(o2d, None)
         #}
     #}
     return (errnum)
예제 #3
0
def outputFiles(out_dir, img_series, rescale, nowrite):
    for i in img_series:
        vrbMsg('Encoding ' + img_series[i][0].ProtocolName + '_' + str(i))
        obj = makeWlzImageObj(img_series[i], rescale)
        file_base = out_dir + '/' + img_series[i][0].ProtocolName + '_' + str(
            i)
        wlz_file_nm = file_base + '.wlz'
        txt_file_nm = file_base + '.txt'
        if not nowrite:
            fp = c.cast(fopen(wlz_file_nm.encode('utf-8'), b'wb'),
                        c.POINTER(w.FILE))
            err_num = w.WlzWriteObj(fp, obj)
            if bool(err_num):
                raise WlzError()
            fclose(fp)
            vrbMsg('Woolz object written to ' + wlz_file_nm)
        w.WlzFreeObj(obj)
        obj = None
        if not nowrite:
            f = open(txt_file_nm, 'wt')
            f.write(str(img_series[i][0]))
            f.close()
            vrbMsg('Text written to ' + txt_file_nm)
예제 #4
0
print('Computing histogram')
hobj = Wlz.WlzAssignObject( \
       Wlz.WlzHistogramObj(obj, \
               ctypes.c_int(0), ctypes.c_double(0.0), ctypes.c_double(1.0), \
				       ctypes.byref(errNum)), None)
if(bool(errNum)):
  print('Failed to compute histogram ' + \
        '(' + Wlz.WlzStringFromErrorNum(errNum, None) + ').')
  exit(1)
  
print('Plotting histogram')
hist   = hobj.contents.domain.hist.contents;
n_bins = hist.nBins
center = np.arange(n_bins) + 0.5;
bins = [0] * n_bins
if(hist.type == Wlz.WLZ_HISTOGRAMDOMAIN_INT):
  for i in range(0, n_bins):
    bins[i] = hist.binValues.inp[i]
elif(hist.type == Wlz.WLZ_HISTOGRAMDOMAIN_FLOAT):
  for i in range(0, n_bins):
    bins[i] = hist.binValues.dbp[i]
pt.xlim(0,256)
pt.yscale('log')
pt.bar(center, bins, align='center', width=1.0)
pt.show()

print('Free the objects')
Wlz.WlzFreeObj(hobj)
Wlz.WlzFreeObj(obj)

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) + ')')
    if (args.flip):
        if (args.verbose):
            print('Flipping face orientation.')
        errNum = Wlz.WlzGMFilterFlipOrient(model)
        if (bool(errNum)):
            print(prog + ': Failed to flip face orientation (' +
                  Wlz.WlzStringFromErrorNum(errNum, None) + ').')
            CleanExit(1)

    # Write the Woolz object file using the stl file format.
    if (args.verbose):
        print('Writing working Woolz file.')
    if (bool(WriteWoolzObject(workfile + '0.wlz', obj))):
        print(prog + ': Failed to write working Woolz file.')
        CleanExit(1)
    Wlz.WlzFreeObj(obj)
    cmdline = [WlzExtFFConvert, '-o' + workfile + '0.stl', \
               workfile + '0.wlz']
    if (args.verbose):
        print(cmdline)
    rtn = subprocess.call(cmdline)
    if (bool(rtn)):
        print(prog +
              ': WlzExtFFConvert failed to convert working wlz file to stl.')
        CleanExit(1)

    # Create meshlab filter script to reduce the number of faces and smooth
    # the surface
    if (args.verbose):
        print('Creating MeshLab filter file.')
    flt = []
     cpd_obj.contents.o[row.index] = w.WlzAssignObject( \
         w.WlzReadObj(fp, c.byref(errNum)), None)
     libc.fclose(fp)
     ChkWoolzError(errNum, 0)
   except: #}{
     MsgError('Failed to read anatomy domain from' + domfile, 1)
   #}
 #}
 try: #{
   alcerr = [None, None, None]
   MsgVerbose('Creating index object from compound of domains.')
   errNum = w.enum__WlzErrorNum(w.WLZ_ERR_NONE)
   tmp_obj = w.WlzAssignObject(
             w.WlzIndexObjFromCompound(cpd_obj, 
                                       c.byref(errNum)), None)
   w.WlzFreeObj(cpd_obj)
   ChkWoolzError(errNum, 0)
   MsgVerbose('Cutting index image to a cuboid.')
   box = w.WlzBoundingBox3I(ref_obj, c.byref(errNum))
   ChkWoolzError(errNum, 0)
   idx_obj_grey_type = w.WLZ_GREY_INT
   if (idx_max < 255): #{
     idx_obj_grey_type = w.WLZ_GREY_UBYTE
   elif (idx_max < 32768): #}{
     idx_obj_grey_type = w.WLZ_GREY_SHORT
   #}
   idx_obj = w.WlzAssignObject(
             w.WlzCutObjToValBox3D(tmp_obj, box,
                w.enum__WlzGreyType(idx_obj_grey_type), None,
                0, 0.0, 0.0, c.byref(errNum)), None)
   ChkWoolzError(errNum, 0)