{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Segmentation 3D\n", "\n", "## 0. Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import openalea.phenomenal.data as phm_data\n", "import openalea.phenomenal.display.notebook as phm_display_notebook\n", "import openalea.phenomenal.object as phm_obj\n", "import openalea.phenomenal.segmentation as phm_seg\n", "from openalea.phenotyping_data.fetch import fetch_all_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Prerequisites\n", "\n", "### 1.1 Load cloud point of voxel centers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plant_number = 2\n", "voxels_size = 16\n", "data_dir = fetch_all_data(f\"plant_{plant_number}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bin_images = phm_data.bin_images(data_dir)\n", "calibrations = phm_data.calibrations(data_dir)\n", "voxel_grid = phm_data.voxel_grid(data_dir, voxels_size=voxels_size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 Display voxel grid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display_notebook.show_voxel_grid(voxel_grid, size=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Skeletonization\n", "\n", "### 2.1 Compute Graph and the skeleton\n", "Compute the skeleton of the plant voxelgrid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = phm_seg.graph_from_voxel_grid(voxel_grid, connect_all_point=True)\n", "voxel_skeleton = phm_seg.skeletonize(voxel_grid, graph)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2 Display it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display_notebook.show_skeleton(voxel_skeleton, with_voxel=True, size=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3 Remove not enouth visible segment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Select images\n", "image_projection = list()\n", "for angle in bin_images[\"side\"]:\n", " projection = calibrations[\"side\"].get_projection(angle)\n", " image_projection.append((bin_images[\"side\"][angle], projection))\n", "\n", "voxel_skeleton_denoised = phm_seg.segment_reduction(\n", " voxel_skeleton, image_projection, required_visible=5, nb_min_pixel=400\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.4 Display it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display_notebook.show_skeleton(voxel_skeleton_denoised, size=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Maize Segmentation\n", "\n", "### 3.1 Process\n", "\n", "Voxel skeleton are segmented in several label \"stem\", \"mature_leaf\", \"growing_leaf\" and \"unknow\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vms = phm_seg.maize_segmentation(voxel_skeleton_denoised, graph)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.2 Write / Read" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write\n", "vms.write_to_json_gz(\"voxel_maize_segmentation.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load\n", "vms = phm_obj.VoxelSegmentation.read_from_json_gz(\"voxel_maize_segmentation.json\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.3 Display" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display_notebook.show_segmentation(vms, size=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Maize Analysis\n", "\n", "### 4.1 Process" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vmsi = phm_seg.maize_analysis(vms)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.2 How to save analysis result :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vmsi.write_to_json_gz(\"voxel_maize_segmentation_info.json.gz\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vmsi = phm_obj.VoxelSegmentation.read_from_json_gz(\n", " \"voxel_maize_segmentation_info.json.gz\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.3 Visualize results: " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display_notebook.show_segmentation(vmsi, size=1)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.17" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }