def test_nearest(): import matplotlib.pyplot as plt N = 64 d = np.random.rand(N**3).reshape((N, ) * 3).astype(np.float32) rend = VolumeRenderer((400, 400), 'nearest') rend.set_data(d) rend.render() out = rend.output plt.imshow(out) plt.show() plt.pause(.1) plt.close() return 1
def test_simple(): import matplotlib.pyplot as plt N = 64 d = np.linspace(0, 1, N**3).reshape((N, ) * 3).astype(np.float32) rend = VolumeRenderer((400, 400)) rend.set_data(d) rend.render() out = rend.output plt.imshow(out) plt.show() plt.pause(0.1) plt.close() return 1
def render_iso(data, t = -.8): from gputools.utils.utils import remove_cache_dir, get_cache_dir remove_cache_dir() rend = VolumeRenderer((400,400)) # rend.set_occ_strength(0) # rend.set_occ_radius(21) # rend.set_occ_n_points(30) rend.set_modelView(mat4_translate(0, 0, t)) rend.render(data, maxVal = 130., method="iso_surface") return rend
def __init__(self, parent=None, N_PREFETCH=0, interpolation = "linear", **kwargs): logger.debug("init") super(GLWidget, self).__init__(parent, **kwargs) self.parent = parent self.texture_LUT = None self.setAcceptDrops(True) self.renderer = VolumeRenderer((spimagine.config.__DEFAULT_TEXTURE_WIDTH__, spimagine.config.__DEFAULT_TEXTURE_WIDTH__), interpolation=interpolation) self.renderer.set_projection(mat4_perspective(60, 1., .1, 100)) # self.renderer.set_projection(projMatOrtho(-2,2,-2,2,-10,10)) self.output = np.zeros([self.renderer.height, self.renderer.width], dtype=np.float32) self.output_alpha = np.zeros([self.renderer.height, self.renderer.width], dtype=np.float32) self.sliceOutput = np.zeros((100, 100), dtype=np.float32) self.setTransform(TransformModel()) self.renderTimer = QtCore.QTimer(self) self.renderTimer.setInterval(10) self.renderTimer.timeout.connect(self.onRenderTimer) self.renderTimer.start() self.renderedSteps = 0 self.N_PREFETCH = N_PREFETCH self.NSubrenderSteps = 1 self.dataModel = None self.meshes = [] # self.setMouseTracking(True) self._dataModelChanged.connect(self.dataModelChanged) self.refresh()
def render_iso(data, is_ao=True): from gputools.utils.utils import remove_cache_dir, get_cache_dir remove_cache_dir() rend = VolumeRenderer((400, 400)) if is_ao: rend.set_occ_strength(1) rend.set_occ_radius(101) rend.set_occ_n_points(1000) else: rend.set_occ_strength(0) rend.set_occ_radius(21) rend.set_occ_n_points(30) rend.set_modelView(mat4_translate(0, 0, -1.)) rend.render(data, maxVal=70., method="iso_surface") return rend
def test_opacity(): N = 128 x = np.linspace(-1, 1, N) Z, Y, X = np.meshgrid(x, x, x, indexing="ij") R = np.sqrt(X**2 + Y**2 + Z**2) d = 200 * np.exp(-10 * R**2) rend = VolumeRenderer((600, ) * 2) rend.set_modelView(mat4_translate(0, 0, -10.)) rend.set_alpha_pow(.6) rend.set_data(d.astype(np.float32)) rend.render(method="max_project", maxVal=200.) out = rend.output plt.imshow(out, cmap="magma") plt.axis("off") plt.show() return rend
def test_simple_rendering(): from gputools.utils.utils import remove_cache_dir, get_cache_dir remove_cache_dir() dtypes = [np.float32, np.uint16] # build some test data N = 128 x = np.linspace(-1, 1, N) Z, Y, X = np.meshgrid(x, x, x, indexing="ij") R1 = np.sqrt((X - .2)**2 + Y**2 + Z**2) R2 = np.sqrt((X + .2)**2 + Y**2 + Z**2) data = 255 * np.exp(-30 * R1**2) + np.exp(-30 * R2**2) rend = VolumeRenderer((400, 400)) outs = [] for dtype in dtypes: rend.render(data=data.astype(dtype), maxVal=255.) outs.append(rend.output) # drawing plt.figure(1) plt.clf() for i, out in enumerate(outs): plt.subplot(1, len(outs), i + 1) plt.imshow(out) plt.axis("off") plt.title("%s" % (dtype)) plt.pause(.1) plt.close() plt.show() plt.pause(.1) plt.close() return rend
def test_time_to_render(): import time from gputools import get_device get_device().print_info() N = 256 x = np.linspace(-1, 1, N) Z, Y, X = np.meshgrid(x, x, x, indexing="ij") R = np.sqrt(X**2 + Y**2 + Z**2) d = 10000 * np.exp(-10 * R**2) rend = VolumeRenderer((600, 600)) rend.set_modelView(mat4_translate(0, 0, -10.)) # rend.set_box_boundaries(.3*np.array([-1,1,-1,1,-1,1])) t1 = time.time() get_device().queue.finish() rend.set_data(d, autoConvert=True) get_device().queue.finish() t2 = time.time() get_device().queue.finish() rend.render(maxVal=10000.) out = rend.output get_device().queue.finish() print("time to set data %s^3:\t %.2f ms" % (N, 1000 * (t2 - t1))) print("time to render %s^3:\t %.2f ms" % (N, 1000 * (time.time() - t2))) return d, rend, out
# np.int32(1), # d.data, # d.data, # im) # # print(out.get()) if __name__ == '__main__': from spimagine.volumerender.volumerender import VolumeRenderer from gputools.utils.utils import remove_cache_dir, get_cache_dir remove_cache_dir() data = (123 * np.ones((10, 10, 10))).astype(np.float32) rend = VolumeRenderer((4, 4)) rend.proc = OCLProgram("/Users/mweigert/python/spimagine/spimagine/volumerender/kernels/all_render_kernels.cl", build_options= ["-I", "/Users/mweigert/python/spimagine/spimagine/volumerender/kernels", "-D", "maxSteps=100", "-D","QUALIFIER_CONSTANT_TO_GLOBAL" "-cl-finite-math-only", "-cl-fast-relaxed-math", "-cl-unsafe-math-optimizations", "-cl-mad-enable"]) rend.set_data(data) rend.proc.run_kernel("max_project_float", (rend.width, rend.height), None,
def main(): parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description="""renders max projectios of 3d data example usage: Tif data: \t \tspim_render -i mydata.tif -o myoutput.png -t 0 0 -4 -u 1 1 4 Bscope data: \tspim_render -f bscope -i mydataFolder -o myoutput.png -t 0 0 -4 -u 1 1 4 """) parser.add_argument("-f","--format",dest="format",metavar="format", help = """format currently supported: tif (default) bscope """, type=str,default = "tif", required = False) parser.add_argument("-i","--input",dest="input",metavar="infile", help = "name of the input file to render, currently only 3d Tiff is supported", type=str,default = None, required = True) parser.add_argument("-o","--output",dest="output",metavar="outfile", help = "name of the output file, png extension is recommended", type=str,default = "out.png") parser.add_argument("-p","--pos",dest="pos",metavar="timepoint position", help = "timepoint to render if format=='bscope' ", type=int,default = 0) parser.add_argument("-w","--width",dest="width",metavar="width", help = "pixelwidth of the rendered output ", type=int,default = 400) parser.add_argument("-s","--scale",dest="scale",metavar="scale", type=float,nargs=1 ,default = [1.]) parser.add_argument("-u","--units",dest="units",metavar="units", type=float,nargs= 3 ,default = [1.,1.,5.]) parser.add_argument("-t","--translate",dest="translate", type = float, nargs=3,default = [0,0,-4], metavar=("x","y","z")) parser.add_argument("-r","--rotation",dest="rotation", type = float, nargs=4,default = [0,1,0,0], metavar=("w","x","y","z")) parser.add_argument("-R","--range",dest="range", type = float, nargs=2,default = None, help = "if --16bit is set, the range of the data values to consider, defaults to [min,max]", metavar=("min","max")) parser.add_argument("-O","--Orthoview",help="use parallel projection (default: perspective)", dest="ortho",action="store_true") parser.add_argument("--16bit",help="render into 16 bit png", dest="is16Bit",action="store_true") if len(sys.argv)==1: parser.print_help() return args = parser.parse_args() for k,v in six.iteritems(vars(args)): print(k,v) rend = VolumeRenderer((args.width,args.width)) if args.format=="tif": data = read3dTiff(args.input) elif args.format=="bscope": data = fromSpimFolder(args.input,pos=args.pos,count=1)[0,...] else: raise ValueError("format %s not supported (should be tif/bscope)" %args.format) rend.set_data(data) rend.set_units(args.units) M = mat4_scale(*(args.scale*3)) M = np.dot(mat4_rotation(*args.rotation),M) M = np.dot(mat4_translate(*args.translate),M) rend.set_modelView(M) if args.ortho: rend.set_projection(mat4_ortho(-1,1,-1,1,-1,1)) else: rend.set_projection(mat4_perspective(60,1.,1,10)) out = rend.render() # image is saved by scipy.misc.toimage(out,low,high,cmin,cmax) # p' = p * high/cmax if not args.is16Bit: imsave(args.output,out) # if not args.range: # imsave(args.output,out) # else: # img = toimage(out, low = args.range[0], high = args.range[1]) # img.save(args.output) else: if not args.range: print("min/max: ", np.amin(out), np.amax(out)) img = toimage(out, low = np.amin(out), high = np.amax(out),mode = "I") else: img = toimage(out, low = args.range[0], high = args.range[1], mode = "I") img.save(args.output)