def create_analyze_from_imgdata(data, out, pix_x, pix_y, pix_z, tx, ty, tz, data_type="fl"): if data_type == "1b": dtype = np.int8 elif data_type == "2b": dtype = np.int16 elif data_type == "db": dtype = np.float64 else: dtype = np.float32 hdr1 = nib.AnalyzeHeader() hdr1.set_data_dtype(dtype) hdr1.set_data_shape((pix_x, pix_y, pix_z)) hdr1.set_zooms((tx, ty, tz)) f = open(data, 'rb') img_data = hdr1.raw_data_from_fileobj(f) analyze_img = nib.AnalyzeImage(img_data, hdr1.get_base_affine(), hdr1) nib.save(analyze_img, out)
def operate_single_image(input_image, operation, factor, output_image, logfile): """ Given an input image, multiply or divide it by a numerical factor saving the result as output_image :param input_image: image base operation on :param operation: 1 = multiply, 2 = divide :param factor: operation factor :param output_image: output image file :return: """ img = nib.load(input_image) data = img.get_data()[:, :, :] data = np.nan_to_num(data) if operation == 'mult': data = data * float(factor) elif operation == 'div': data = data / float(factor) else: message = "Error! Invalid operation: " + str(operation) print(message) log_message(logfile, message, 'error') hdr1 = nib.AnalyzeHeader() hdr1.set_data_dtype(img.get_data_dtype()) hdr1.set_data_shape(img.shape) hdr1.set_zooms(abs(np.diag(img.affine))[0:3]) analyze_img = nib.AnalyzeImage(data, hdr1.get_base_affine(), hdr1) nib.save(analyze_img, output_image)
def write_analyze(im,filepath): id1=np.swapaxes(im.img_data,0,2) ps=im.params.pixel_size hdr=nibabel.AnalyzeHeader() hdr.set_data_shape(id1.shape) hdr.set_data_dtype(id1.dtype) hdr.set_zooms([ps,ps,ps,im.params.frame_duration[0]]) analyze_img=nibabel.AnalyzeImage(id1,None,hdr) print('writing Analyze 7.5 image: '+filepath) analyze_img.to_filename(filepath)
def make_random_image(filename=None, dims=(10, 10, 10), xform=None, imgtype=1, pixdims=None, dtype=np.float32): """Convenience function which makes an image containing random data. Saves and returns the nibabel object. imgtype == 0: ANALYZE imgtype == 1: NIFTI1 imgtype == 2: NIFTI2 """ if imgtype == 0: hdr = nib.AnalyzeHeader() elif imgtype == 1: hdr = nib.Nifti1Header() elif imgtype == 2: hdr = nib.Nifti2Header() if pixdims is None: pixdims = [1] * len(dims) pixdims = pixdims[:len(dims)] zooms = [abs(p) for p in pixdims] hdr.set_data_dtype(dtype) hdr.set_data_shape(dims) hdr.set_zooms(zooms) if xform is None: xform = np.eye(4) for i, p in enumerate(pixdims[:3]): xform[i, i] = p data = np.array(np.random.random(dims) * 100, dtype=dtype) if imgtype == 0: img = nib.AnalyzeImage(data, xform, hdr) elif imgtype == 1: img = nib.Nifti1Image( data, xform, hdr) elif imgtype == 2: img = nib.Nifti2Image( data, xform, hdr) if filename is not None: if op.splitext(filename)[1] == '': if imgtype == 0: filename = '{}.img'.format(filename) else: filename = '{}.nii'.format(filename) nib.save(img, filename) return img
def operate_images_analyze(image1, image2, out_image, operation='mult'): """ Given the input images, calculate the multiplication image or the ratio between them :param image1: string, path to the first image :param image2: string, path to the second image :param operation: string, multi (default) for multiplication divid for division :param out_image: string (optional), path to the output image :return: """ img1, data1 = nib_load(image1) img2, data2 = nib_load(image2) # TODO CHECK IF NEGATIVE VALUES NEED TO BE REMOVED # Remove NaN and negative values data1 = np.nan_to_num(data1) data2 = np.nan_to_num(data2) if operation == 'mult': res_data = data1 * data2 elif operation == 'div': res_data = data1 / data2 elif operation == 'sum': res_data = data1 + data2 elif operation == 'diff': res_data = data1 - data2 else: message = 'Error! Unknown operation: ' + str(operation) raise TypeError(message) hdr1 = nib.AnalyzeHeader() hdr1.set_data_dtype(img1.get_data_dtype()) hdr1.set_data_shape(img1.shape) hdr1.set_zooms(abs(np.diag(img1.affine))[0:3]) analyze_img = nib.AnalyzeImage(res_data, hdr1.get_base_affine(), hdr1) nib.save(analyze_img, out_image)