{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "61468bff", "metadata": {}, "outputs": [], "source": [ "from io import BytesIO\n", "\n", "import cv2\n", "import IPython.display\n", "import numpy as np\n", "import PIL.Image\n", "from ipywidgets import widgets\n", "\n", "from hsv_utils import identify_object, resize_img\n", "\n", "img_hsv = None # Initialize global variable\n", "img_rgb = None # Initialize global variable" ] }, { "cell_type": "code", "execution_count": null, "id": "hourly-navigator", "metadata": { "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [], "source": [ "def hsv_select(**kwargs) -> None:\n", " hsv_min = tuple([int(low) for low, high in kwargs.values()])\n", " hsv_max = tuple([int(high) for low, high in kwargs.values()])\n", " hsv_min_new = (hsv_min[0]/2, hsv_min[1], hsv_min[2])\n", " hsv_max_new = (hsv_max[0]/2, hsv_max[1], hsv_max[2])\n", " mask = cv2.inRange(img_hsv, hsv_min_new, hsv_max_new)\n", " img_masked = cv2.bitwise_and(img_rgb, img_rgb, mask = mask)\n", " f = BytesIO()\n", " PIL.Image.fromarray(img_masked).save(f, \"jpeg\")\n", " img_jpeg = IPython.display.Image(data=f.getvalue())\n", " IPython.display.display_jpeg(img_jpeg)" ] }, { "cell_type": "code", "execution_count": null, "id": "external-purple", "metadata": { "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [], "source": [ "# 画像を読み込む。\n", "img = cv2.imread(\"./picture/red_blue_cone.jpg\")\n", "img = resize_img(img)\n", "\n", "img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", "img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n", "\n", "# hsvを設定するスライダー\n", "hsv = {}\n", "hsv[\"h\"] = widgets.IntRangeSlider(\n", " value=[0, 360], min=0, max=360, description=\"Hue:\", continuous_update=True, layout=widgets.Layout(width=\"100%\"),\n", ")\n", "hsv[\"s\"] = widgets.IntRangeSlider(\n", " value=[0, 255],\n", " min=0,\n", " max=255,\n", " description=\"Saturation:\",\n", " continuous_update=True,\n", " layout=widgets.Layout(width=\"100%\"),\n", ")\n", "hsv[\"v\"] = widgets.IntRangeSlider(\n", " value=[0, 255], min=0, max=255, description=\"Value:\", continuous_update=True, layout=widgets.Layout(width=\"100%\"),\n", ")\n", "\n", "\n", "# マスクされた画像を表示\n", "widgets.interactive(hsv_select, **hsv)" ] }, { "cell_type": "code", "execution_count": null, "id": "toxic-possession", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [], "source": [ "# HSVの下限と上限を設定\n", "HSV_LOWER = np.array([0, 0, 0])\n", "HSV_UPPER = np.array([360, 255, 255])" ] }, { "cell_type": "code", "execution_count": null, "id": "seasonal-cigarette", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [], "source": [ "# 物体とみなす最小サイズ\n", "EXIST_SIZE = 100" ] }, { "cell_type": "code", "execution_count": null, "id": "friendly-stewart", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [], "source": [ "# 検出された最も大きな物体に輪郭の線を表示する\n", "identify_object(img, HSV_LOWER, HSV_UPPER, EXIST_SIZE)" ] } ], "metadata": { "kernelspec": { "display_name": "img-rec-exp-ver2", "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.13.8" } }, "nbformat": 4, "nbformat_minor": 5 }