{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5d62d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2799ab25",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Базовая работа с изображением"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5037aa29",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = cv2.imread('sar_2_color.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3e5f356b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad6776dc40>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 490192 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37b8f016",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(572, 974, 3)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image.shape # h,w,c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e4530952",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12, 12, 12], dtype=uint8)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image[250,250] # b,g,r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "51aab9ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ROI\n",
    "img_roi = image[100:200, 500:700]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "70010d8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad673c1e80>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 265232 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(img_roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f882dac3",
   "metadata": {},
   "outputs": [],
   "source": [
    "b,g,r = cv2.split(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1f89e4b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad673bd160>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 301692 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(b, cmap = 'gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "80f48335",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad66fc5a60>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 308608 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(g, cmap = 'gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "14bf6bec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# alternative approach\n",
    "b = image[:,:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "65f6cb65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "\n",
    "image2 = copy.deepcopy(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6e348740",
   "metadata": {},
   "outputs": [],
   "source": [
    "image2[50:100,50:100] = [0,0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f0af9079",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad5cd46150>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 488212 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(image2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7b3beafd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# empty image\n",
    "image_template = np.zeros(image.shape,np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "649438f3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad673d2960>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 10012 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(image_template)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c9b56ab",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Конвертация цветовых моделей"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ddf46c8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0], dtype=uint8)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_template[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "90601bc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4254803d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.uint8(40)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_gray[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "abb0a164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(572, 974)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_gray.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f282c88e",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d37c08b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(572, 974, 3)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_hsv.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ae9ef2ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([117, 143,  75], dtype=uint8)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_hsv[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d8cdda21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([75, 37, 33], dtype=uint8)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "73fdb374",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0ed9c8e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 42, 139, 104], dtype=uint8)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_lab[0,0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0d7f4f0",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Пороговая фильтрация"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0b6d5e3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "_,thresh1 = cv2.threshold(image_gray,200,255,cv2.THRESH_BINARY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6e84b905",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad5cda2360>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 54244 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(thresh1, cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f5e1453e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.uint64(0)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "thresh1[thresh1==100].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "396e3b66",
   "metadata": {},
   "source": [
    "## Построение гистограммы"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4b455e07",
   "metadata": {},
   "outputs": [],
   "source": [
    "histSize = 256\n",
    "histRange = (0, 256)\n",
    "accumulate = False\n",
    "\n",
    "b_hist = cv2.calcHist([b], [0], None, [histSize], histRange, accumulate=accumulate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "477df536",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fad66fb35c0>]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 30304 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(b_hist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "92636df5",
   "metadata": {},
   "outputs": [],
   "source": [
    "b_hist_cum = b_hist.cumsum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1a195130",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fad5cb2c860>]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 25148 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(b_hist_cum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e11f8476",
   "metadata": {},
   "outputs": [],
   "source": [
    "b_hist_norm = b_hist /  (image.shape[0] * image.shape[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ae7a2b79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fad5cb916d0>]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 25912 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(b_hist_norm)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa3739f4",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Сравнение двух изображений"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b67e4981",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SSIM: 1.0\n"
     ]
    }
   ],
   "source": [
    "from skimage.metrics import structural_similarity, mean_squared_error\n",
    "\n",
    "(ssim, diff) = structural_similarity(image_gray, image_gray, full=True)\n",
    "diff = (diff * 255).astype(\"uint8\")\n",
    "print(\"SSIM: {}\".format(ssim))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e130dabe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad42eeb890>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 10220 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(diff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "03ab140b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mse = mean_squared_error(image_gray, image_gray)\n",
    "mse"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7c085bc",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Статистические характеристики изображений"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "de9202f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "mean = image_gray.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "aaff4206",
   "metadata": {},
   "outputs": [],
   "source": [
    "std = image_gray.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "248147b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "67.41225535245043 52.016191875959635\n"
     ]
    }
   ],
   "source": [
    "print(mean,std)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "3fbab27e",
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_gray = cv2.equalizeHist(image_gray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "250af16c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad42f65160>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 340820 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(eq_gray, cmap=\"gray\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "7c013a0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fad42f6ea50>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 306872 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(image_gray, cmap=\"gray\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3ad3ea1-b103-4383-8219-946233ef84c5",
   "metadata": {},
   "source": [
    "# Задание"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6329e214",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. Загрузите изображение в оттенках серого sar_1_gray.jpg. \n",
    "# 2. постройте гистограмму\n",
    "# 3. реализуйте алгоритм гамма коррекции с параметром гамма <1, >1.\n",
    "# 4. Сравните исходное изображение, скорректированное при помощи гамма-фильтра. MSE, SSIM.\n",
    "# 5. реализуйте алгоритм статистической цветокоррекции на основе статистики eq_gray.\n",
    "# 6. Протестируйте работу алгоритмов пороговой фильтрации с различными параметрами.\n",
    "# Для каждого решения - напечатайте результат\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2427bbf1-ffed-4948-80e5-a102df665e9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#1\n",
    "image_gray = cv2.imread('sar_1_gray.jpg', cv2.IMREAD_GRAYSCALE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3134249-c6c5-4373-bcf1-09bee7f2a87d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
