{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import k3d\n", "import math\n", "import numpy as np\n", "\n", "# Y-Z flip\n", "model_matrix = [\n", " 1.0, 0.0, 0.0, 0.0,\n", " 0.0, -1.0, 0.0, 0.0,\n", " 0.0, 0.0, 1.0, 0.0,\n", " 0.0, 0.0, 0.0, 1.0\n", "]\n", "\n", "texture = k3d.texture(open('assets/texture.png', 'br').read(), 'png', \n", " rotation=[math.radians(90),1,0,0], \n", " model_matrix = model_matrix,\n", " name='Photo')\n", "\n", "plot = k3d.plot()\n", "plot += texture\n", "plot.display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "texture.binary = open('assets/mandelbrot.jpg', 'br').read()\n", "texture.name = 'Fractal'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from k3d.helpers import download\n", "filename = download('https://github.com/FNNDSC/data/raw/master/nifti/adi_brain/adi_brain.nii.gz')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nibabel as nib\n", "nii_source = nib.load(filename)\n", "img = nii_source.get_fdata()\n", "dx, dy, dz = nii_source.header.get_zooms()\n", "img = np.swapaxes(img,0,2)\n", "nz, ny, nx = img.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import widgets,interact\n", "\n", "plot = k3d.plot()\n", "\n", "bounds = [0, nx * dx, 0, ny * dy, 0, nz * dz]\n", "\n", "texture = k3d.texture(attribute=img[0,:,:], \n", " color_map=k3d.basic_color_maps.Jet, \n", " color_range=[0.0, 1024.0],\n", " bounds=bounds,\n", " name='Slice')\n", "plot += texture\n", "\n", "@interact(x=widgets.IntRangeSlider(value=[0, 1024], min=0, max=np.max(img), step=1, description='Color range:'))\n", "def g(x):\n", " texture.color_range = x\n", " \n", "@interact(z=widgets.IntSlider(value=0,min=0,max=img.shape[0]-1,step=1, description='Slice:'))\n", "def g(z):\n", " texture.attribute =img[z, :, :]\n", " texture.transform.bounds = [bounds[0], bounds[1], bounds[2], bounds[3], -1 + z * dz, 1 + z * dz]\n", "\n", "plot.display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot.grid_auto_fit = False\n", "plot.camera_auto_fit = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot += k3d.marching_cubes(img.astype(np.float32), level=900, bounds=bounds, color=0xff0000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bounds = [0, nx * dx, 0, ny * dy, 0, nz * dz]\n", "\n", "texture = k3d.texture(attribute=img[0,:,:], \n", " color_map=k3d.basic_color_maps.Jet, \n", " color_range=[0.0, 1024.0],\n", " bounds=bounds,\n", " name='Slice')\n", "plot += texture" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }