Exemple #1
0
def run():
    global MODEL
    global TEST
    global VAL
    global DATAPATH
    global WEIGHTSPATH
    global OUTPATH
    global OUTFILE
    print "TEST: " + str(TEST) + "   VAL: " + str(VAL) + "   MODEL: " + str(
        MODEL)
    filelist, filepathlist = getFilelist(DATAPATH)
    model = seg3d.getModel()
    model.load_weights(WEIGHTSPATH)
    candlist = pd.read_csv(ANNOTATIONS)
    time1 = time.time()
    flag = False
    #for i in range(len(filelist)):
    #  print filelist[i]
    #return
    for n in range(START, len(filelist)):
        if scanids is not None:
            for scan in scanids:
                #print n
                flag = True
                if filelist[n] == scan:
                    flag = False
                    break
        if flag:
            flag = False
            continue
        t1 = time.time()
        print "FILE NO " + str(n) + "   UID: " + filelist[n]
        currentNodules = candlist[candlist['seriesuid'] == filelist[n]]
        itk = sitk.ReadImage(filepathlist[n])
        image, _ = cg.loadItk(filepathlist[n], None)
        origin = np.array(itk.GetOrigin())
        spacing = np.array(itk.GetSpacing())
        coords, d = getCoords(currentNodules, origin, spacing)
        print coords
        #TODO: ADD TO OTHER SEGMENT_x_.PY
        if len(coords) == 0:
            continue
        segmentation = cd.segmentLung(image, reseg=False)
        if cd.checkSeg(segmentation):
            segmentation = cd.segmentLung(image, reseg=True)
        mask = cd.applyMask(image, segmentation)
        segmentedImage = copy.deepcopy(image)
        segmentedImage[mask == 0] = 0
        zmin, zmax, ymin, ymax, xmin, xmax = cd.findROI(segmentedImage)
        roi = cd.crop(segmentedImage)
        if roi.shape[0] < 100 or roi.shape[1] < 100 or roi.shape[2] < 100:
            segmentation = cd.segmentLung(image, error=True)
            mask = cd.applyMask(image, segmentation)
            segmentedImage = copy.deepcopy(image)
            segmentedImage[mask == 0] = 0
            zmin, zmax, ymin, ymax, xmin, xmax = cd.findROI(segmentedImage)
            roi = cd.crop(segmentedImage)
        shift = (xmin, ymin, zmin)  #**XYZ NOT ZYX
        segShape = getSegmentationShape(
            roi.shape)  #, sixteen=SIXTEEN)#image.shape)
        lpad, hpad = getPadding(segShape, roi)  #image)
        segImage = np.zeros(segShape)
        segImage[lpad[0]:segShape[0] - hpad[0], lpad[1]:segShape[1] - hpad[1],
                 lpad[2]:segShape[2] - hpad[2]] = roi  #image
        segImage = np.clip(segImage, -1000, 1000)
        segImage += 1000
        segImage *= (255. / 2000.)
        print "SEGMENTING ..."
        stride = STRIDE
        strides = copy.deepcopy(segShape)
        strides /= 32
        print segShape
        print strides
        segmentation = segmentImage(segImage, model, strides, stride)
        #if stride == 32:
        lpad -= 16
        hpad -= 16
        #elif stride == 16:
        #  lpad -= 8
        #  hpad -= 8
        #print segmentation.shape
        #print mask.shape
        #segmentation[mask==0] = 0
        tmpseg = segmentation[lpad[0]:segmentation.shape[0] - hpad[0],
                              lpad[1]:segmentation.shape[1] - hpad[1],
                              lpad[2]:segmentation.shape[2] - hpad[2]]
        #tmpseg[mask==0] = 0
        pseg = np.zeros(image.shape)
        pseg[zmin:zmax, ymin:ymax, xmin:xmax] = tmpseg
        seg = np.zeros(pseg.shape)
        seg = np.where(pseg > THRESH, 1, 0)
        #pseg[mask==0] = 0
        #seg[mask==0] = 0
        print "ASSESSING ..."
        labels = measure.label(seg, connectivity=1)
        #if np.max(labels) >= 315:
        #  print "TOO MANY: " + str(np.max(labels))
        #  continue
        assess(OUTFILE, labels, coords, filelist[n], OUTPATH, pseg, image, d)
        t2 = time.time()
        print "LAST SCAN TIME: " + str((t2 - t1) / 60) + 'm ' + str(
            (t2 - t1) % 60) + 's'
    time2 = time.time()
    print "TOTAL TIME: " + str((time2 - time1) / 60) + 'm ' + str(
        (time2 - time1) % 60) + 's'
Exemple #2
0
    filepathlist = copy.deepcopy(filelist)
templist = []
for f in filelist:
    f = f.split('/')
    f = f[-1]
    templist.append(f[:-4])
filelist = templist
del (templist)

no = 4
candlist = pd.read_csv(annotations)
currentNodules = candlist[candlist['seriesuid'] == filelist[no]]
print currentNodules[['coordX', 'coordY', 'coordZ', 'diameter_mm']]

itk = sitk.ReadImage(filepathlist[no])
image, _ = cg.loadItk(filepathlist[no], None)
origin = np.array(itk.GetOrigin())
spacing = np.array(itk.GetSpacing())
cx = currentNodules['coordX']
cx = cx.as_matrix()
cy = currentNodules['coordY']
cy = cy.as_matrix()
cz = currentNodules['coordZ']
cz = cz.as_matrix()
di = currentNodules['diameter_mm']
di = di.as_matrix()

nno = 0
tcx = cx[nno]
tcy = cy[nno]
tcz = cz[nno]