120 lines
7.3 KiB
Plaintext
Executable File
120 lines
7.3 KiB
Plaintext
Executable File
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import cv2\n",
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import random\n",
|
|
"\n",
|
|
"def add_salt_and_pepper_noise(image, salt_pepper_ratio=0.02, amount=0.004):\n",
|
|
" # 获取图像的宽高\n",
|
|
" row, col = image.shape\n",
|
|
" # 计算要添加的噪声数量\n",
|
|
" num_salt = np.ceil(amount * image.size * salt_pepper_ratio)\n",
|
|
" num_pepper = np.ceil(amount * image.size * (1.0 - salt_pepper_ratio))\n",
|
|
"\n",
|
|
" # 随机选择像素点\n",
|
|
" coords = [random.randint(0, i - 1) for i in image.shape for _ in range(num_salt)]\n",
|
|
" coords.extend([random.randint(0, i - 1) for i in image.shape for _ in range(num_pepper)])\n",
|
|
" \n",
|
|
" # 将选中的像素点设置为白或黑\n",
|
|
" for coord in coords:\n",
|
|
" image[coord] = 255 if random.random() < salt_pepper_ratio else 0\n",
|
|
"\n",
|
|
" return image\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "'numpy.float64' object cannot be interpreted as an integer",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
|
"Cell \u001b[0;32mIn[5], line 5\u001b[0m\n\u001b[1;32m 2\u001b[0m I \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlena.bmp\u001b[39m\u001b[38;5;124m'\u001b[39m, cv2\u001b[38;5;241m.\u001b[39mIMREAD_GRAYSCALE)\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# 添加椒盐噪声\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m J \u001b[38;5;241m=\u001b[39m \u001b[43madd_salt_and_pepper_noise\u001b[49m\u001b[43m(\u001b[49m\u001b[43mI\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# 显示原图像和加噪图像\u001b[39;00m\n\u001b[1;32m 8\u001b[0m plt\u001b[38;5;241m.\u001b[39msubplot(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m1\u001b[39m)\n",
|
|
"Cell \u001b[0;32mIn[2], line 11\u001b[0m, in \u001b[0;36madd_salt_and_pepper_noise\u001b[0;34m(image, salt_pepper_ratio, amount)\u001b[0m\n\u001b[1;32m 8\u001b[0m num_pepper \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mceil(amount \u001b[38;5;241m*\u001b[39m image\u001b[38;5;241m.\u001b[39msize \u001b[38;5;241m*\u001b[39m (\u001b[38;5;241m1.0\u001b[39m \u001b[38;5;241m-\u001b[39m salt_pepper_ratio))\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# 随机选择像素点\u001b[39;00m\n\u001b[0;32m---> 11\u001b[0m coords \u001b[38;5;241m=\u001b[39m \u001b[43m[\u001b[49m\u001b[43mrandom\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrandint\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mimage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshape\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m_\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mrange\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mnum_salt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 12\u001b[0m coords\u001b[38;5;241m.\u001b[39mextend([random\u001b[38;5;241m.\u001b[39mrandint(\u001b[38;5;241m0\u001b[39m, i \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m image\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(num_pepper)])\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# 将选中的像素点设置为白或黑\u001b[39;00m\n",
|
|
"Cell \u001b[0;32mIn[2], line 11\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 8\u001b[0m num_pepper \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mceil(amount \u001b[38;5;241m*\u001b[39m image\u001b[38;5;241m.\u001b[39msize \u001b[38;5;241m*\u001b[39m (\u001b[38;5;241m1.0\u001b[39m \u001b[38;5;241m-\u001b[39m salt_pepper_ratio))\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# 随机选择像素点\u001b[39;00m\n\u001b[0;32m---> 11\u001b[0m coords \u001b[38;5;241m=\u001b[39m [random\u001b[38;5;241m.\u001b[39mrandint(\u001b[38;5;241m0\u001b[39m, i \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m image\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28;43mrange\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mnum_salt\u001b[49m\u001b[43m)\u001b[49m]\n\u001b[1;32m 12\u001b[0m coords\u001b[38;5;241m.\u001b[39mextend([random\u001b[38;5;241m.\u001b[39mrandint(\u001b[38;5;241m0\u001b[39m, i \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m image\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(num_pepper)])\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# 将选中的像素点设置为白或黑\u001b[39;00m\n",
|
|
"\u001b[0;31mTypeError\u001b[0m: 'numpy.float64' object cannot be interpreted as an integer"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# 读取图像\n",
|
|
"I = cv2.imread('lena.bmp', cv2.IMREAD_GRAYSCALE)\n",
|
|
"\n",
|
|
"# 添加椒盐噪声\n",
|
|
"J = add_salt_and_pepper_noise(I)\n",
|
|
"\n",
|
|
"# 显示原图像和加噪图像\n",
|
|
"plt.subplot(2, 2, 1)\n",
|
|
"plt.imshow(I, cmap='gray')\n",
|
|
"plt.title('原图像')\n",
|
|
"plt.subplot(2, 2, 2)\n",
|
|
"plt.imshow(J, cmap='gray')\n",
|
|
"plt.title('加入椒盐噪声的图像')\n",
|
|
"\n",
|
|
"# 均值滤波处理\n",
|
|
"K1 = cv2.blur(J, (3, 3))\n",
|
|
"K2 = cv2.blur(J, (5, 5))\n",
|
|
"\n",
|
|
"# 显示滤波处理结果\n",
|
|
"plt.subplot(2, 2, 3)\n",
|
|
"plt.imshow(K1, cmap='gray')\n",
|
|
"plt.title('3x3 均值滤波处理结果')\n",
|
|
"plt.subplot(2, 2, 4)\n",
|
|
"plt.imshow(K2, cmap='gray')\n",
|
|
"plt.title('5x5 均值滤波处理结果')\n",
|
|
"\n",
|
|
"# 显示图像\n",
|
|
"plt.show()\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"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
|
|
}
|