def main(unused_args): test_data = load_train_data_folder('../input/norm1/', data_type=0, number_files=4) test_data = np.reshape(test_data, [-1, 64, 64, 64]) # print("Input Data...") # print("Shape: ", np.shape(test_data)) # print("Max: ", np.max(test_data)) # print("Min: ", np.min(test_data)) # print("Mean: ", np.mean(test_data)) # Create the Estimator mnist_classifier = tf.estimator.Estimator( model_fn=cnn_model_fn, model_dir="/home/joseph/Desktop/models/gilestest") predict_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": test_data}, batch_size=1, num_epochs=1, shuffle=False) # Predict data predictions = mnist_classifier.predict(input_fn=predict_input_fn) predictions = np.array(list(predictions)) print(predictions[0]) result = predictions[0]['classes'] # print(result) # print(np.shape(result)) write_itk_imageArray(result, '../output/detected.nii.gz')
def main(unused_args): test_data = load_test_data_folder('../input/norm2/', ) test_data = np.reshape(test_data, [-1, 64, 64]) print("Input Data...") print("Shape: ", np.shape(test_data)) print("Max: ", np.max(test_data)) print("Min: ", np.min(test_data)) print("Mean: ", np.mean(test_data)) # Create the Estimator mnist_classifier = tf.estimator.Estimator( model_fn=cnn_model_fn, model_dir="/home/joseph/Projects/vascularnetworks/models/giles2d_deep") predict_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": test_data}, batch_size=1, num_epochs=1, shuffle=False) # Predict data predictions = mnist_classifier.predict(input_fn=predict_input_fn) predictions = np.array(list(predictions)) print(np.shape(predictions)) print(predictions[0]['classes']) print(np.shape(predictions[0]['classes'])) result = np.zeros([64, 64, 64, 64], dtype='uint8') for i in utility.my_range(0, 64, 1): for j in utility.my_range(0, 64, 1): result[i, j, :, :] = predictions[i * 64 + j]['classes'] # print(result) # print(np.shape(result)) for i in utility.my_range(0, 64, 1): write_itk_imageArray(result[i], '../input/myseg2/cropped' + str(i) + '.nii.gz')
def main(unused_args): # Load training and eval data # mnist = tf.contrib.learn.datasets.load_dataset("mnist") # train_data = mnist.train.images # Returns np.array (55000, 784) # train_labels = np.asarray(mnist.train.labels, dtype=np.int32) # (55000, 1) test_data = load_test_data('..\\input\\test.mhd') # print("Input Data...") # print("Shape: ", np.shape(test_data)) # print("Max: ", np.max(test_data)) # print("Min: ", np.min(test_data)) # print("Mean: ", np.mean(test_data)) # Create the Estimator mnist_classifier = tf.estimator.Estimator( model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model") predict_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": test_data}, batch_size=100, num_epochs=1, shuffle=False) # Predict data predictions = mnist_classifier.predict(input_fn=predict_input_fn) predictions = np.array(list(predictions)) result = np.zeros(np.shape(predictions)) for index in utility.my_range(0, np.shape(predictions)[0], 1): result[index] = predictions[index]['classes'] # print(result) write_itk_imageArray(np.reshape(result, [128, 128, 128]), '..\\output\\detected.nii.gz')
from itkutilities import get_itk_array, write_itk_imageArray import numpy as np import sys if __name__ == '__main__': if len(sys.argv) != 3: print("Usage: " + sys.argv[0] + " <inputImage> <outputImage>") sys.exit(1) inputimg = np.array(get_itk_array(sys.argv[1]), dtype="uint8") outputimg = sys.argv[2] inputimg = np.subtract(inputimg, inputimg.min()) inputimg = np.multiply(inputimg, 1.0 / inputimg.max(), casting='unsafe') write_itk_imageArray(np.asarray(inputimg), outputimg)
# Crop image and write it. # Checks surrounding. If no bifurc. Add it. surrounding = bifimg[(i - 2 * stepsize - 1):i, (j - 2 * stepsize):j, (k - 2 * stepsize):k] # If no bifurc if np.max(surrounding) == 0: cropped = inputimg[(i - blocksize - stepsize):(i + blocksize - stepsize + 1), (j - blocksize - stepsize):(j + blocksize + 1 - stepsize), (k - blocksize - stepsize):(k + blocksize + 1 - stepsize)] # cropped_seg = segimg[(i - blocksize - stepsize):(i + blocksize + 1 - stepsize), # (j - blocksize - stepsize):(j + blocksize + 1 - stepsize), # (k - blocksize - stepsize):(k + blocksize + 1 - stepsize)] write_itk_imageArray(cropped, workfolder + "cropped" + str(index) + ".nii.gz") # write_itk_imageArray(cropped_seg, workfolder + "cropped" + str(index) + "_seg.nii.gz") # Update indices index = index + 1 print("Total bifurcations: ", index) file.write(str(index) + "\n") file.close() print("Done.")
outputfile = sys.argv[2] inputimg = np.array(get_itk_array(inputfile), dtype="uint8") # perform skeletonization skeleton = skeletonize_3d(inputimg) if display == True: # display results fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 4), sharex=True, sharey=True, subplot_kw={'adjustable': 'box-forced'}) ax = axes.ravel() ax[0].imshow(inputimg[55], cmap=plt.cm.gray) ax[0].axis('off') ax[0].set_title('original', fontsize=20) ax[1].imshow(skeleton[55], cmap=plt.cm.gray) ax[1].axis('off') ax[1].set_title('skeleton', fontsize=20) fig.tight_layout() plt.show() write_itk_imageArray(skeleton, outputfile)
if len(sys.argv) != 9: print( "Usage: " + sys.argv[0] + " <inputImage> <outputImage> <startX> <startY> <startZ> <sizeX> <sizeY> <sizeZ>" ) sys.exit(1) inputimg = get_itk_array(sys.argv[1]) outfile = sys.argv[2] startX = int(sys.argv[3]) startY = int(sys.argv[4]) startZ = int(sys.argv[5]) sizeX = int(sys.argv[6]) sizeY = int(sys.argv[7]) sizeZ = int(sys.argv[8]) endX = startX + sizeX endY = startY + sizeY endZ = startZ + sizeZ print(np.shape(inputimg)) print(startX, startY, startZ) print(endX, endY, endZ) cropped = inputimg[startX:endX, startY:endY, startZ:endZ] write_itk_imageArray(cropped, outfile)
from itkutilities import get_itk_array, write_itk_imageArray import numpy as np import sys if __name__ == '__main__': inputimg = np.array(get_itk_array(sys.argv[1]), dtype='uint8') # Write to File write_itk_imageArray(inputimg, sys.argv[2])
from itkutilities import get_itk_array, write_itk_imageArray import numpy as np import sys if __name__ == '__main__': if len(sys.argv) != 3: print("Usage: " + sys.argv[0] + " <inputImage> <outputImage>") sys.exit(1) inputimg = get_itk_array(sys.argv[1]) outputimg = sys.argv[2] inputimg = np.multiply(inputimg, inputimg) inputimg = np.multiply(inputimg, 255) write_itk_imageArray(np.asarray(inputimg, dtype='uint8'), outputimg)
index = 0 for i in utility.my_range(0, xSize, stepsize): for j in utility.my_range(0, ySize, stepsize): for k in utility.my_range(0, zSize, stepsize): print("Step at: (", index, ")", startX, startY, startZ) endX = startX + stepsize endY = startY + stepsize endZ = startZ + stepsize currentfilename = splitfolder + "/cropped" + str(index) + ".nii.gz" currentfile = np.array(get_itk_array(currentfilename), dtype='uint8') joinedfile[startX:endX, startY:endY, startZ:endZ] = currentfile # Update indices startZ = startZ + stepsize index = index + 1 startY = startY + stepsize startZ = 0 startX = startX + stepsize startY = 0 # Write the file on to disk. print("Writing File to Disk...") write_itk_imageArray(joinedfile, outputfile) print("Done.")
counts = np.zeros(num_points) for i in range(0, num_cells): data = output.GetCell(i).GetPointIds() counts[data.GetId(0)] += 1 counts[data.GetId(1)] += 1 # Indices start with 0. for i in range(0, num_points): # If bifurcation if counts[i] > 2: # print "index: ", i, " with ", counts[i], " connections" pt = output.GetPoint(i) x = int(pt[0] / xscale) y = int(pt[1] / yscale) z = int(pt[2] / zscale) # print x, y, z for j in range(-2, 3): for k in range(-2, 3): for l in range(-2, 3): xinc = x + j yinc = y + k zinc = z + l if xinc < 0 or yinc < 0 or zinc < 0 or xinc >= xwidth or yinc >= ywidth or zinc >= zwidth: continue else: a[xinc, yinc, zinc] = 1 # Write the image. itkutilities.write_itk_imageArray(a, output_file_name)
from itkutilities import get_itk_array, write_itk_imageArray import numpy as np import sys if __name__ == '__main__': if len(sys.argv) != 4: print("Usage: " + sys.argv[0] + " <inputImage> <outputImage> <threshold>") sys.exit(1) inputimg = get_itk_array(sys.argv[1]) outputimg = sys.argv[2] threshold = float(sys.argv[3]) write_itk_imageArray(np.asarray(inputimg >= threshold, dtype='uint8'), outputimg)
segment = False if segment: for i in utility.my_range(0, xSize, stepsize): for j in utility.my_range(0, ySize, stepsize): for k in utility.my_range(0, zSize, stepsize): print("Step at: (", index, ")", startX, startY, startZ) endX = startX + stepsize endY = startY + stepsize endZ = startZ + stepsize # Crop image and write it. cropped = inputimg[startX:endX, startY:endY, startZ:endZ] write_itk_imageArray( cropped, workfolder + "/cropped" + str(index) + ".nii.gz") # Apply segmentation to it. os.system("THEANO_FLAGS='device=gpu,floatX=float32' python " + testfilename + " --model " + modelfilename + " " + workfolder + "/cropped" + str(index) + ".nii.gz") # Update indices startZ = startZ + stepsize index = index + 1 startY = startY + stepsize startZ = 0 startX = startX + stepsize startY = 0