def draw_circles(image, cands, origin, spacing): # make empty matrix, which will be filled with the mask image_mask = np.zeros(image.shape) # run over all the nodules in the lungs for ca in cands.values: # get middle x-,y-, and z-worldcoordinate of the nodule radius = np.ceil(ca[4]) / 2 coord_x = ca[1] coord_y = ca[2] coord_z = ca[3] image_coord = np.array((coord_x, coord_y, coord_z)) # determine voxel coordinate given the worldcoordinate image_coord = world_2_voxel(image_coord, origin, spacing) # determine the range of the nodule noduleRange = seq(-radius, radius, RESIZE_SPACING[0]) # create the mask for x in noduleRange: for y in noduleRange: for z in noduleRange: coords = world_2_voxel( np.array((coord_x + x, coord_y + y, coord_z + z)), origin, spacing) if (np.linalg.norm(image_coord - coords) * RESIZE_SPACING[0]) < radius: image_mask[int(np.round(coords[0])), int(np.round(coords[1])), int(np.round(coords[2]))] = int(1) return image_mask
def draw_circles(image,cands,origin,spacing): #make empty matrix, which will be filled with the mask image_mask = np.zeros(image.shape) #run over all the nodules in the lungs for ca in cands.values: #get middel x-,y-, and z-worldcoordinate of the nodule radius = np.ceil(ca[4])/2 coord_x = ca[1] coord_y = ca[2] coord_z = ca[3] image_coord = np.array((coord_x,coord_y,coord_z)) #determine voxel coordinate given the worldcoordinate image_coord = world_2_voxel(image_coord,origin,spacing) #determine the range of the nodule noduleRange = seq(-radius, radius, RESIZE_SPACING[0]) #create the mask for x in noduleRange: for y in noduleRange: for z in noduleRange: coords = world_2_voxel(np.array((coord_x+x,coord_y+y,coord_z+z)),origin,spacing) if (np.linalg.norm(image_coord-coords) * RESIZE_SPACING[0]) < radius: image_mask[np.round(coords[0]),np.round(coords[1]),np.round(coords[2])] = int(1) return image_mask
def process_image(image_path, candidates, save_dir): #load image image, origin, spacing = load_itk(image_path) #calculate resize factor resize_factor = spacing / OUTPUT_SPACING new_real_shape = image.shape * resize_factor new_shape = np.round(new_real_shape) real_resize = new_shape / image.shape new_spacing = spacing / real_resize #resize image image = scipy.ndimage.interpolation.zoom(image, real_resize) #Pad image with offset (OUTPUT_DIM/2) to prevent problems with candidates on edge of image offset = OUTPUT_DIM/2 image = np.pad(image,offset,'constant',constant_values=0) #Make a indixlist of the candidates of the image image_name = os.path.split(image_path)[1].replace('.mhd','') indices = candidates[candidates['image_name'] == image_name].index #loop through the candidates within this image for i in indices: #get row data and nodule voxel coords row = candidates.iloc[i] world_coords = np.array([row.x, row.y, row.z]) # add offset to voxel coords to cope with padding coords = np.floor(world_2_voxel(world_coords,origin,new_spacing)) + offset label = row.label # Create xy, xz, yz xy_slice = np.transpose(image[coords[0]-offset:coords[0]+offset,coords[1]-offset:coords[1]+offset,coords[2]]) xz_slice = np.rot90(image[coords[0]-offset:coords[0]+offset,coords[1],coords[2]-offset:coords[2]+offset]) yz_slice = np.rot90(image[coords[0],coords[1]-offset:coords[1]+offset,coords[2]-offset:coords[2]+offset]) # UNCOMMENT THESE LINES IF YOU WANT TO MANUALLY COMPARE IMAGES WITH MEVISLAB # test_coords means coords you need to look up in mevislab #test_coords = world_2_voxel(world_coords,origin,spacing) #print 'x:',test_coords[0],'y:',test_coords[1],'z:',test_coords[2] #plt.imshow(xy_slice) #plt.gray() #plt.figure(2) #plt.imshow(xz_slice) #plt.gray() #plt.figure(3) #plt.imshow(yz_slice) #plt.gray() #plt.show() # Create output output = np.zeros([3,OUTPUT_DIM,OUTPUT_DIM]) output[0,:,:] = xy_slice output[1,:,:] = xz_slice output[2,:,:] = yz_slice # Determine save_path based on label and indices (need +2 due to starting with zero + header in csv) if label: save_path = save_dir.format('True') + '/{}.pkl.gz'.format(i+2) else: save_path = save_dir.format('False') + '/{}.pkl.gz'.format(i+2) # save with gzip/pickle with gzip.open(save_path,'wb') as f: pickle.dump(output,f,protocol=-1)
def process_image(image_path, candidates, save_dir): # load image image, origin, spacing = load_itk(image_path) # calculate resize factor resize_factor = spacing / OUTPUT_SPACING new_real_shape = image.shape * resize_factor new_shape = np.round(new_real_shape) real_resize = new_shape / image.shape new_spacing = spacing / real_resize # resize image image = scipy.ndimage.interpolation.zoom(image, real_resize) # Pad image with offset (OUTPUT_DIM/2) to prevent problems with candidates on edge of image offset = OUTPUT_DIM / 2 image = np.pad(image, offset, 'constant', constant_values=0) # Make a indixlist of the candidates of the image image_name = os.path.split(image_path)[1].replace('.mhd', '') indices = candidates[candidates['image_name'] == image_name].index # loop through the candidates within this image for i in indices: # get row data and nodule voxel coords row = candidates.iloc[i] world_coords = np.array([row.x, row.y, row.z]) # add offset to voxel coords to cope with padding coords = np.floor(world_2_voxel(world_coords, origin, new_spacing)) + offset label = row.label # Create xy, xz, yz xy_slice = np.transpose(image[coords[0] - offset:coords[0] + offset, coords[1] - offset:coords[1] + offset, coords[2]]) xz_slice = np.rot90(image[coords[0] - offset:coords[0] + offset, coords[1], coords[2] - offset:coords[2] + offset]) yz_slice = np.rot90(image[coords[0], coords[1] - offset:coords[1] + offset, coords[2] - offset:coords[2] + offset]) # UNCOMMENT THESE LINES IF YOU WANT TO MANUALLY COMPARE IMAGES WITH MEVISLAB # test_coords means coords you need to look up in mevislab # test_coords = world_2_voxel(world_coords,origin,spacing) # print 'x:',test_coords[0],'y:',test_coords[1],'z:',test_coords[2] # plt.imshow(xy_slice) # plt.gray() # plt.figure(2) # plt.imshow(xz_slice) # plt.gray() # plt.figure(3) # plt.imshow(yz_slice) # plt.gray() # plt.show() # Create output output = np.zeros([3, OUTPUT_DIM, OUTPUT_DIM]) output[0, :, :] = xy_slice output[1, :, :] = xz_slice output[2, :, :] = yz_slice # Determine save_path based on label and indices (need +2 due to starting with zero + header in csv) if label: save_path = save_dir.format('True') + '/{}.pkl.gz'.format(i + 2) else: save_path = save_dir.format('False') + '/{}.pkl.gz'.format(i + 2) # save with gzip/pickle with gzip.open(save_path, 'wb') as f: pickle.dump(output, f, protocol=-1)