def get_local_feature_one_vertebra(v, I, label, block_size=block_size): # step 1: filter the image. # step 2: extract 1st - 4th order of the filtered image. # here we use, 6 different scaled gaussian + linear transformation # (DCT, DST, fft) slice_num = I.shape[0] width = I.shape[2] height = I.shape[1] feature_dim = get_feature_dim() feature = np.empty((slice_num, height, width, feature_dim), dtype=np.float) # first guassian filtered prev_filtered_dim = 0 for s in sigmas: print "sigma value: " + str(s) filtered_dim = get_filtered_dim(s) for i in range(I.shape[0]): print "for slice " + str(i) img = I[i] f = get_local_feature_one_vertebra_one_slice_one_sigma(img, s) feature[i, :, :, prev_filtered_dim*moment: (prev_filtered_dim + filtered_dim)*moment]=f prev_filtered_dim = filtered_dim # step 3: now that we have feature of all the points in image scale, # we can extract the feature for all the sample points. # Based on the paper, IEEE T. on Medical Imaging, 2002, "ASM Segmentation with optimal feature" # we need to use 5*5 window centered at each landmark. # note: feature = np.empty((slice_num, height, width, feature_dim),dtype=np.float) print "start " + str( block_size)+ "x" + str(block_size) + " sampling..." block_area = block_size*block_size feature_block = np.empty((8, slice_num*block_area, feature_dim), dtype=np.float) label_block = np.empty((8, slice_num*block_area), dtype=np.int) size_1 = block_size/2 size_2 = block_size - size_1 for i in range(8): idx_x = i*2 idx_y = i*2 + 1 # slice_num * 2k+1 * feature_dim for n in range(slice_num): # here block is block centered at landmark min_x = v[n, idx_x] - size_1 max_x = v[n, idx_x] + size_2 min_y = v[n, idx_y] - size_1 max_y = v[n, idx_y] + size_2 ######################################################### #DEBUG if n == 0: from src.util import show_slice from src.util import read_jpg, crop_slice pn = 1 fn = '4' fn = '/Users/ruhansa/Dropbox/spine/data/p' + str(pn) + '/label/'+ fn + '.jpg' label_jpg = read_jpg(fn) label_arr = np.array(label_jpg.convert('L'), dtype=np.int16) x_min = 0 x_max = 512 y_min = 130 y_max = 350 clabel = crop_slice(label_arr, (x_min, y_min), (x_max, y_max)) * 255 clabel[min_x: max_x, min_y: max_y] = 255 show_slice(clabel) ######################################################### feature_block[i, n*block_area:n*block_area+block_area, :] = feature[n, min_x:max_x, min_y: max_y, : ].reshape(block_area, feature_dim) label_block[i, n*block_area: n*block_area+block_area] = label[n, min_x:max_x, min_y:max_y].reshape(block_area) return label_block, feature_block
from src.util import show_volume_dots dots = np.zeros((10, 2, 8), dtype=np.int16) for i in range(10): xs = shapes[i, np.arange(0, 15, 2)].astype(int) ys = shapes[i, np.arange(1, 16, 2)].astype(int) dots[i] = np.array((xs, ys)) show_volume_dots(background, dots) if __name__ == "__main__": from src.util import read_jpg, crop_slice pn = 1 fn = "4" fn = "/Users/ruhansa/Dropbox/spine/data/p" + str(pn) + "/label/" + fn + ".jpg" label_jpg = read_jpg(fn) label = np.array(label_jpg.convert("L"), dtype=np.int16) x_min = 0 x_max = 512 y_min = 130 y_max = 350 clabel = crop_slice(label, (x_min, y_min), (x_max, y_max)) # slice(model.mean, clabel) volume(shapes, clabel) # from src.asm_util import show_one_vertebra # show_one_vertebra(shapes, slice_number=shapes.shape[0],width=220, height=512)