def run(self): gui = ti.GUI("Multigrid Preconditioned Conjugate Gradients", res=(self.N_gui, self.N_gui)) self.init() self.solve(max_iters=400) self.paint() ti.imshow(self.pixels) ti.kernel_profiler_print()
def imdisplay(img): """ Try to display image in interactive shell. """ if ti.lang.shell.oinspect.name == ti.lang.shell.ShellType.JUPYTER: import PIL.Image from io import BytesIO import IPython.display import numpy as np img = cook_image_to_bytes(img) with BytesIO() as f: PIL.Image.fromarray(img).save(f, 'png') IPython.display.display(IPython.display.Image(data=f.getvalue())) else: ti.imshow(img)
def imdisplay(img): """ Try to display image in interactive shell. """ try: get_ipython() except: ti.imshow(img) else: import PIL.Image from io import BytesIO import IPython.display img = cook_image_to_bytes(img) with BytesIO() as f: PIL.Image.fromarray(img).save(f, 'png') IPython.display.display(IPython.display.Image(data=f.getvalue()))
def imdisplay(img): """ Try to display image in interactive shell. Args: img (Union[ti.field, np.ndarray]): A field of of array with shape `(width, height)` or `(height, width, 3)` or `(height, width, 4)`. """ try: get_ipython() except: ti.imshow(img) else: import IPython.display # pylint: disable=C0415 import PIL.Image # pylint: disable=C0415 img = cook_image_to_bytes(img) with BytesIO() as f: PIL.Image.fromarray(img).save(f, 'png') IPython.display.display(IPython.display.Image(data=f.getvalue()))
import taichi as ti ti.init() ## Load the image: input_file_name = input('Enter the input image file name: ') input_image = ti.imread(input_file_name) ## Process the image: image = ti.field(ti.u8, input_image.shape) image.from_numpy(input_image) @ti.kernel def process(): for i, j, k in image: image[i, j, k] = 255 - image[i, j, k] # revert color process() ## Save the image: output_image = image.to_numpy() ti.imshow(output_image) output_file_name = input('Enter the image file name to save: ') ti.imwrite(output_image, output_file_name)
#-------------------------------------------------------- # actual computation # compute the SDF for visualization start_time = perf_counter() computeVisualSdf() stop_time = perf_counter() print("computeVisualSdf: %f [ms]" % ((stop_time - start_time)*1000)) # compute the actual SDF start_time = perf_counter() computeSdf() stop_time = perf_counter() print("computeSdf: %f [ms]" % ((stop_time - start_time)*1000)) # compute the laplacian field start_time = perf_counter() computeVisualLaplacian() stop_time = perf_counter() print("computeVisualLaplacian: %f [ms]" % ((stop_time - start_time)*1000)) #-------------------------------------------------------- # visualization ti.imshow(visual_sdf, 'SDF') ti.imshow(visual_laplacian, 'Laplacian field') # gui = ti.GUI('Closest point', RES) # while not gui.get_event(ti.GUI.ESCAPE, ti.GUI.EXIT): # pos = gui.get_cursor_pos() # gui.set_image(visual_sdf) # gui.show()
import taichi as ti import os pixel = ti.field(ti.u8, shape=(512, 512, 3)) @ti.kernel def paint(): for I in ti.grouped(pixel): pixel[I] = ti.random() * 255 paint() pixel = pixel.to_numpy() ti.imshow(pixel, 'Random Generated') for ext in ['bmp', 'png', 'jpg']: fn = 'taichi-example-random-img.' + ext ti.imwrite(pixel, fn) pixel_r = ti.imread(fn) if ext != 'jpg': assert (pixel_r == pixel).all() else: ti.imshow(pixel_r, 'JPEG Read Result') os.remove(fn)
def run(self, verbose=False): self.init() self.solve(max_iters=400, verbose=verbose) self.paint() ti.imshow(self.pixels) ti.kernel_profiler_print()
# place the input points into the bitmasked grid start_time = perf_counter() placePts(ref_pts_numpy) stop_time = perf_counter() print("placePts: %f [ms]" % ((stop_time - start_time)*1000)) # compute SDF start_time = perf_counter() computeSdf(ref_pts_numpy) stop_time = perf_counter() print("computeSdf: %f [ms]" % ((stop_time - start_time)*1000)) # compute normals start_time = perf_counter() computeNormalAndCurvature() stop_time = perf_counter() print("computeNormalAndCurvature: %f [ms]" % ((stop_time - start_time)*1000)) # compute normals start_time = perf_counter() computeVisualLaplacian() stop_time = perf_counter() print("computeVisualLaplacian: %f [ms]" % ((stop_time - start_time)*1000)) #-------------------------------------------------------- # visualization #ti.imshow(sdf, 'SDF') #ti.imshow(visual_norm, 'Normals') #ti.imshow(visual_curv, 'Curvature') ti.imshow(visual_laplacian, 'Laplacian')
stop_time = perf_counter() print("computeSdf: %f [ms]" % ((stop_time - start_time) * 1000)) compueRefDstError() # Show the intial state of the dst points writeDstPtsOnVisualSdfGreen() nb_passes = 0 while nb_passes < 10 and error < 1: # compute ICP start_time = perf_counter() computeIcp() stop_time = perf_counter() print("computeIcp: %f [ms]" % ((stop_time - start_time) * 1000)) compueRefDstError() nb_passes += 1 # Show the final state of the dst points writeDstPtsOnVisualSdfBlue() #-------------------------------------------------------- # visualization ti.imshow(visual_sdf, 'SDF ref') # gui = ti.GUI('Closest point', RES) # while not gui.get_event(ti.GUI.ESCAPE, ti.GUI.EXIT): # pos = gui.get_cursor_pos() # gui.set_image(visual_sdf) # gui.show()
import taichi as ti ti.init(arch=ti.gpu) img = ti.var(ti.f32, (512, 512, 3)) # 分辨率 512x512,RGB 三通道 @ti.kernel def paint(): for i, j, in ti.ndrange(512, 512): # 并行执行 # 红色从左到右,蓝色色从下到上,亮度从0.0增加到1.0 img[i, j, 0] = i / 512 #左上蓝色 img[i, j, 2] = j / 512 #右下红色 img[i, j, 1] = 0 paint() # 填充张量内容 ti.imshow(img) # 将张量作为图像显示
@ti.kernel def render(): for i, j in x: x[i, j] = [i / x.shape[0], j / x.shape[1], 0] @ti.kernel def dump_ppm(tensor: ti.template()): if ti.static(isinstance(tensor, ti.Matrix)): print('P3') else: print('P2') print(tensor.shape[0], tensor.shape[1], 255) for _ in range(1): for i in range(x.shape[0]): for j in range(x.shape[1]): c = min(255, max(0, int(tensor[j, x.shape[1] - 1 - i] * 255 + 0.5))) if ti.static(isinstance(tensor, ti.Matrix)): r, g, b = c print(r, g, b) else: print(c) render() dump_ppm(x) ti.imshow(x)