{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Red channel:\n", "tensor([[0.0000],\n", " [0.9294]])\n", "Green channel:\n", "tensor([[0.0000],\n", " [0.1098]])\n", "Blue channel:\n", "tensor([[0.0000],\n", " [0.1412]])\n", "Alpha channel:\n", "tensor([[0.],\n", " [1.]])\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMwAAAGFCAYAAACxAhziAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAEO0lEQVR4nO3VsQ3CUBAFQYyowDkh/RdESE4LRwl4A+vL0kx8wUtWt83M3IBD7qsHwJUIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEgoFAMBA8jh5+n68zd8By++f998aHgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDASCgUAwEAgGAsFAIBgIBAOBYCAQDATbzMzqEXAVPgwEgoFAMBAIBgLBQCAYCAQDgWAgEAwEP8rSDgOaVu6AAAAAAElFTkSuQmCC", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "from PIL import Image\n", "from torchvision import transforms\n", "\n", "\n", "def show_rgba(image_path):\n", " # See RGBA data\n", " image = Image.open(image_path)\n", " to_tensor = transforms.ToTensor()\n", " tensor = to_tensor(image)\n", "\n", " for i, color in enumerate(['Red', 'Green', 'Blue', 'Alpha']):\n", " print(f\"{color} channel:\")\n", " print(tensor[i])\n", " plt.imshow(tensor.permute(1, 2, 0))\n", " plt.axis('off')\n", " plt.show()\n", "\n", "\n", "show_rgba('../data/samples/transparent_indonesia_flag.png')\n", "# Alpha channel: tensor([[0.], [1.]]). It means alpha 0 is transparent.\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np\n", "import os\n", "\n", "from PIL import Image\n", "\n", "\n", "def get_object_bounding_boxes(image):\n", " # アルファチャンネルを取得し、バイナリマスクを作成\n", " alpha_channel = image[:, :, 3]\n", "\n", " # cv2.threshold関数を使用して、アルファチャンネルの値が1以上のピクセルを255(白)に、それ以外を0(黒)に変換します。\n", " # これにより、画像のオブジェクト部分を白、背景部分を黒としたバイナリマスクが作成されます。\n", " _, binary_mask = cv2.threshold(alpha_channel, 1, 255, cv2.THRESH_BINARY)\n", "\n", " # 輪郭を検出\n", " contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n", "\n", " return contours or []\n", "\n", "\n", "def show_bounding_boxes(image_path):\n", " # 透明背景の画像をRGBA形式で読み込む\n", " image_pil = Image.open(image_path)\n", "\n", " # PIL画像をOpenCV形式に変換\n", " image = np.array(image_pil)\n", "\n", " # バウンディングボックスの取得\n", " contours = get_object_bounding_boxes(image)\n", " rects = [cv2.boundingRect(c) for c in contours]\n", "\n", " # バウンディングボックスの描画\n", " image_bgr = cv2.cvtColor(image[:, :, :3], cv2.COLOR_RGB2BGR)\n", " [\n", " cv2.rectangle(image_bgr, (x, y), (x + w, y + h), (0, 255, 0), 2)\n", " for [x,y,w,h] in rects\n", " ]\n", "\n", " # バウンディングボックスを適用した画像の表示\n", " cv2.imshow('Bounding Box', image_bgr)\n", " cv2.waitKey(0)\n", " cv2.destroyAllWindows()\n", "\n", "\n", "show_bounding_boxes('../data/nobg/ポケットモンスターシールド/2020022922273500_s.png')\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "input_dir = \"../data/nobg/ポケモン公式\"\n", "output_dir = \"../data/cropped/ポケモン公式\"\n", "\n", "def get_max_bounding_rect(contours):\n", " if len(contours) == 0:\n", " return 0,0,0,0\n", "\n", " c = max(contours, key=cv2.contourArea)\n", " x, y, w, h = cv2.boundingRect(c)\n", " return x, y, w, h\n", "\n", "# Loop over all files and subdirectories in the input directory\n", "for root, dirs, files in os.walk(input_dir):\n", " for filename in files:\n", " # Construct full file path\n", " file_path = os.path.join(root, filename)\n", " \n", " # Open the image and convert it to numpy array\n", " image_pil = Image.open(file_path)\n", " image = np.array(image_pil)\n", " \n", " # Get the bounding boxes of the objects in the image\n", " contours = get_object_bounding_boxes(image)\n", " \n", " if len(contours) == 0:\n", " continue\n", " \n", " # Get the maximum bounding rectangle\n", " x, y, w, h = get_max_bounding_rect(contours)\n", " \n", " # Crop the image\n", " cropped_image = image[y:y+h, x:x+w]\n", " \n", " cropped_image_pil = Image.fromarray(cropped_image)\n", " \n", " # Create output subdirectory if it doesn't exist\n", " output_subdir = os.path.join(output_dir, os.path.relpath(root, input_dir))\n", " os.makedirs(output_subdir, exist_ok=True)\n", " \n", " # Save the cropped image to the output directory\n", " output_file_path = os.path.join(output_subdir, filename)\n", " cropped_image_pil.save(output_file_path)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "pokemon-pal", "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.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }