{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Binarization\n", "\n", "## 0. Import package" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import cv2\n", "import collections\n", "\n", "import openalea.phenomenal.image as phm_img\n", "import openalea.phenomenal.data as phm_data\n", "import openalea.phenomenal.display as phm_display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Prerequisites\n", "\n", "### 1. Load images\n", "\n", "Here, we load all the image of the dataset to simply the toturial, commodly you load just the images you want process one after the other." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "raw_images = phm_data.raw_images(\"data/plant_6\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.3. Display images \n", "\n", "You can view each image according to this date and angle of view like this :\n", "Note : Angle top view is represented by negative number (-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display.show_images(\n", " [raw_images[\"side\"][120], raw_images[\"side\"][30], raw_images[\"top\"][0]]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Binarization\n", "\n", "### 2.1. Define a binarization routines" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def routine_side_binarization(image, mean_img):\n", " maks = phm_data.tutorial_data_binarization_mask(\"data\")\n", "\n", " threshold = 0.3\n", " dark_background = False\n", "\n", " hsv_min = (30, 11, 0)\n", " hsv_max = (129, 254, 141)\n", "\n", " # Convert image on HSV representation\n", " hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n", " # Threshold the image with HSV min and max value\n", " binary_hsv_image = phm_img.threshold_hsv(hsv_image, hsv_min, hsv_max, maks[0])\n", "\n", " # Threshold the image with difference between image and mean_image\n", " binary_mean_shift_image = phm_img.threshold_meanshift(\n", " image, mean_img, threshold, dark_background, maks[1]\n", " )\n", "\n", " # Add the two image\n", " result = cv2.add(binary_hsv_image, binary_mean_shift_image)\n", "\n", " # Erode and dilate the image to remove possible noise\n", " result = cv2.medianBlur(result, 3)\n", "\n", " return result\n", "\n", "\n", "def routine_top_binarization(image):\n", " hsv_min = (42, 75, 28)\n", " hsv_max = (80, 250, 134)\n", " median_blur_size = 9\n", " iterations = 5\n", "\n", " # Convert image on HSV representation\n", " hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n", " # Apply a median blur on the image\n", " hsv_image = cv2.medianBlur(hsv_image, ksize=median_blur_size)\n", "\n", " # Threshold the image with HSV min and max value\n", " bin_img = phm_img.threshold_hsv(hsv_image, hsv_min, hsv_max)\n", " # dilate and erode the image to remove possible noise\n", " bin_img = phm_img.dilate_erode(bin_img, kernel_shape=(3, 3), iterations=iterations)\n", "\n", " return bin_img" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2. Binarize images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compute the mean image of the side view image\n", "\n", "mean_img = phm_img.mean_image(list(raw_images[\"side\"].values()))\n", "\n", "routine_binarization = {\n", " \"side\": lambda im: routine_side_binarization(im, mean_img),\n", " \"top\": lambda im: routine_top_binarization(im),\n", "}\n", "\n", "bin_images = collections.defaultdict(dict)\n", "for id_camera in raw_images:\n", " for angle in raw_images[id_camera]:\n", " bin_images[id_camera][angle] = routine_binarization[id_camera](\n", " raw_images[id_camera][angle]\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3. Display images binarize " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id_camera, angle = \"side\", 120\n", "phm_display.show_images(\n", " [raw_images[id_camera][angle], mean_img, bin_images[id_camera][angle]]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id_camera, angle = \"top\", 0\n", "phm_display.show_images([raw_images[id_camera][angle], bin_images[id_camera][angle]])" ] } ], "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.12.7" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }