def rview( img, seg=None, overlay=None, colors=None, binary="rview" ): """ Launches rview. """ if isinstance( img, Image ): if overlay is not None and seg is not None: print "You cannot specify both seg and overlay" return if overlay is not None and colors is None: colors = 'jet' # default colormap if colors is not None and overlay is None: overlay = img.rescale() # we want to see img in colors img = zeros(img.get_header(), dtype='int16') if isinstance( colors, str ): colors = utils.get_colormap( colors ) if seg is not None and colors is None: colors = utils.random_colormap(seg.max()) # now, seg == overlay if overlay is not None: seg = overlay if seg is not None and not isinstance(seg, Image): seg = Image( seg, img.get_header() ) handle,filename = tempfile.mkstemp(suffix=".nii") garbage.append(filename) imwrite( filename, img.astype('int16') ) # rview reads in "short" anyway args = [ binary, filename ] if seg is not None: handle,seg_filename = tempfile.mkstemp(suffix=".nii") garbage.append(seg_filename) handle,colors_filename = tempfile.mkstemp(suffix=".txt") garbage.append(colors_filename) imwrite( seg_filename, seg.astype('int16') ) args.extend( ["-seg", seg_filename]) utils.colormap( colors, colors_filename ) args.extend( ["-lut", colors_filename]) if overlay is not None: args.append("-labels") # launch rview as an independant process subprocess.Popen( args )
def rview(img, seg=None, overlay=None, colors=None, binary="rview"): """ Launches rview. """ if isinstance(img, Image): if overlay is not None and seg is not None: print "You cannot specify both seg and overlay" return if overlay is not None and colors is None: colors = 'jet' # default colormap if colors is not None and overlay is None: overlay = img.rescale() # we want to see img in colors img = zeros(img.get_header(), dtype='int16') if isinstance(colors, str): colors = utils.get_colormap(colors) if seg is not None and colors is None: colors = utils.random_colormap(seg.max()) # now, seg == overlay if overlay is not None: seg = overlay if seg is not None and not isinstance(seg, Image): seg = Image(seg, img.get_header()) handle, filename = tempfile.mkstemp(suffix=".nii") garbage.append(filename) imwrite(filename, img.astype('int16')) # rview reads in "short" anyway args = [binary, filename] if seg is not None: handle, seg_filename = tempfile.mkstemp(suffix=".nii") garbage.append(seg_filename) handle, colors_filename = tempfile.mkstemp(suffix=".txt") garbage.append(colors_filename) imwrite(seg_filename, seg.astype('int16')) args.extend(["-seg", seg_filename]) utils.colormap(colors, colors_filename) args.extend(["-lut", colors_filename]) if overlay is not None: args.append("-labels") # launch rview as an independant process subprocess.Popen(args)
def thumbnail(self, seg=None, overlay=None, colors=None, opacity=0.5, step=2, unroll=False, index=None): """ Create a thumbnail. """ img = self.copy('int') if overlay is not None and seg is not None: print "You cannot specify both seg and overlay" return if seg is not None: seg = seg.astype('int') if overlay is not None and colors is None: colors = 'jet' # default colormap if colors is not None and seg is None and overlay is None: overlay = img.copy() # we want to see img in colors img = zeros(img.get_header(), dtype='uint8') opacity = 1.0 if isinstance(colors, str): colors = utils.get_colormap(colors) if seg is not None and colors is None: colors = utils.random_colormap(seg.max()) if index is None: index = np.array(img.shape).astype('int32') / 2 # now, seg == overlay if overlay is not None: seg = overlay if len(img.shape) == 2: if seg is not None: data = img.get_data('uint8') data = data.reshape(data.shape[0], data.shape[1], 1) data = np.concatenate([data, data, data], axis=2) rgb_overlay = utils.remap(seg, colors) # op = (1-opacity) * (seg != 0).astype('float') + (seg == 0).astype('float') # op = op.reshape(op.shape[0], # op.shape[1], # 1) # op = np.concatenate( [ op, # op, # op ], axis=2 ) # img = op * data + opacity*rgb_overlay img = (1 - opacity) * data + opacity * rgb_overlay return img.astype('uint8') elif len(img.shape) == 3: if not unroll: shape = np.array(img.shape).max() if seg is None: output = np.ones((shape, shape * 3 + step * (3 - 1))) * 255 else: output = np.ones( (shape, shape * 3 + step * (3 - 1), 3)) * 255 offset1 = int((shape - img.shape[1]) / 2) offset2 = int((shape - img.shape[2]) / 2) if seg is None: tmp_img = img[index[0], :, :] else: tmp_img = Image(img[index[0], :, :]).thumbnail( seg[index[0], :, :], colors=colors, opacity=opacity) output[offset1:offset1 + img.shape[1], offset2:offset2 + img.shape[2]] = tmp_img offset1 = int((shape - img.shape[0]) / 2) offset2 = int(shape + step + (shape - img.shape[2]) / 2) if seg is None: tmp_img = img[:, index[1], :] else: tmp_img = Image(img[:, index[1], :]).thumbnail( seg[:, index[1], :], colors=colors, opacity=opacity) output[offset1:offset1 + img.shape[0], offset2:offset2 + img.shape[2]] = tmp_img offset1 = int((shape - img.shape[0]) / 2) offset2 = int(2 * shape + 2 * step + (shape - img.shape[1]) / 2) if seg is None: tmp_img = img[:, :, index[2]] else: tmp_img = Image(img[:, :, index[2]]).thumbnail(seg[:, :, index[2]], colors=colors, opacity=opacity) output[offset1:offset1 + img.shape[0], offset2:offset2 + img.shape[1]] = tmp_img return output.astype('uint8') else: # unroll is True if seg is None: output = np.ones( (self.shape[1], self.shape[2] * self.shape[0] + 2 * (self.shape[0] - 1))) * 255 else: output = np.ones( (self.shape[1], self.shape[2] * self.shape[0] + 2 * (self.shape[0] - 1), 3)) * 255 for k in xrange(self.shape[0]): if seg is None: tmp_img = img[k, :, :] else: tmp_img = Image(img[k, :, :]).thumbnail( seg[k, :, :], colors=colors, opacity=opacity) output[:, k * self.shape[2] + 2 * k:(k + 1) * self.shape[2] + 2 * k] = tmp_img return output.astype('uint8') else: raise "Wrong number of dimensions for thumbnail: " + str( len(self.shape))
def thumbnail( self, seg=None, overlay=None, colors=None, opacity=0.5, step=2, unroll=False ): """ Create a thumbnail. """ img = self.copy('int') if overlay is not None and seg is not None: print "You cannot specify both seg and overlay" return if seg is not None: seg = seg.astype('int') if overlay is not None and colors is None: colors = 'jet' # default colormap if colors is not None and seg is None and overlay is None: overlay = img.copy() # we want to see img in colors img = zeros(img.get_header(), dtype='uint8') opacity = 1.0 if isinstance( colors, str ): colors = utils.get_colormap( colors ) if seg is not None and colors is None: colors = utils.random_colormap(seg.max()) # now, seg == overlay if overlay is not None: seg = overlay if len(img.shape) == 2: if seg is not None: data = img.get_data('uint8') data = data.reshape(data.shape[0], data.shape[1], 1) data = np.concatenate( [ data, data, data ], axis=2 ) rgb_overlay = utils.remap( seg, colors ) op = (1-opacity) * (seg != 0).astype('float') + (seg == 0).astype('float') op = op.reshape(op.shape[0], op.shape[1], 1) op = np.concatenate( [ op, op, op ], axis=2 ) img = op * data + opacity*rgb_overlay return img.astype('uint8') elif len(img.shape) == 3: if not unroll: shape = np.array(img.shape).max() if seg is None: output = np.ones((shape,shape*3+step*(3-1)))*255 else: output = np.ones((shape,shape*3+step*(3-1),3))*255 offset1 = (shape - img.shape[1])/2 offset2 = (shape - img.shape[2])/2 if seg is None: tmp_img = img[img.shape[0]/2,:,:] else: tmp_img = Image(img[img.shape[0]/2,:,:]).thumbnail( seg[img.shape[0]/2,:,:], colors=colors, opacity=opacity ) output[offset1:offset1+img.shape[1], offset2:offset2+img.shape[2]] = tmp_img offset1 = (shape - img.shape[0])/2 offset2 = shape + step + (shape - img.shape[2])/2 if seg is None: tmp_img = img[:,img.shape[1]/2,:] else: tmp_img = Image(img[:,img.shape[1]/2,:]).thumbnail( seg[:,img.shape[1]/2,:], colors=colors, opacity=opacity) output[offset1:offset1+img.shape[0], offset2:offset2+img.shape[2]] = tmp_img offset1 = (shape - img.shape[0])/2 offset2 = 2*shape + 2*step + (shape - img.shape[1])/2 if seg is None: tmp_img = img[:,:,img.shape[2]/2] else: tmp_img = Image(img[:,:,img.shape[2]/2]).thumbnail( seg[:,:,img.shape[2]/2], colors=colors, opacity=opacity ) output[offset1:offset1+img.shape[0], offset2:offset2+img.shape[1]] = tmp_img return output.astype('uint8') else: # unroll is True if seg is None: output = np.ones( ( self.shape[1], self.shape[2]*self.shape[0]+2*(self.shape[0]-1) ) ) * 255 else: output = np.ones( ( self.shape[1], self.shape[2]*self.shape[0]+2*(self.shape[0]-1), 3 ) ) * 255 for k in xrange(self.shape[0]): if seg is None: tmp_img = img[k,:,:] else: tmp_img = Image(img[k,:,:]).thumbnail( seg[k,:,:], colors=colors, opacity=opacity ) output[:, k*self.shape[2]+2*k:(k+1)*self.shape[2]+2*k] = tmp_img return output.astype('uint8') else: raise "Wrong number of dimensions for thumbnail: " + str(len(self.shape))