{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unwrap to disk" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from skimage import io\n", "import napari\n", "import vedo\n", "import numpy as np\n", "from napari_stress import measurements, vectors\n", "\n", "import napari_segment_blobs_and_things_with_membranes as nsbatwm\n", "import napari_process_points_and_surfaces as nppas\n", "import pyclesperanto_prototype as cle\n", "from napari_shape_odyssey import unwrapping" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Invalid schema for package 'napari-stl-exporter', please run 'npe2 validate napari-stl-exporter' to check for manifest errors.\n", "WARNING: QWindowsWindow::setGeometry: Unable to set geometry 6880x2754+1280+560 (frame: 6906x2825+1267+502) on QWidgetWindow/\"_QtMainWindowClassWindow\" on \"\\\\.\\DISPLAY1\". Resulting geometry: 3844x2119+1280+560 (frame: 3870x2190+1267+502) margins: 13, 58, 13, 13 minimum size: 385x499 MINMAXINFO maxSize=0,0 maxpos=0,0 mintrack=796,1069 maxtrack=0,0)\n" ] } ], "source": [ "viewer = napari.Viewer()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "root = r'C:\\Users\\johamuel\\Desktop\\test_surface_nuclei_unwrapping'\n", "filename = r'Strausberg_Tribolium_LA-GFP_tailpole_run-C0opticsprefused-301-310.tif'\n", "\n", "image = io.imread(os.path.join(root, filename))[:, :-2]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "viewer.add_image(image[0], name='data', colormap='gray')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10, 109, 1024, 1024)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image.shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "image_binary = cle.threshold_otsu(image[0])\n", "image_eroded = cle.opening_sphere(image_binary, radius_x=5, radius_y=5, radius_z=2)\n", "image_closed = cle.closing_sphere(image_eroded, radius_x=5, radius_y=5, radius_z=2)\n", "image_small_labels_removed = cle.exclude_small_labels(image_closed)\n", "image_labelled = cle.connected_components_labeling_box(image_small_labels_removed)\n", "\n", "# cheatcode:\n", "image_labelled[-1] = 0\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "viewer.add_labels(image_labelled)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "surface = nppas.label_to_surface(image_labelled, 1)\n", "mesh = vedo.Mesh(surface).clean().smooth(50, pass_band=0.05, feature_angle=150, edge_angle=90, boundary=True).decimate(n=20000).clean()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", "\n", "
\n", "vedo.mesh.Mesh\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
bounds
(x/y/z)
-0.7683 ... 108.5
197.3 ... 830.1
214.7 ... 853.3
center of mass (49.5, 508, 534)
average size 253.595
nr. points / faces 20181 / 39892
\n", "
" ], "text/plain": [ "(Mesh)0000022D8D7CE760" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "smoothed = vedo.Mesh((mesh.points(), mesh.faces()))\n", "smoothed.smooth(feature_angle=10, edge_angle=10, boundary=True).clean()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intensities = measurements.measure_intensity_on_surface((smoothed.points(), np.asarray(smoothed.faces())), image[0],\n", " measurement_range=-64, sampling_distance=4, center=True)\n", "mean_intensity = np.nanmean(intensities.values, axis=1)\n", "\n", "viewer.add_surface((smoothed.points(), np.asarray(smoothed.faces()), mean_intensity), colormap='inferno')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = unwrapping.map_surface_to_disk((smoothed.points(), np.asarray(smoothed.faces()), mean_intensity),\n", " only_uvs=True)\n", "\n", "new_values = np.zeros(len(result[0]))\n", "new_values[:len(mean_intensity)] = mean_intensity\n", "viewer.add_surface((result[0], result[1], new_values), colormap='inferno')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(20190, 3)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result[0].shape" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(20181,)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean_intensity.shape" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", "\n", "
\n", "vedo.mesh.Mesh
(...yssey/_unwrapping/mesh_out.obj)\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
bounds
(x/y/z)
4.731e-3 ... 697.8
0.7769 ... 697.0
0 ... 0
center of mass (355, 351, 0)
average size 258.422
nr. points / faces 20190 / 39892
cell data array GroupIds
\n", "
" ], "text/plain": [ "(Mesh)000001FA05651A60" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vedo.load(r'../../../src/napari_shape_odyssey/_unwrapping/mesh_out.obj').clean()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", "\n", "
\n", "vedo.mesh.Mesh
(...dyssey/_unwrapping/mesh_in.obj)\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
bounds
(x/y/z)
-50.24 ... 59.02
-310.9 ... 321.9
-319.0 ... 319.6
center of mass (7.84e-5, -2.03e-3, 7.24e-4)
average size 253.595
nr. points / faces 20181 / 39892
cell data array GroupIds
\n", "
" ], "text/plain": [ "(Mesh)000001FA27074A00" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vedo.load(r'../../../src/napari_shape_odyssey/_unwrapping/mesh_in.obj').clean()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "test_out = vedo.load(os.path.join(r'C:\\Users\\johamuel\\Downloads\\windows-v1.6', 'test_out.obj'))\n", "test = vedo.load(os.path.join(r'C:\\Users\\johamuel\\Downloads\\windows-v1.6', 'test.obj'))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "121567" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test.npoints" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "122517" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_out.npoints" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "napari-shape-odyssey", "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.9.17" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }