{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bc5d3202-7004-4a30-9688-d590b7ff40ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "afcaafc9-bc9d-4b6e-a000-7daae79ca33c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def image_blur(image):\n",
    "    if image is None:\n",
    "        raise ValueError(\"Image is not loaded correctly.\")\n",
    "    # 高斯模糊\n",
    "    gaussian_blur = cv2.GaussianBlur(image, (15, 15), 0)\n",
    "    # 均值模糊\n",
    "    mean_blur = cv2.blur(image, (15, 15))\n",
    "    # 中值模糊\n",
    "    median_blur = cv2.medianBlur(image, 15)\n",
    "    return gaussian_blur,mean_blur,median_blur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "34a40ed3-6050-4161-ba30-8156473f8078",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: Image not found or unable to load.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ WARN:0@1179.199] global loadsave.cpp:268 findDecoder imread_('002.png'): can't open/read file: check file path/integrity\n"
     ]
    }
   ],
   "source": [
    "image_path = '002.png'\n",
    "image = cv2.imread(image_path)\n",
    "if image is None:\n",
    "    raise ValueError(\"Error: Image not found or unable to load.\")\n",
    "\n",
    "\n",
    "    # gaussian_blur, mean_blur, median_blur = image_blur(image)\n",
    "    # cv2.imwrite('gaussian_blur.jpg', gaussian_blur)\n",
    "    # cv2.imwrite('mean_blur.jpg', mean_blur)\n",
    "    # cv2.imwrite('median_blur.jpg', median_blur)\n",
    "# gaussian_blur,mean_blur,median_blur=image_blur(image)\n",
    "# cv2.imwrite('123', image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a745fe14-a960-4668-bd98-c17025619f1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def brenner(image):\n",
    "    gradient = np.zeros_like(image, dtype=np.float32)\n",
    "    gradient[:-2, :] = image[2:, :] - image[:-2, :]\n",
    "    return np.var(gradient)\n",
    "\n",
    "# 计算清晰度\n",
    "brenner_original = brenner(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))\n",
    "brenner_gaussian = brenner(cv2.cvtColor(gaussian_blur, cv2.COLOR_BGR2GRAY))\n",
    "brenner_mean = brenner(cv2.cvtColor(mean_blur, cv2.COLOR_BGR2GRAY))\n",
    "brenner_median = brenner(cv2.cvtColor(median_blur, cv2.COLOR_BGR2GRAY))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "45a1739c-20fe-4df8-b703-732c23ff3400",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ WARN:0@1400.488] global loadsave.cpp:268 findDecoder imread_('002.png'): can't open/read file: check file path/integrity\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Error: Image not found or unable to load.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[25], line 6\u001b[0m\n\u001b[1;32m      4\u001b[0m image \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(image_path)\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m image \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m----> 6\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: Image not found or unable to load.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m      7\u001b[0m cv2\u001b[38;5;241m.\u001b[39mimwrite(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m123.png\u001b[39m\u001b[38;5;124m'\u001b[39m, image)\n",
      "\u001b[0;31mValueError\u001b[0m: Error: Image not found or unable to load."
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "\n",
    "image_path = '002.png'\n",
    "image = cv2.imread(image_path)\n",
    "if image is None:\n",
    "    raise ValueError(\"Error: Image not found or unable to load.\")\n",
    "cv2.imwrite('123.png', image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f645692-aca6-4701-8382-99b993eef448",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pmn_env",
   "language": "python",
   "name": "pmn_env"
  },
  "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.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
