def run(f): patient_id = os.path.basename(f)[:-len("_seg.nii.gz")] # if patient_id != "1585": # return print "PATIENT_ID",patient_id f_img = img_folder + "/" + patient_id + ".nii" if not os.path.exists(f_img): f_img += ".gz" seg = irtk.imread( f, dtype='float32', force_neurological=True ) img = irtk.imread( f_img, dtype='float32', force_neurological=True ) img = irtk.Image(nd.median_filter(img.view(np.ndarray),(3,5,5)),img.get_header()) ga = all_ga[patient_id] scale = get_CRL(ga)/get_CRL(30.0) # if all_iugr[patient_id][0] == 1: # scale = (get_weight(ga,0.02) / get_weight(30,0.5)) ** (1.0/3.0) # else: # scale = (get_weight(ga,0.5) / get_weight(30,0.5)) ** (1.0/3.0) seg = seg.resample( 1.0*scale, interpolation='nearest') img = img.resample( 1.0*scale, interpolation='bspline' ) irtk.imwrite(output_folder + "/data_resampled_weight/"+patient_id+"_img.nii.gz",img) irtk.imwrite(output_folder + "/data_resampled_weight/"+patient_id+"_seg.nii.gz",seg) return
def crop_data(f,mask,t): file_id = os.path.basename(f).split('.')[0] img = irtk.imread( f, dtype='float32',force_neurological=True ) seg = mask.transform(t.invert(), target=img,interpolation='nearest') x_min,y_min,z_min,x_max,y_max,z_max = seg.bbox() seg = seg[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] img = img[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1].rescale(0,1000) + 1.0 # +1 to avoid zeros in the heart img_file = output_dir + '/img_' + file_id + ".nii.gz" irtk.imwrite( img_file, img ) for z in range(img.shape[0]): scale = img[z].max() img[z] = restoration.nl_means_denoising(img[z].rescale(0.0,1.0).view(np.ndarray), fast_mode=False, patch_size=5, patch_distance=7, h=0.05, multichannel=False) img[z] *= scale img[seg==0] = 0 masked_file = output_dir + '/masked_' + file_id + ".nii.gz" irtk.imwrite( masked_file, img )
def sift3d( img, file_id=None, tmp_dir='tmp' ): if file_id is None: file_id = str( random.randint(1, 1000) ) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) tmp_file = tmp_dir + '/' + str(os.getpid()) + "_" + file_id + ".nii" tmp_sift = tmp_dir + '/' + str(os.getpid()) + "_" + file_id + ".txt" irtk.imwrite( tmp_file, img ) proc = subprocess.Popen([os.path.dirname(irtk.__file__) + "/ext/featExtract.ubu", tmp_file, tmp_sift], stdout=subprocess.PIPE) (out, err) = proc.communicate() print out print err os.remove( tmp_file ) f = open(tmp_sift, "r") # skipping header line1 = f.next() line2 = f.next() line3 = f.next() titles = ["x", "y", "z", "scale", # Scale-space location "o11", "o12", "o13", "o21", "o22", "o23", "o31", "o32", "o32", # orientation "e1", "e2", "e3", # 2nd moment eigenvalues "i1" # info flag ] # descriptor for i in range(64): titles.append("d"+str(i)) reader = csv.DictReader(f, titles, delimiter='\t',quoting=csv.QUOTE_NONNUMERIC) features = [] for row in reader: descr = [] for i in range(64): descr.append(float(row["d"+str(i)])) descr = np.array( descr, dtype='float' ) features.append( ( ( int(row['x']), int(row['y']), int(row['z']) ), descr ) ) return features
def predict_autocontext( self, img, mask, nb_labels, nb_autocontext, debug=False, return_all=False ): proba = np.ones((nb_labels,img.shape[0],img.shape[1],img.shape[2]),dtype='float32') proba /= nb_labels header = img.get_header() header['dim'][3] = nb_labels proba = irtk.Image(proba,header,squeeze=False) all_steps = [] for k in xrange(nb_autocontext): knowledge = self.get_knowledge(img,proba,mask=mask) if debug: irtk.imwrite("knowledge_"+str(k)+".nii.gz", knowledge) forest = integralForest( folder=self.folder(k), test=self.params['test'], parallel=self.params['parallel'], nb_knowledge_layers=knowledge.shape[0] ) proba = forest.predict_autocontext( img, knowledge, mask, self.params['ksampling'] ) proba = irtk.Image(proba,header,squeeze=False) if return_all: all_steps.append( proba.copy() ) if debug: irtk.imwrite("debug_"+str(k)+".nii.gz", proba) if k < 1: for i in xrange(proba.shape[0]): if i == 0: proba[i] = 0 else: proba[i] = self.get_center(proba[i]) if debug: print "done autocontext", k # irtk.imwrite("debug_rescaled_"+str(k)+".nii.gz", proba) if not return_all: return proba else: return all_steps
def mean_shift_selection( votes, n_points=5000, bandwidth=10, n=10, cutoff=None, debug=False, debug_output="./" ): points = np.argwhere(votes) probas = votes[points[:,0], points[:,1], points[:,2]] points, probas = random_pick(points,probas,n_points) ms = MeanShift(bandwidth=bandwidth) ms.fit(points) weights = np.zeros( ms.cluster_centers_.shape[0], dtype='float' ) for i,c in enumerate(ms.cluster_centers_): weights[i] = np.sum(probas[ms.labels_==i]) detection = ms.cluster_centers_[np.argsort(weights)[::-1]] #points = points[np.argsort(weights)[::-1]] #ms.labels_ = ms.labels_[np.argsort(weights)[::-1]] weights = np.sort(weights)[::-1] weights /= weights.max() if debug: print weights.tolist() res = irtk.zeros(votes.get_header(),dtype='int') points = np.array(detection,dtype='int') if points.shape[0] > 30: points = points[:30] print np.arange(1,int(points.shape[0]+1))[::-1] res[points[:,0], points[:,1], points[:,2]] = np.arange(1,int(points.shape[0]+1))#[::-1] irtk.imwrite(debug_output+"/clusters.nii.gz",res) irtk.imwrite(debug_output+"/clusters_centers.nii.gz",irtk.landmarks_to_spheres(res,r=5)) if cutoff is not None: selected = np.sum( weights >= cutoff ) detection = detection[:selected] weights = weights[:selected] else: if len(detection) > n: detection = detection[:n] weights = weights[:n] return detection, weights
def flirt_registration( img_src, img_tgt, FSL_BIN="/vol/vipdata/packages/fsl-5.0.1/bin/", dof=7, coarsesearch=60, finesearch=18, verbose=False, cost="corratio"): flirt = FSL_BIN+"flirt" fh1, source_file = tempfile.mkstemp(suffix=".nii.gz") fh2, target_file = tempfile.mkstemp(suffix=".nii.gz") fh3, flirt_output = tempfile.mkstemp(suffix=".txt") irtk.imwrite( source_file, img_src ) irtk.imwrite( target_file, img_tgt ) cmd = [ flirt, "-cost", cost, "-searchcost", cost, "-coarsesearch", str(coarsesearch), "-finesearch", str(finesearch), "-searchrx", "-180", "180", "-searchry", "-180", "180", "-searchrz", "-180", "180", "-dof", str(dof), "-in", source_file, "-ref", target_file, # "-inweight", "tmp/"+patient_id+"_img.nii.gz", # "-refweight", "tmp/"+patient_id+"_atlas.nii.gz", #"-schedule", "simple3D.sch", "-omat", flirt_output ] utils.run_cmd(cmd, verbose=verbose) flirt_matrix = np.loadtxt(flirt_output) transformation = flirt2dof( flirt_matrix, img_tgt, img_src, return_matrix=False ) # clean os.close(fh1) os.close(fh2) os.close(fh3) os.remove(flirt_output) os.remove(source_file) os.remove(target_file) return transformation
def mask_data(f): file_id = f.split('/')[-3] seg = irtk.imread(f,force_neurological=True) > 0 r = 10 x_min,y_min,z_min,x_max,y_max,z_max = seg.bbox() seg = seg[max(0,z_min-3*r):min(z_max+3*r+1,seg.shape[0]), max(0,y_min-3*r):min(y_max+3*r+1,seg.shape[1]), max(0,x_min-3*r):min(x_max+3*r+1,seg.shape[2])] ball = morphology.ball( 5 ) seg = irtk.Image( nd.binary_dilation(seg,ball), seg.get_header() ) ball = morphology.ball( r ) seg = irtk.Image( nd.binary_closing(seg,ball), seg.get_header() ) seg = seg.bbox(crop=True) seg_file = output_dir + '/seg_' + file_id + ".nii.gz" irtk.imwrite( seg_file, seg )
def create_mask_from_all_masks(f_lists,transformations,ga,resolution=0.75): points = [] for f, t in zip(f_lists,transformations): m = irtk.imread(f,force_neurological=True) points.extend( t.apply(get_corners(m)) ) points = np.array(points,dtype='float64') x_min, y_min, z_min = points.min(axis=0) x_max, y_max, z_max = points.max(axis=0) pixelSize = [resolution, resolution, resolution, 1] orientation = np.eye( 3, dtype='float64' ) origin = [ x_min + (x_max - x_min)/2, y_min + (y_max - y_min)/2, z_min + (z_max - z_min)/2, 0 ] dim = [ (x_max - x_min)/resolution, (y_max - y_min)/resolution, (z_max - z_min)/resolution, 1 ] header = irtk.new_header( pixelSize=pixelSize, orientation=orientation, origin=origin, dim=dim ) mask = irtk.zeros( header, dtype='float32' ) for f, t in zip( f_lists, transformations ): m = irtk.imread(f,force_neurological=True).transform(t, target=mask,interpolation="linear") mask += m irtk.imwrite( "debug_mask1.nii.gz", mask) mask = irtk.Image( nd.gaussian_filter( mask, 0.5 ), mask.get_header() ) irtk.imwrite( "debug_mask2.nii.gz", mask) mask = (mask > 0).bbox(crop=True).astype('uint8') scale = get_CRL(ga)/get_CRL(30.0) template = irtk.imread(f_template,force_neurological=True) template.header['pixelSize'][:3] /= scale template = template.transform(target=mask,interpolation='nearest') mask[template==0] = 0 irtk.imwrite( "debug_template.nii.gz", template) irtk.imwrite( "debug_mask3.nii.gz", mask) return mask
def align_to_template(f,f_template,output_folder,ga): file_id = f.split('/')[-3] landmarks = irtk.imread(f,force_neurological=True) scale = get_CRL(ga)/get_CRL(30.0) template = irtk.imread(f_template,force_neurological=True) template.header['pixelSize'][:3] /= scale points = [] points_template = [] for i,j in zip( [2,8,3,4,5], [5,4,1,2,3] ): points_template.append( template.ImageToWorld( nd.center_of_mass(template.view(np.ndarray)==i)[::-1] ) ) points.append( landmarks.ImageToWorld( nd.center_of_mass(landmarks.view(np.ndarray)==j)[::-1] ) ) t,rms = irtk.registration_rigid_points( np.array(points), np.array(points_template), rms=True ) print "RMS: ", rms t.invert().write( output_folder + '/' + file_id + '.dof' ) landmarks = landmarks.transform(t,target=template) irtk.imwrite( output_folder + '/landmarks_' + file_id + '.nii.gz',landmarks ) return t
def run(f): patient_id = os.path.basename(f)[:-len("_seg.nii.gz")] print "PATIENT_ID",patient_id f_img = data_folder + "/" + patient_id + ".nii" if not os.path.exists(f_img): f_img += ".gz" seg = irtk.imread( f, dtype='float32', force_neurological=True ) img = irtk.imread( f_img, dtype='float32', force_neurological=True ) img = irtk.Image(nd.median_filter(img.view(np.ndarray),(3,5,5)),img.get_header()) ga = all_ga[patient_id] scale = get_CRL(ga)/get_CRL(30.0) OFD = get_OFD(30.0) BPD = get_BPD(30.0) CRL = get_CRL(30.0) brain_center = seg.ImageToWorld( np.array(nd.center_of_mass( (seg == 2).view(np.ndarray) ), dtype='float32')[::-1] ) header = img.get_header() header['origin'][:3] = brain_center header['pixelSize'][:3] = 1.0*scale header['dim'][0] = CRL header['dim'][1] = CRL header['dim'][2] = CRL img = img.transform( target=header, interpolation="bspline" ) seg = seg.transform( target=header, interpolation="nearest" ) img[img<1.0] = 0 irtk.imwrite(output_folder + "brain_center/"+patient_id+"_img.nii.gz",img) irtk.imwrite(output_folder + "brain_center/"+patient_id+"_seg.nii.gz",seg) return
print "preprocessing..." img = irtk.imread( args.input, dtype='float32', force_neurological=True ) grad = irtk.Image(nd.gaussian_gradient_magnitude( img, 0.5 ), img.get_header()) sat = integral_image(img) sat_grad = integral_image(grad) blurred_img = nd.gaussian_filter(img,0.5) gradZ = nd.sobel( blurred_img, axis=0 ).astype('float32') gradY = nd.sobel( blurred_img, axis=1 ).astype('float32') gradX = nd.sobel( blurred_img, axis=2 ).astype('float32') irtk.imwrite(args.output + "/img.nii.gz", img) irtk.imwrite(args.output + "/grad.nii.gz", grad) print "done preprocessing" del blurred_img del grad args.brain_center = np.array(args.brain_center, dtype='float32') print args.brain_center narrow_band = get_narrow_band( img.shape, args.brain_center, r_min=40, r_max=120 )
import irtk from wrapping import rif f = "/vol/vipdata/data/fetal_data/motion_correction/original_scans/3879_5.nii" img = irtk.imread(f, dtype='float32').resample(5.0) irtk.imwrite( "toto.nii.gz", img ) print img.shape features = rif.extractRIF(img) print features.shape irtk.imwrite("features.nii.gz", features.rescale(0,1000) ) f = "/vol/vipdata/data/fetal_data/motion_correction/original_scans/3879_5.nii" img = irtk.imread(f, dtype='float32').resample(2.0) irtk.imwrite( "toto2.nii.gz", img ) print img.shape features = rif.extractRIF(img) print features.shape irtk.imwrite("features2.nii.gz", features[features.shape[0]/2].rescale(0,1000))
def get_noiseZ(img): img = img.astype('float32') new_img = np.zeros(img.shape, dtype='float32') for x in xrange(img.shape[2]): new_img[:, :, x] = nd.gaussian_filter(img[:, :, x], 2, mode='reflect') noise = img - new_img #print "Noise Z:", noise.std(), img.std() return noise.std() output_filename = sys.argv[3] img = irtk.imread(sys.argv[1], dtype='float64').saturate() mask = irtk.imread(sys.argv[2], dtype='int16') mask = irtk.Image(mask, img.get_header()) # crop x_min, y_min, z_min, x_max, y_max, z_max = mask.bbox() mask = mask[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] tmp_img = img[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] downsampled_img = tmp_img.resample(2) mask = mask.transform(target=downsampled_img, interpolation='nearest') seg = irtk.graphcut(downsampled_img, mask, sigma=get_noiseXY(downsampled_img), sigmaZ=get_noiseZ(downsampled_img)) irtk.imwrite(sys.argv[3], seg.transform(target=img, interpolation='nearest'))
def mask_image( file_img, file_mask, ga, r, neigh, output_dir ): img = irtk.imread( file_img, dtype='float32' ) input_mask = irtk.imread( file_mask ) print "predicting..." res = irtk.zeros( img.get_header(), dtype='float32' ) res2 = irtk.zeros( img.get_header(), dtype='float32' ) res3 = irtk.zeros( img.get_header(), dtype='float32' ) res4 = irtk.zeros( img.get_header(), dtype='uint8' ) mask = irtk.ones( input_mask.get_header(), dtype='uint8' ) mask[input_mask == 2] = 0 for z in xrange(img.shape[0]): print z YX = np.transpose( np.nonzero( mask[z] ) ) if YX.shape[0] == 0: continue # this slice does not intersect the box patches = extract_patches2D( img[z], r, YX ) patches = np.reshape( patches, (patches.shape[0],patches.shape[1]*patches.shape[2]) ) predictions = neigh.predict_proba(patches)[:,1] res[z,YX[:,0],YX[:,1]] = predictions x_min, y_min, z_min, x_max, y_max, z_max = mask.bbox() proba = res[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] if args.mass: BV = get_BV( args.ga ) box_volume = (z_max-z_min)*img.header['pixelSize'][2]*(y_max-y_min)*img.header['pixelSize'][1]*(x_max-x_min)*img.header['pixelSize'][0] ratio = float(BV) / float(box_volume) print "ratio", ratio q0,q1 = mquantiles( proba.flatten(), prob=[0.5*(1.0-ratio), 1.0-0.5*ratio] ) print "threshold", q0,q1 #threshold = max(0.5,threshold) # labels = res[z_min:z_max+1, # y_min:y_max+1, # x_min:x_max+1] > threshold #res = 1 / (np.exp(-(res-threshold)/(res.max()-res.min()))) res[res<q0] = q0 res[res>q1] = q1 res -= res.min() res /= res.max() labels = res[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] > 0.5 proba = res[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] cropped_img = img[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] if args.do_3D: labels = irtk.crf( cropped_img, labels, proba, l=args.l, sigma=get_noiseXY(cropped_img), sigmaZ=get_noiseZ(cropped_img) ) # elif args.do_patchZ: # labels = irtk.crf_patchZ( cropped_img, # labels, # proba, # l=10.0 ) # else: # for z in xrange(z_min,z_max+1): # labels[z] = irtk.crf( cropped_img[z], # labels[z], # proba[z], # l=1.0 ) print "MAX LABEL:", labels.max() irtk.imwrite(output_dir + "/bare_"+os.path.basename(file_img), labels ) tmp = irtk.zeros( img.get_header(), dtype='uint8' ) tmp[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] = labels ( min_x_bare, min_y_bare, min_z_bare, max_x_bare, max_y_bare, max_z_bare ) = tmp.bbox() if not args.no_cleaning: # clean by fitting ellipses enlarged of 10% for z in xrange(labels.shape[0]): edges = nd.morphological_gradient( labels[z] > 0,size=5 ) points = np.transpose(edges.nonzero())[:,::-1] if len(points) == 0: continue points = np.array(map(lambda x:[x],points),dtype='int32') ellipse = cv2.fitEllipse(points) cv2.ellipse( labels[z], (ellipse[0], (1.1*ellipse[1][0],1.1*ellipse[1][1]), ellipse[2]) , 1, -1 ) irtk.imwrite(output_dir + "/seg_"+os.path.basename(file_img), labels ) irtk.imwrite(output_dir + "/res_"+os.path.basename(file_img), res ) # re-read the image in case we processed it img = irtk.imread( file_img, dtype='float32' ) cropped_img = img[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] cropped_img[labels==0] = -1 masked = cropped_img.bbox(crop=True) irtk.imwrite(output_dir + "/masked_"+os.path.basename(file_img), masked ) # re-read the image in case we processed it img = irtk.imread( file_img, dtype='float32' ) x0 = min_x_bare + (max_x_bare - min_x_bare) / 2 y0 = min_y_bare + (max_y_bare - min_y_bare) / 2 ofd = get_OFD(ga)/img.header['pixelSize'][0] cropped_img = img[min_z_bare:max_z_bare+1, max(0,int(round(y0-ofd/2))):min(img.shape[1],int(round(y0+ofd/2+1))), max(0,int(round(x0-ofd/2))):min(img.shape[2],int(round(x0+ofd/2+1)))].copy() irtk.imwrite(output_dir + "/very_large_"+os.path.basename(file_img), cropped_img ) cropped_proba = res[min_z_bare:max_z_bare+1, max(0,int(round(y0-ofd/2))):min(img.shape[1],int(round(y0+ofd/2+1))), max(0,int(round(x0-ofd/2))):min(img.shape[2],int(round(x0+ofd/2+1)))].copy() irtk.imwrite(output_dir + "/proba_"+os.path.basename(file_img), cropped_proba )
def rand(scale): return float(scale) * (np.random.random(1) - 0.5) * 2 img = irtk.imread(sys.argv[1]) seg = irtk.imread(sys.argv[2]) prefix = sys.argv[3] tx, ty, tz = img.ImageToWorld([(img.shape[2] - 1) / 2, (img.shape[1] - 1) / 2, (img.shape[0] - 1) / 2]) centering = irtk.RigidTransformation(tx=-tx, ty=-ty, tz=-tz) t = irtk.RigidTransformation(tx=rand(img.header['pixelSize'][0] * 10), ty=rand(img.header['pixelSize'][1] * 10), tz=rand(img.header['pixelSize'][2] * 10), rx=rand(30), ry=rand(30), rz=rand(30)) print t t = centering.invert() * t * centering new_img = img.transform(t, target=img.get_header(), interpolation='linear') new_seg = seg.transform(t, target=img.get_header(), interpolation='nearest') irtk.imwrite( "data_augmentation_" + prefix + "_" + os.path.basename(sys.argv[1]), new_img) irtk.imwrite( "data_augmentation_" + prefix + "_" + os.path.basename(sys.argv[2]), new_seg)
def sift3d(img, file_id=None, tmp_dir='tmp'): if file_id is None: file_id = str(random.randint(1, 1000)) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) tmp_file = tmp_dir + '/' + str(os.getpid()) + "_" + file_id + ".nii" tmp_sift = tmp_dir + '/' + str(os.getpid()) + "_" + file_id + ".txt" irtk.imwrite(tmp_file, img) proc = subprocess.Popen([ os.path.dirname(irtk.__file__) + "/ext/featExtract.ubu", tmp_file, tmp_sift ], stdout=subprocess.PIPE) (out, err) = proc.communicate() print out print err os.remove(tmp_file) f = open(tmp_sift, "r") # skipping header line1 = f.next() line2 = f.next() line3 = f.next() titles = [ "x", "y", "z", "scale", # Scale-space location "o11", "o12", "o13", "o21", "o22", "o23", "o31", "o32", "o32", # orientation "e1", "e2", "e3", # 2nd moment eigenvalues "i1" # info flag ] # descriptor for i in range(64): titles.append("d" + str(i)) reader = csv.DictReader(f, titles, delimiter='\t', quoting=csv.QUOTE_NONNUMERIC) features = [] for row in reader: descr = [] for i in range(64): descr.append(float(row["d" + str(i)])) descr = np.array(descr, dtype='float') features.append(((int(row['x']), int(row['y']), int(row['z'])), descr)) return features
import sys,os import irtk import numpy as np def rand(scale): return float(scale)*(np.random.random(1)-0.5)*2 img = irtk.imread(sys.argv[1]) seg = irtk.imread(sys.argv[2]) prefix = sys.argv[3] tx,ty,tz = img.ImageToWorld( [(img.shape[2]-1)/2, (img.shape[1]-1)/2, (img.shape[0]-1)/2] ) centering = irtk.RigidTransformation( tx=-tx, ty=-ty, tz=-tz ) t = irtk.RigidTransformation( tx=rand(img.header['pixelSize'][0]*10), ty=rand(img.header['pixelSize'][1]*10), tz=rand(img.header['pixelSize'][2]*10), rx=rand(30), ry=rand(30), rz=rand(30) ) print t t = centering.invert()*t*centering new_img = img.transform(t,target=img.get_header(),interpolation='linear') new_seg = seg.transform(t,target=img.get_header(),interpolation='nearest') irtk.imwrite( "data_augmentation_"+prefix+"_"+os.path.basename(sys.argv[1]), new_img ) irtk.imwrite( "data_augmentation_"+prefix+"_"+os.path.basename(sys.argv[2]), new_seg )
def mask_image(file_img, file_mask, ga, r, neigh, output_dir): img = irtk.imread(file_img, dtype='float32') input_mask = irtk.imread(file_mask) print "predicting..." res = irtk.zeros(img.get_header(), dtype='float32') res2 = irtk.zeros(img.get_header(), dtype='float32') res3 = irtk.zeros(img.get_header(), dtype='float32') res4 = irtk.zeros(img.get_header(), dtype='uint8') mask = irtk.ones(input_mask.get_header(), dtype='uint8') mask[input_mask == 2] = 0 for z in xrange(img.shape[0]): print z YX = np.transpose(np.nonzero(mask[z])) if YX.shape[0] == 0: continue # this slice does not intersect the box patches = extract_patches2D(img[z], r, YX) patches = np.reshape( patches, (patches.shape[0], patches.shape[1] * patches.shape[2])) predictions = neigh.predict_proba(patches)[:, 1] res[z, YX[:, 0], YX[:, 1]] = predictions x_min, y_min, z_min, x_max, y_max, z_max = mask.bbox() proba = res[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] if args.mass: BV = get_BV(args.ga) box_volume = (z_max - z_min) * img.header['pixelSize'][2] * ( y_max - y_min) * img.header['pixelSize'][1] * ( x_max - x_min) * img.header['pixelSize'][0] ratio = float(BV) / float(box_volume) print "ratio", ratio q0, q1 = mquantiles(proba.flatten(), prob=[0.5 * (1.0 - ratio), 1.0 - 0.5 * ratio]) print "threshold", q0, q1 #threshold = max(0.5,threshold) # labels = res[z_min:z_max+1, # y_min:y_max+1, # x_min:x_max+1] > threshold #res = 1 / (np.exp(-(res-threshold)/(res.max()-res.min()))) res[res < q0] = q0 res[res > q1] = q1 res -= res.min() res /= res.max() labels = res[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] > 0.5 proba = res[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] cropped_img = img[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] if args.do_3D: labels = irtk.crf(cropped_img, labels, proba, l=args.l, sigma=get_noiseXY(cropped_img), sigmaZ=get_noiseZ(cropped_img)) # elif args.do_patchZ: # labels = irtk.crf_patchZ( cropped_img, # labels, # proba, # l=10.0 ) # else: # for z in xrange(z_min,z_max+1): # labels[z] = irtk.crf( cropped_img[z], # labels[z], # proba[z], # l=1.0 ) print "MAX LABEL:", labels.max() irtk.imwrite(output_dir + "/bare_" + os.path.basename(file_img), labels) tmp = irtk.zeros(img.get_header(), dtype='uint8') tmp[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] = labels (min_x_bare, min_y_bare, min_z_bare, max_x_bare, max_y_bare, max_z_bare) = tmp.bbox() if not args.no_cleaning: # clean by fitting ellipses enlarged of 10% for z in xrange(labels.shape[0]): edges = nd.morphological_gradient(labels[z] > 0, size=5) points = np.transpose(edges.nonzero())[:, ::-1] if len(points) == 0: continue points = np.array(map(lambda x: [x], points), dtype='int32') ellipse = cv2.fitEllipse(points) cv2.ellipse( labels[z], (ellipse[0], (1.1 * ellipse[1][0], 1.1 * ellipse[1][1]), ellipse[2]), 1, -1) irtk.imwrite(output_dir + "/seg_" + os.path.basename(file_img), labels) irtk.imwrite(output_dir + "/res_" + os.path.basename(file_img), res) # re-read the image in case we processed it img = irtk.imread(file_img, dtype='float32') cropped_img = img[z_min:z_max + 1, y_min:y_max + 1, x_min:x_max + 1] cropped_img[labels == 0] = -1 masked = cropped_img.bbox(crop=True) irtk.imwrite(output_dir + "/masked_" + os.path.basename(file_img), masked) # re-read the image in case we processed it img = irtk.imread(file_img, dtype='float32') x0 = min_x_bare + (max_x_bare - min_x_bare) / 2 y0 = min_y_bare + (max_y_bare - min_y_bare) / 2 ofd = get_OFD(ga) / img.header['pixelSize'][0] cropped_img = img[min_z_bare:max_z_bare + 1, max(0, int(round(y0 - ofd / 2)) ):min(img.shape[1], int(round(y0 + ofd / 2 + 1))), max(0, int(round(x0 - ofd / 2)) ):min(img.shape[2], int(round(x0 + ofd / 2 + 1)))].copy() irtk.imwrite(output_dir + "/very_large_" + os.path.basename(file_img), cropped_img) cropped_proba = res[min_z_bare:max_z_bare + 1, max(0, int(round(y0 - ofd / 2)) ):min(img.shape[1], int(round(y0 + ofd / 2 + 1))), max(0, int(round(x0 - ofd / 2)) ):min(img.shape[2], int(round(x0 + ofd / 2 + 1)))].copy() irtk.imwrite(output_dir + "/proba_" + os.path.basename(file_img), cropped_proba)
img_file = output_dir + '/img_' + file_id + ".nii.gz" img_files.append(img_file) masked_file = output_dir + '/masked_' + file_id + ".nii.gz" masked_files.append(masked_file) landmark_file = detection_folder + "/" + file_id + "/prediction_2/landmarks.nii.gz" landmark_files.append( landmark_file ) transformation_file = output_dir + '/' + file_id + ".dof" transformation_files.append(transformation_file) Parallel(n_jobs=-1)(delayed(mask_data)(f) for f in files) transformations = Parallel(n_jobs=-1)(delayed(align_to_template)(f,f_template,output_dir,ga) for f in landmark_files) mask_file = output_dir + '/' + patient_id + "_mask.nii.gz" mask = create_mask_from_all_masks(seg_files, transformations, ga,resolution=0.75) irtk.imwrite( mask_file, mask ) Parallel(n_jobs=-1)(delayed(crop_data)(f,mask,t) for f,t in zip(original_files,transformations)) cmd = ( ["/usr/bin/time", "--verbose", "--output", patient_id + "_masking.time", reconstruction_binary] + [ "-o", "reconstruction_" + patient_id + ".nii.gz", "-m", mask_file, "-i"] + img_files # + ["-dofin"] + transform + ['-t'] + transformation_files + ["--log_prefix", patient_id + "_masking",
#!/usr/bin/python import irtk import sys import scipy.ndimage as nd input_file = sys.argv[1] output_file = sys.argv[2] img = irtk.imread( input_file, dtype='float32' ) img = irtk.Image( nd.median_filter(img.get_data(), 5), img.get_header() ) irtk.imwrite(output_file, img )
# irtk.imwrite(output_dir + "/mask.nii", mask ) # irtk.imwrite(output_dir + "/mask.vtk", mask ) #x,y,z = img.WorldToImage(center) x, y, z = center x = int(round(x / img.header['pixelSize'][0])) y = int(round(y / img.header['pixelSize'][1])) z = int(round(z / img.header['pixelSize'][2])) w = h = int(round(ofd / img.header['pixelSize'][0])) d = int(round(ofd / img.header['pixelSize'][2])) print z, y, x print w, h, d # cropped = img[max(0,z-d/2):min(img.shape[0],z+d/2+1), # max(0,y-h/2):min(img.shape[1],y+h/2+1), # max(0,x-w/2):min(img.shape[2],x+w/2+1)] #irtk.imwrite(output_dir + "/cropped.nii", cropped ) #irtk.imwrite(output_dir + "/cropped.vtk", cropped ) neg_mask[max(0, z - d / 2):min(img.shape[0], z + d / 2 + 1), max(0, y - h / 2):min(img.shape[1], y + h / 2 + 1), max(0, x - w / 2):min(img.shape[2], x + w / 2 + 1)] = 0 mask[neg_mask > 0] = 2 print mask, mask.max() irtk.imwrite(output_mask, mask)
tmp_img = img[max(0,z_min-args.narrow_band-1):min(img.shape[0],z_max+args.narrow_band+1+1), max(0,y_min-args.narrow_band-1):min(img.shape[1],y_max+args.narrow_band+1+1), max(0,x_min-args.narrow_band-1):min(img.shape[2],x_max+args.narrow_band+1+1)] background = (nd.binary_dilation( tmp_seg>0, structure=morphology.ball(args.narrow_band) ) == 0).astype('int32') if args.thorax: tmp_seg[background>0] = 4 else: tmp_seg[background>0] = 5 if args.debug: debug_seg = tmp_seg.transform(target=img,interpolation='nearest') debug_seg[irtk.largest_connected_component(debug_seg==0)>0] = debug_seg.max() irtk.imwrite("debug_seg.nii.gz",debug_seg) irtk.imwrite("debug_background.nii.gz",debug_seg!=5) tmp_img = tmp_img.rescale(-1,1) labels = random_walker( tmp_img.view(np.ndarray), tmp_seg.view(np.ndarray), beta=1000, mode='cg_mg', return_full_prob=False ) header = tmp_img.get_header() #header['dim'][3] = 5 if args.thorax: labels[labels==4] = 0 else:
mask[ellipse_mask == 1] = 1 mask = mask.transform(target=img, interpolation='nearest' ) #irtk.imwrite("mask.nii.gz",mask) # fill holes, close and dilate disk_close = morphology.disk( 5 ) disk_dilate = morphology.disk( 2 ) for z in xrange(mask.shape[0]): mask[z] = nd.binary_fill_holes( mask[z] ) mask[z] = nd.binary_closing( mask[z], disk_close ) mask[z] = nd.binary_dilation( mask[z], disk_dilate ) neg_mask = np.ones(mask.shape, dtype='uint8')*2 x,y,z = img.WorldToImage(center) ofd = get_OFD(ga,centile=95) w = h = int(round( ofd / img.header['pixelSize'][0])) d = int(round( ofd / img.header['pixelSize'][2])) neg_mask[max(0,z-d/2):min(img.shape[0],z+d/2+1), max(0,y-h/2):min(img.shape[1],y+h/2+1), max(0,x-w/2):min(img.shape[2],x+w/2+1)] = 0 mask[neg_mask>0] = 2 irtk.imwrite(output_mask, mask )
for f in all_frames: if "_seg" in f: continue if not args.time: print f all_proba = heartdetector.predict( detector, f, ga=0.0, nb_autocontext=args.nb_autocontext, mask=mask, debug=args.debug, return_all=not args.time ) if isinstance(all_proba,irtk.Image): irtk.imwrite("predictions/"+args.patient_id+"/iter"+str(args.nb_autocontext)+"_"+os.path.basename(f),all_proba) irtk.imwrite("predictions/"+args.patient_id+"/iter"+str(args.nb_autocontext)+"_"+os.path.basename(f)[:-len('.nii.gz')] +"_hard.nii.gz", detector.groups[0].hard_thresholding( all_proba[1].resample(0.0005), smoothing=4.0*0.001 ) ) else: for i,proba in enumerate(all_proba,start=1): irtk.imwrite("predictions/"+args.patient_id+"/iter"+str(i)+"_"+os.path.basename(f),proba) irtk.imwrite("predictions/"+args.patient_id+"/iter"+str(i)+"_"+os.path.basename(f)[:-len('.nii.gz')] +"_hard.nii.gz", detector.groups[0].hard_thresholding( proba[1].resample(0.0005), smoothing=4.0*0.001 ) ) stop = time()
for f in all_frames: if "_seg" in f: continue if not args.time: print f all_proba = heartdetector.predict(detector, f, ga=0.0, nb_autocontext=args.nb_autocontext, mask=mask, debug=args.debug, return_all=not args.time) if isinstance(all_proba, irtk.Image): irtk.imwrite( "predictions/" + args.patient_id + "/iter" + str(args.nb_autocontext) + "_" + os.path.basename(f), all_proba) irtk.imwrite( "predictions/" + args.patient_id + "/iter" + str(args.nb_autocontext) + "_" + os.path.basename(f)[:-len('.nii.gz')] + "_hard.nii.gz", detector.groups[0].hard_thresholding(all_proba[1].resample(0.0005), smoothing=4.0 * 0.001)) else: for i, proba in enumerate(all_proba, start=1): irtk.imwrite( "predictions/" + args.patient_id + "/iter" + str(i) + "_" + os.path.basename(f), proba) irtk.imwrite( "predictions/" + args.patient_id + "/iter" + str(i) + "_" +
def run_detection( filename, ga, output_folder ): file_id = os.path.basename(filename).split('.')[0] if '_' in os.path.basename(filename): patient_id = file_id.split('_')[0] else: patient_id = file_id print patient_id # brain detection vocabulary = "../brain-detector/trained_model/vocabulary_0.npy" mser_detector = "../brain-detector/trained_model/mser_detector_0_LinearSVC" mask_file = output_folder +"/" + file_id + "/brain_mask.nii.gz" cmd = [ "python", "../brain-detector/fetalMask_detection.py", filename, str(ga), mask_file, "--classifier", mser_detector, "--vocabulary", vocabulary ] print ' '.join(cmd) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) (out, err) = proc.communicate() print out print err # heart and body detection ## preprocessing (resampling+denoising) img = irtk.imread( filename, dtype='float32', force_neurological=True ) img = irtk.Image(nd.median_filter(img.view(np.ndarray),(3,5,5)),img.get_header()) scale = get_CRL(ga)/get_CRL(30.0) img = img.resample( 1.0*scale, interpolation='bspline' ) brain_center = img.WorldToImage( get_center_brain_detection(mask_file) )[::-1] new_filename = output_folder + "/" + file_id + "/" + os.path.basename(filename) irtk.imwrite(new_filename,img) n_jobs = 5 output_folder1 = output_folder + "/" + file_id + "/prediction_1/" output_folder2 = output_folder + "/" + file_id + "/prediction_2" detector1 = "trained_model/stage1" detector2 = "trained_model/stage2" shape_model = "trained_model/stage1/shape_model.pk" cmd = [ "python", "predict.py", "--input", new_filename, "--output", output_folder1, "--output2", output_folder2, "--detector", detector1, "--detector2", detector2, "--padding", str(10), "--chunk_size", str(int(1e5)), "--n_jobs", str(n_jobs), "--brain_center"] + map(str,brain_center) + \ ["--shape_model",shape_model, "-l", str(0.5), "--shape_optimisation", "--narrow_band", "--selective"] print ' '.join(cmd) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (out, err) = proc.communicate() print out print err return
#!/usr/bin/python import irtk import cv2 mask = irtk.imread("mask.nii", dtype='uint8') irtk.imwrite("mask.png", mask) img = irtk.Image(cv2.imread("lena.png", 0)) irtk.imshow(img, mask, filename="initialisation.png", colors={ 1: (255, 0, 0), 2: (0, 255, 0) }, opacity=1.0) mask2 = irtk.imread("mask2.nii", dtype='uint8') irtk.imwrite("mask2.png", mask2) irtk.imshow(img, mask2, filename="initialisation2.png", colors={ 1: (255, 0, 0), 2: (0, 255, 0) }, opacity=1.0)
proba[1] += proba[2] # #proba[2] = proba[3] header = proba.get_header() header['dim'][3] = 2 proba = proba[:2] proba = irtk.Image(proba,header)#.saturate(10,90).rescale(0,1) proba[1] = nd.gaussian_filter(proba[1],1.0) proba[1] /= proba[1].max() proba[0] = 1.0 - proba[1] # #proba[proba>1] = 1 irtk.imwrite( "proba_debug.nii.gz", proba ) # seg = irtk.zeros( img.get_header(), dtype='int32' ) # for i in [1,2]: # seg[proba[i] > 0.5] = i if args.seg is None: seg_init = irtk.Image( np.argmax(proba,axis=0).astype('int32'), img.get_header() ) else: seg_init = irtk.imread( args.seg, dtype='int32', force_neurological=True ) seg_init[seg_init>2] = 0 seg_init[seg_init==2] = 1 irtk.imwrite("seg_debug.nii.gz",seg_init)
for i in range(1,5): tmp_seg[nd.binary_closing(tmp_seg==i, structure=morphology.ball(2))>0] = i background = (nd.binary_dilation( tmp_seg>0, structure=ball ) == 0).astype('int32') header = tmp_img.get_header() header['dim'][3] = 5 proba = irtk.zeros( header, dtype='float32' ) proba[0][tmp_seg==0] = 1.0 for i in range(1,5): proba[i][tmp_seg==i] = 1.0 irtk.imwrite( "proba.nii.gz", proba ) print proba.header, proba.shape tmp_seg = irtk.crf( tmp_img, tmp_seg, proba, l=4.0, sigma=50 ) res = tmp_seg.transform( target=seg, interpolation='nearest' ) print np.unique(res) irtk.imwrite( args.output, res )
#!/usr/bin/python import irtk from irtk.vtk2irtk import read_polydata, voxellise from irtk.sitkreader import sitk_read import sys vtk_file = sys.argv[1] img_file = sys.argv[2] nifti_file = sys.argv[3] points,triangles = read_polydata( vtk_file ) img = sitk_read( img_file ) print img.header, img.shape img = voxellise(points,triangles,img.get_header()) irtk.imwrite(nifti_file,img)
img = irtk.imread( args.input, dtype='float32', force_neurological=True ) grad = irtk.Image(nd.gaussian_gradient_magnitude( img, 0.5 ), img.get_header()) sat = integral_image(img) sat_grad = integral_image(grad) blurred_img = nd.gaussian_filter(img,0.5) gradZ = nd.sobel( blurred_img, axis=0 ).astype('float32') gradY = nd.sobel( blurred_img, axis=1 ).astype('float32') gradX = nd.sobel( blurred_img, axis=2 ).astype('float32') if not args.score: irtk.imwrite(args.output + "/img.nii.gz", img) irtk.imwrite(args.output + "/grad.nii.gz", grad) del blurred_img del grad if args.verbose: print "preprocessing done" args.brain_center = np.array(args.brain_center,dtype='float32') args.heart_center = np.array(args.heart_center,dtype='float32') narrow_band = get_narrow_band( img.shape, args.heart_center, r_max=50 ) narrow_band[img==0] = 0 narrow_band = irtk.Image(narrow_band,img.get_header())
args = parser.parse_args() print args output_dir = os.path.dirname(args.output) if not os.path.exists(output_dir): os.makedirs(output_dir) template = irtk.imread(args.template,force_neurological=True) img = irtk.imread(args.input,force_neurological=True) if args.ga > 0: scale = get_CRL(args.ga)/get_CRL(30.0) resized_img = img.resample( 1.0*scale, interpolation='bspline' ).rescale(0,1000) resized_input = output_dir + "/resized_" + os.path.basename(args.input) irtk.imwrite( resized_input, resized_img ) heart_center = resized_img.WorldToImage([0,0,0])[::-1]#np.array(resized_img.shape,dtype='float32')/2 if not args.brain_center: brain_center = heart_center + np.array([0,0,100]) else: brain_center = np.array(args.brain_center,dtype='float32') else: resized_input = args.input heart_center = np.array(img.shape,dtype='float32')/2 if not args.brain_center: brain_center = heart_center + np.array([0,0,100]) else: brain_center = np.array(args.brain_center,dtype='float32')
#!/usr/bin/python import irtk import cv2 mask = irtk.imread("mask.nii",dtype='uint8') irtk.imwrite("mask.png",mask) img = irtk.Image( cv2.imread("lena.png",0) ) irtk.imshow( img, mask, filename="initialisation.png", colors={1:(255,0,0),2:(0,255,0)}, opacity=1.0 ) mask2 = irtk.imread("mask2.nii",dtype='uint8') irtk.imwrite("mask2.png",mask2) irtk.imshow( img, mask2, filename="initialisation2.png", colors={1:(255,0,0),2:(0,255,0)}, opacity=1.0 )
#!/usr/bin/python import sys import numpy as np import irtk from pyhull.convex_hull import ConvexHull from irtk.vtk2irtk import voxellise seg = irtk.imread(sys.argv[1]) output = sys.argv[2] ZYX = np.transpose(np.nonzero(seg)) pts = seg.ImageToWorld(ZYX[:, ::-1]) hull = ConvexHull(pts) img = voxellise(hull.points, hull.vertices, header=seg.get_header()) irtk.imwrite(output, img)
seg = irtk.imread("/vol/medic02/users/kpk09/gitlab/fetus-detector/body-detector/notebooks/tmp/seg_template.nii.gz",force_neurological=False) res = irtk.zeros(average.get_header(),dtype='int32') heart_center = np.array(nd.center_of_mass( (seg == 5).view(np.ndarray) ), dtype='float32') heart = np.argwhere( (seg == 5).view(np.ndarray) ).astype('float32') r_heart = np.linalg.norm(heart_center-heart,axis=1).mean() ball = irtk.Image( morphology.ball(r_heart) ) ball.header['origin'] = np.array([0,0,0],dtype='float64') ball2 = ball.transform(target=liver) res[ball2>0] = 5 brain_center = np.array(nd.center_of_mass( (seg == 2).view(np.ndarray) ), dtype='float32') brain = np.argwhere( (seg == 2).view(np.ndarray) ).astype('float32') r_brain = np.linalg.norm(brain_center-brain,axis=1).mean() ball = irtk.Image( morphology.ball(r_brain) ) ball.header['origin'] = np.array(liver.ImageToWorld(brain_center[::-1]),dtype='float64') ball2 = ball.transform(target=liver) res[ball2>0] = 2 threshold = 0.5 res[left_lung>threshold] = 3 res[right_lung>threshold] = 4 res[liver>threshold] = 8 irtk.imwrite("model.nii.gz",res)
return labels == best_label def background_distance(img,metric='geodesic',includeEDT=True): background = get_background(img) if metric == "euclidean": distanceMap = edt( img, background ) elif metric == "geodesic": distanceMap = gdt( img, background, includeEDT ) else: raise ValueError("Unknown metric: "+ metric) return irtk.Image(distanceMap,img.get_header()) if __name__ == "__main__": img = irtk.imread( sys.argv[1], dtype="float64" ) #filtered = nd.minimum_filter(img,5) filtered = nd.gaussian_gradient_magnitude(img,0.5) img = irtk.Image(filtered,img.get_header()) irtk.imwrite("test2.nii.gz",img) exit(0) img = world_align(img,pixelSize=[2,2,2,1]) irtk.imwrite("distanceEDT.nii.gz",background_distance(img,metric="euclidean")) irtk.imwrite( "distanceGDT.nii.gz", background_distance(img,metric="geodesic"))
def predict_autocontext( self, img, mask, extra_layers, metadata, nb_labels, ga, nb_autocontext, debug=False, return_all=False ): proba = np.ones((nb_labels,img.shape[0],img.shape[1],img.shape[2]),dtype='float32') proba /= nb_labels header = img.get_header() header['dim'][3] = nb_labels proba = irtk.Image(proba,header) all_steps = [] for k in xrange(nb_autocontext): metadata = self.get_center_axis(proba,k) knowledge = self.get_knowledge(img,proba,extra_layers,mask=mask) if debug: irtk.imwrite("knowledge_"+str(k)+".nii.gz", knowledge) forest = integralForest( folder=self.folder(k), test=self.params['test'], parallel=self.params['parallel'], nb_knowledge_layers=knowledge.shape[0] ) proba = forest.predict_autocontext( img, knowledge, mask, self.params['ksampling'], metadata ) proba = irtk.Image(proba,header) if return_all: all_steps.append( proba.copy() ) if debug: irtk.imwrite("debug_"+str(k)+".nii.gz", proba) if k < 1: for i in xrange(proba.shape[0]): if i == 0: proba[i] = 0 else: proba[i] = self.groups[i-1].get_center(proba[i]) # # volume constraint # # set not ventricule to 0 # tmp_proba = proba[1] # for i in xrange(proba.shape[0]): # if i == 1: # continue # proba[i] = 0 # # rescale ventricule # target_volume = 182950.0*0.001**3 # #target_volume = 151807.0*0.001**3 # if k == 0: # target_volume *= 0.5 # # elif k == 1: # # target_volume *= 0.25 # # elif k == 2: # # target_volume *= 0.5 # box_volume = float(proba.shape[1])*proba.header['pixelSize'][2]*float(proba.shape[2])*proba.header['pixelSize'][1]*float(proba.shape[3])*proba.header['pixelSize'][0] # ratio = float(target_volume) / float(box_volume) # #print "ratio", ratio # q0 = mquantiles( tmp_proba.flatten(), prob=[1.0-ratio] ) # tmp_proba[proba[1]<q0] = q0 # tmp_proba -= tmp_proba.min() # tmp_proba /= tmp_proba.max() # lcc = irtk.largest_connected_component(tmp_proba,fill_holes=False) # tmp_proba[lcc==0] = 0 # proba[1] = tmp_proba if debug: print "done autocontext", k # irtk.imwrite("debug_rescaled_"+str(k)+".nii.gz", proba) if not return_all: return proba else: return all_steps
def preprocess_training_data( patient_id, img_folder, seg_folder, resample, offline=False, online=True): if offline or online: if ( offline and os.path.exists( "offline_preprocessing/"+patient_id+"_img.nii.gz" ) and os.path.exists( "offline_preprocessing/"+patient_id+"_seg.nii.gz" ) ): return img = irtk.imread( img_folder + "/" + patient_id + ".nii.gz", dtype='float32' ) seg = irtk.imread( seg_folder +"/"+patient_id+"_seg.nii.gz", dtype="uint8" ) wall = nd.binary_dilation( seg, morphology.ball(int(12.5*0.001/seg.header['pixelSize'][0])) ) wall = wall.astype('int') points = np.transpose(np.nonzero(wall))[::4] center,S,V = fit_ellipsoidPCA( points ) if V[0,0] < 0: V *= -1 points = np.transpose(np.nonzero(wall)) projections = np.dot(points-center,V[0]) # valves index = projections > (projections.max() - 40.0*0.001/seg.header['pixelSize'][0]) #print "VALVE size:",np.sum(index), projections.max(), 40.0*0.001/seg.header['pixelSize'][0] wall[points[index,0], points[index,1], points[index,2]] = 2 #print "VALVE1", wall.max() wall = irtk.Image(wall,seg.get_header()) img = img.resample( pixelSize=resample, interpolation='linear' ).rescale(0,1000) seg = seg.transform(target=img,interpolation="nearest").astype('uint8') wall = wall.transform(target=img,interpolation="nearest").astype('uint8') wall[seg>0] = 0 seg[wall==1] = 2 seg[wall==2] = 3 #print "VALVE2", seg.max() #irtk.imwrite("debug/"+patient_id+"_border.nii.gz",seg) seg[img==0] = 255 if offline: irtk.imwrite( "offline_preprocessing/"+patient_id+"_img.nii.gz", img ) irtk.imwrite( "offline_preprocessing/"+patient_id+"_seg.nii.gz", seg ) return if not online: img = irtk.imread( "offline_preprocessing/"+patient_id+"_img.nii.gz" ) seg = irtk.imread( "offline_preprocessing/"+patient_id+"_seg.nii.gz" ) mask = irtk.ones( img.get_header(), dtype='uint8' ) mask[img==0] = 0 return { 'patient_id': patient_id, 'img' : img, 'seg' : seg, 'mask' : mask }
#!/usr/bin/python import sys import numpy as np import irtk # img1 = irtk.imread("reconstruction/t2.nii", dtype='float32') # img2 = irtk.imread("reconstruction/t2seg.nii", dtype='float32') # irtk.imwrite( "reconstruction/segfixed.nii", img2.transform(target=img1) ) shape = map( int, sys.argv[1:4] ) z = int(sys.argv[4]) y = int(sys.argv[5]) x = int(sys.argv[6]) target = irtk.imread( sys.argv[7] ) img = irtk.imread( sys.argv[8], dtype='float32' ) new_data = np.zeros( shape, dtype='int32' ) new_data[z:z+img.shape[0], y:y+img.shape[1], x:x+img.shape[2]] = img new_img = irtk.Image( new_data, target.get_header() ) irtk.imwrite( sys.argv[9], new_img )
#!/usr/bin/python import irtk import sys import scipy.ndimage as nd input_file = sys.argv[1] output_file = sys.argv[2] img = irtk.imread(input_file, dtype='float32') img = irtk.Image(nd.median_filter(img.get_data(), 5), img.get_header()) irtk.imwrite(output_file, img)
def predict_autocontext(self, img, mask, extra_layers, metadata, nb_labels, ga, nb_autocontext, debug=False, return_all=False): proba = np.ones((nb_labels, img.shape[0], img.shape[1], img.shape[2]), dtype='float32') proba /= nb_labels header = img.get_header() header['dim'][3] = nb_labels proba = irtk.Image(proba, header) all_steps = [] for k in xrange(nb_autocontext): metadata = self.get_center_axis(proba, k) knowledge = self.get_knowledge(img, proba, extra_layers, mask=mask) if debug: irtk.imwrite("knowledge_" + str(k) + ".nii.gz", knowledge) forest = integralForest(folder=self.folder(k), test=self.params['test'], parallel=self.params['parallel'], nb_knowledge_layers=knowledge.shape[0]) proba = forest.predict_autocontext(img, knowledge, mask, self.params['ksampling'], metadata) proba = irtk.Image(proba, header) if return_all: all_steps.append(proba.copy()) if debug: irtk.imwrite("debug_" + str(k) + ".nii.gz", proba) if k < 1: for i in xrange(proba.shape[0]): if i == 0: proba[i] = 0 else: proba[i] = self.groups[i - 1].get_center(proba[i]) # # volume constraint # # set not ventricule to 0 # tmp_proba = proba[1] # for i in xrange(proba.shape[0]): # if i == 1: # continue # proba[i] = 0 # # rescale ventricule # target_volume = 182950.0*0.001**3 # #target_volume = 151807.0*0.001**3 # if k == 0: # target_volume *= 0.5 # # elif k == 1: # # target_volume *= 0.25 # # elif k == 2: # # target_volume *= 0.5 # box_volume = float(proba.shape[1])*proba.header['pixelSize'][2]*float(proba.shape[2])*proba.header['pixelSize'][1]*float(proba.shape[3])*proba.header['pixelSize'][0] # ratio = float(target_volume) / float(box_volume) # #print "ratio", ratio # q0 = mquantiles( tmp_proba.flatten(), prob=[1.0-ratio] ) # tmp_proba[proba[1]<q0] = q0 # tmp_proba -= tmp_proba.min() # tmp_proba /= tmp_proba.max() # lcc = irtk.largest_connected_component(tmp_proba,fill_holes=False) # tmp_proba[lcc==0] = 0 # proba[1] = tmp_proba if debug: print "done autocontext", k # irtk.imwrite("debug_rescaled_"+str(k)+".nii.gz", proba) if not return_all: return proba else: return all_steps
def preprocess_training_data(patient_id, img_folder, seg_folder, resample, offline=False, online=True): if offline or online: if (offline and os.path.exists("offline_preprocessing/" + patient_id + "_img.nii.gz") and os.path.exists("offline_preprocessing/" + patient_id + "_seg.nii.gz")): return img = irtk.imread(img_folder + "/" + patient_id + ".nii.gz", dtype='float32') seg = irtk.imread(seg_folder + "/" + patient_id + "_seg.nii.gz", dtype="uint8") wall = nd.binary_dilation( seg, morphology.ball(int(12.5 * 0.001 / seg.header['pixelSize'][0]))) wall = wall.astype('int') points = np.transpose(np.nonzero(wall))[::4] center, S, V = fit_ellipsoidPCA(points) if V[0, 0] < 0: V *= -1 points = np.transpose(np.nonzero(wall)) projections = np.dot(points - center, V[0]) # valves index = projections > (projections.max() - 40.0 * 0.001 / seg.header['pixelSize'][0]) #print "VALVE size:",np.sum(index), projections.max(), 40.0*0.001/seg.header['pixelSize'][0] wall[points[index, 0], points[index, 1], points[index, 2]] = 2 #print "VALVE1", wall.max() wall = irtk.Image(wall, seg.get_header()) img = img.resample(pixelSize=resample, interpolation='linear').rescale(0, 1000) seg = seg.transform(target=img, interpolation="nearest").astype('uint8') wall = wall.transform(target=img, interpolation="nearest").astype('uint8') wall[seg > 0] = 0 seg[wall == 1] = 2 seg[wall == 2] = 3 #print "VALVE2", seg.max() #irtk.imwrite("debug/"+patient_id+"_border.nii.gz",seg) seg[img == 0] = 255 if offline: irtk.imwrite("offline_preprocessing/" + patient_id + "_img.nii.gz", img) irtk.imwrite("offline_preprocessing/" + patient_id + "_seg.nii.gz", seg) return if not online: img = irtk.imread("offline_preprocessing/" + patient_id + "_img.nii.gz") seg = irtk.imread("offline_preprocessing/" + patient_id + "_seg.nii.gz") mask = irtk.ones(img.get_header(), dtype='uint8') mask[img == 0] = 0 return { 'patient_id': patient_id, 'img': img, 'seg': seg, 'extra_layers': np.array([], dtype='float32'), 'metadata': None, 'mask': mask }
#!/usr/bin/python import sys import numpy as np import irtk # img1 = irtk.imread("reconstruction/t2.nii", dtype='float32') # img2 = irtk.imread("reconstruction/t2seg.nii", dtype='float32') # irtk.imwrite( "reconstruction/segfixed.nii", img2.transform(target=img1) ) shape = map(int, sys.argv[1:4]) z = int(sys.argv[4]) y = int(sys.argv[5]) x = int(sys.argv[6]) target = irtk.imread(sys.argv[7]) img = irtk.imread(sys.argv[8], dtype='float32') new_data = np.zeros(shape, dtype='int32') new_data[z:z + img.shape[0], y:y + img.shape[1], x:x + img.shape[2]] = img new_img = irtk.Image(new_data, target.get_header()) irtk.imwrite(sys.argv[9], new_img)