{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6ac35470",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "from pathlib import Path\n",
    "from vision_chunk import VisionChunk\n",
    "from position import Position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eb2e908f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 151020 characters]",
      "image/png": "[encoded data, 1675964 characters]",
      "text/plain": [
       "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=700x700>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir = Path('trajectories') / '2026-01-11_14-42-08'\n",
    "chunk_land = VisionChunk.load_image(dir / 'chunks' / 'chunk_0.png')\n",
    "chunk_land.image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cce46307",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 182108 characters]",
      "image/png": "[encoded data, 1753776 characters]",
      "text/plain": [
       "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=700x700>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir_run = Path('images')\n",
    "chunk_run = VisionChunk.load_image(dir_run / 'photo_0.png')\n",
    "\n",
    "_chunk_run = VisionChunk.load_image(dir_run / 'photo_1.png')\n",
    "\n",
    "chunk_run.image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0571055f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 3534144 characters]",
      "text/plain": [
       "<Figure size 1600x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Найдено совпадений: 1206\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "# Получаем совпадения и keypoints\n",
    "pts_src, pts_dst, matches, kp1, kp2 = _chunk_run.detect_and_match_keypoints(chunk_run)\n",
    "\n",
    "# Конвертируем изображения в numpy arrays\n",
    "img1 = _chunk_run.to_numpy()\n",
    "img2 = chunk_run.to_numpy()\n",
    "\n",
    "# Создаем объединенное изображение\n",
    "h1, w1 = img1.shape[:2]\n",
    "h2, w2 = img2.shape[:2]\n",
    "h_max = max(h1, h2)\n",
    "\n",
    "# Создаем холст\n",
    "vis = np.zeros((h_max, w1 + w2, 3), dtype=np.uint8)\n",
    "vis[:h1, :w1] = img1 if img1.ndim == 3 else np.stack([img1]*3, axis=-1)\n",
    "vis[:h2, w1:w1+w2] = img2 if img2.ndim == 3 else np.stack([img2]*3, axis=-1)\n",
    "\n",
    "# Рисуем\n",
    "fig, ax = plt.subplots(figsize=(16, 8))\n",
    "ax.imshow(vis)\n",
    "\n",
    "# Рисуем линии совпадений\n",
    "for i in range(len(pts_src)):\n",
    "    pt1 = pts_src[i][0]  # (x, y) в первом изображении\n",
    "    pt2 = pts_dst[i][0]  # (x, y) во втором изображении\n",
    "    \n",
    "    # Смещаем координаты второго изображения\n",
    "    x1, y1 = pt1[0], pt1[1]\n",
    "    x2, y2 = pt2[0] + w1, pt2[1]\n",
    "    \n",
    "    # Рисуем линию\n",
    "    ax.plot([x1, x2], [y1, y2], 'g-', linewidth=0.5, alpha=0.6)\n",
    "    ax.plot(x1, y1, 'ro', markersize=2)\n",
    "    ax.plot(x2, y2, 'bo', markersize=2)\n",
    "\n",
    "ax.axis('off')\n",
    "ax.set_title(f'Feature Matches: {len(pts_src)} points')\n",
    "plt.tight_layout()\n",
    "plt.savefig('matches.png', dpi=150, bbox_inches='tight')\n",
    "plt.show()\n",
    "\n",
    "print(f\"Найдено совпадений: {len(pts_src)}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "55860992",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(1)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((np.array([\n",
    "    [3, 4],\n",
    "    [4, 3],\n",
    "    [3, 4]\n",
    "]) + np.array([1, 2])) ** 2).sum(axis=1).argmin()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
