{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 41040 characters]",
      "text/plain": [
       "<Figure size 1500x500 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "\n",
    "# 加载图像\n",
    "image = Image.open('rice.png').convert('L')  # 转换为灰度图像\n",
    "pixels = np.array(image)\n",
    "\n",
    "# 定义灰度级数\n",
    "gray_levels = [64, 128, 256]\n",
    "\n",
    "# 创建直方图\n",
    "fig, axs = plt.subplots(1, 3, figsize=(15, 5))\n",
    "\n",
    "for i, levels in enumerate(gray_levels):\n",
    "    # 使用最近邻法将像素值量化到指定灰度级\n",
    "    pixels_quantized = np.digitize(pixels, bins=np.linspace(0, 255, levels))\n",
    "    # 计算直方图\n",
    "    hist, bins = np.histogram(pixels_quantized, bins=levels, range=(0, levels))\n",
    "    axs[i].bar(bins[:-1], hist, width=bins[1]-bins[0], align='center')\n",
    "    axs[i].set_title(f'Gray Levels: {levels}')\n",
    "    axs[i].set_xlabel('Pixel Value')\n",
    "    axs[i].set_ylabel('Frequency')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 234316 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 30224 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from skimage import data, exposure, io\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "# 加载示例图像（由于用户没有上传图像，我们将使用skimage内置的示例图像）\n",
    "image = io.imread('rice.png')\n",
    "\n",
    "# 调整图像的灰度范围\n",
    "def imadjust(I, low_in, high_in, low_out, high_out):\n",
    "    return exposure.rescale_intensity(I, in_range=(low_in, high_in), out_range=(low_out, high_out))\n",
    "\n",
    "# 设置灰度范围的低端和高端\n",
    "low_in, high_in = 40, 210\n",
    "low_out, high_out = 0, 255\n",
    "\n",
    "# 调整图像灰度范围\n",
    "adjusted_image = imadjust(image, low_in, high_in, low_out, high_out)\n",
    "\n",
    "# 绘制原始图像和调整后的图像\n",
    "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))\n",
    "ax1.imshow(image, cmap='gray')\n",
    "ax1.set_title('Original Image')\n",
    "ax1.axis('off')\n",
    "\n",
    "ax2.imshow(adjusted_image, cmap='gray')\n",
    "ax2.set_title('Adjusted Image')\n",
    "ax2.axis('off')\n",
    "\n",
    "# 绘制直方图\n",
    "fig, axs = plt.subplots(1, 2, figsize=(10, 5))\n",
    "axs[0].hist(image.ravel(), 256, [0, 256])\n",
    "axs[0].set_title('Histogram of Original Image')\n",
    "axs[0].set_xlabel('Pixel Value')\n",
    "axs[0].set_ylabel('Frequency')\n",
    "\n",
    "axs[1].hist(adjusted_image.ravel(), 256, [0, 256])\n",
    "axs[1].set_title('Histogram of Adjusted Image')\n",
    "axs[1].set_xlabel('Pixel Value')\n",
    "axs[1].set_ylabel('Frequency')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 337996 characters]",
      "text/plain": [
       "<Figure size 1500x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 406672 characters]",
      "text/plain": [
       "<Figure size 1500x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 绘制直方图\n",
    "def plot_histogram(image, title):\n",
    "    plt.hist(image.ravel(), 256, [0, 256])\n",
    "    plt.title(title)\n",
    "    plt.xlabel('Pixel Intensity')\n",
    "    plt.ylabel('Frequency')\n",
    "\n",
    "# 显示图像\n",
    "def display_image(image, title):\n",
    "    plt.imshow(image, cmap='gray')\n",
    "    plt.title(title)\n",
    "    plt.axis('off')\n",
    "\n",
    "# 图像路径\n",
    "image_paths = ['pout.tif', 'tire.tif']\n",
    "\n",
    "for image_path in image_paths:\n",
    "    # 加载图像\n",
    "    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # 直方图均衡化\n",
    "    equalized_image = cv2.equalizeHist(image)\n",
    "\n",
    "    # 绘制原图像和直方图\n",
    "    plt.figure(figsize=[15, 10])\n",
    "    plt.subplot(2, 2, 1)\n",
    "    display_image(image, 'Original Image')\n",
    "    plt.subplot(2, 2, 2)\n",
    "    plot_histogram(image, 'Histogram of Original Image')\n",
    "\n",
    "    # 绘制处理后的图像和直方图\n",
    "    plt.subplot(2, 2, 3)\n",
    "    display_image(equalized_image, 'Equalized Image')\n",
    "    plt.subplot(2, 2, 4)\n",
    "    plot_histogram(equalized_image, 'Histogram of Equalized Image')\n",
    "\n",
    "    # 显示结果\n",
    "    plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n",
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 322772 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from skimage.exposure import equalize_hist\n",
    "\n",
    "# 读取彩色图像\n",
    "image_path = 'DJI_0049.JPG'\n",
    "image = Image.open(image_path)\n",
    "image = np.array(image)\n",
    "\n",
    "# 分离RGB通道\n",
    "R, G, B = image[:, :, 0], image[:, :, 1], image[:, :, 2]\n",
    "\n",
    "# 对每个通道进行直方图均衡化\n",
    "R_equalized = equalize_hist(R)\n",
    "G_equalized = equalize_hist(G)\n",
    "B_equalized = equalize_hist(B)\n",
    "\n",
    "# 合并均衡化后的通道\n",
    "image_equalized = np.dstack([R_equalized, G_equalized, B_equalized])\n",
    "\n",
    "# 显示原图和均衡化后的图像\n",
    "fig, ax = plt.subplots(1, 4, figsize=(12, 6))\n",
    "ax[0].imshow(image)\n",
    "ax[0].axis('off')\n",
    "ax[0].set_title('before')\n",
    "\n",
    "ax[1].imshow(np.dstack([R_equalized, G_equalized, B_equalized]))\n",
    "ax[1].axis('off')\n",
    "ax[1].set_title('after')\n",
    "\n",
    "ax[2].imshow(np.dstack([R_equalized, G, B]))\n",
    "ax[2].axis('off')\n",
    "ax[2].set_title('only R')\n",
    "\n",
    "ax[3].imshow(np.dstack([R_equalized, G_equalized, B]))\n",
    "ax[3].axis('off')\n",
    "ax[3].set_title('RG')\n",
    "\n",
    "# ax[4].imshow(np.dstack([R, G, B]))\n",
    "# ax[4].axis('off')\n",
    "# ax[4].set_title('only B')\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n",
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 331520 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from skimage.exposure import equalize_hist\n",
    "\n",
    "# 读取彩色图像\n",
    "image_path = 'DJI_0049.JPG'\n",
    "image = Image.open(image_path)\n",
    "image = image.convert('HSV')\n",
    "image = np.array(image)\n",
    "\n",
    "# 分离RGB通道\n",
    "H, S, V = image[:, :, 0], image[:, :, 1], image[:, :, 2]\n",
    "\n",
    "# 对每个通道进行直方图均衡化\n",
    "H_equalized = equalize_hist(H)\n",
    "S_equalized = equalize_hist(S)\n",
    "V_equalized = equalize_hist(V)\n",
    "\n",
    "# 合并均衡化后的通道\n",
    "image_equalized = np.dstack([H_equalized, S_equalized, V_equalized])\n",
    "\n",
    "# 显示原图和均衡化后的图像\n",
    "fig, ax = plt.subplots(1, 4, figsize=(12, 6))\n",
    "\n",
    "ax[0].imshow(image)\n",
    "ax[0].axis('off')\n",
    "ax[0].set_title('before')\n",
    "\n",
    "ax[1].imshow(np.dstack([R_equalized, G_equalized, B_equalized]))\n",
    "ax[1].axis('off')\n",
    "ax[1].set_title('after')\n",
    "\n",
    "ax[2].imshow(np.dstack([R_equalized, G, B]))\n",
    "ax[2].axis('off')\n",
    "ax[2].set_title('only R')\n",
    "\n",
    "ax[3].imshow(np.dstack([R_equalized, G_equalized, B]))\n",
    "ax[3].axis('off')\n",
    "ax[3].set_title('RG')\n",
    "\n",
    "# ax[4].imshow(np.dstack([R, G, B]))\n",
    "# ax[4].axis('off')\n",
    "# ax[4].set_title('only B')\n",
    "\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cv_course",
   "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
