A před 1 rokem
rodič
revize
71634f5d9d

+ 321 - 0
.ipynb_checkpoints/day3-checkpoint.ipynb

@@ -0,0 +1,321 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "62916051-266e-439b-9f14-5a16a9017622",
+   "metadata": {},
+   "source": [
+    "## 1. 回顾\n",
+    "\n",
+    ">1. Git上传\n",
+    ">2. 图像的处理\n",
+    ">   |-访问像素(元素)\n",
+    ">   |-修改像素(元素访问,块访问)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "8f5fbc7c-16f8-4800-9162-5f756a258f9f",
+   "metadata": {},
+   "source": [
+    "## 2.图像的像素\n",
+    "\n",
+    ">图像是矩阵(矩阵表达成一个数组,图像==矩阵,图像是三维矩阵(H,W,C))\n",
+    ">BMP:C=3, JPG:C=3, PNG:C=4(alpha通道), 灰度图像:C=1(结论:图像通道1,3,4:matplotlib把矩阵作为图像显示,只显示三维,第三维度必须是1,3,4)\n",
+    "\n",
+    ">图像的块操作\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 32,
+   "id": "d4884373-e046-4a0c-832c-1e9923c892ad",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import numpy as np              #数学运算:线性代数:向量运算,矩阵运算\n",
+    "import cv2                      #图像处理:调用numpy\n",
+    "import matplotlib.pyplot as plt #可视化:图像可视化,文本可视化,数据可视化(曲线,折线柱状图,饼图)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 41,
+   "id": "5c7ea9d2-edc6-49e3-a09c-fad2a08ae7d4",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "TypeError",
+     "evalue": "'NoneType' object does not support item assignment",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[41], line 4\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# %matplotlib inline\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# 加载图像\u001b[39;00m\n\u001b[0;32m      3\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 4\u001b[0m \u001b[43mimg\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m:\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m200\u001b[39;49m\u001b[43m:\u001b[49m\u001b[38;5;241;43m500\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m:\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m255\u001b[39m   \u001b[38;5;66;03m#对图像连续块进行访问:获取数据,修改数据\u001b[39;00m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# plt.imshow(img)\u001b[39;00m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# 把图像的红色通道像素全部设置为0\u001b[39;00m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m# img[要修改的像素][条件] = 值\u001b[39;00m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 注意:opencv的图像格式是:BGR\u001b[39;00m\n\u001b[0;32m      9\u001b[0m img[\u001b[38;5;241m200\u001b[39m:\u001b[38;5;241m500\u001b[39m, :, \u001b[38;5;241m1\u001b[39m:\u001b[38;5;241m3\u001b[39m][img[\u001b[38;5;241m200\u001b[39m:\u001b[38;5;241m500\u001b[39m, :, \u001b[38;5;241m1\u001b[39m:\u001b[38;5;241m3\u001b[39m] \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m255\u001b[39m  \u001b[38;5;66;03m# 0:red, 1:green, 2:blue\u001b[39;00m\n",
+      "\u001b[1;31mTypeError\u001b[0m: 'NoneType' object does not support item assignment"
+     ]
+    }
+   ],
+   "source": [
+    "# %matplotlib inline\n",
+    "# 加载图像\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img[0:100, 200:500, :] = 255   #对图像连续块进行访问:获取数据,修改数据\n",
+    "# plt.imshow(img)\n",
+    "# 把图像的红色通道像素全部设置为0\n",
+    "# img[要修改的像素][条件] = 值\n",
+    "# 注意:opencv的图像格式是:BGR\n",
+    "img[200:500, :, 1:3][img[200:500, :, 1:3] >= 0] = 255  # 0:red, 1:green, 2:blue\n",
+    "plt.imshow(img)\n",
+    "cv2.imwrite(\"2.jpg\",img)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "6057527b-29d4-411a-a6bc-fb9301837a8c",
+   "metadata": {},
+   "source": [
+    "## 3. 图像的操作\n",
+    "> 图像的操作==矩阵操作(转置,维度转换……)b"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 34,
+   "id": "e4a4a09c-7e27-4ba8-ba53-8ae0be53e05b",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "AttributeError",
+     "evalue": "'NoneType' object has no attribute 'T'",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[34], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# 读取图像\u001b[39;00m\n\u001b[0;32m      6\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m img_T \u001b[38;5;241m=\u001b[39m \u001b[43mimg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mT\u001b[49m    \u001b[38;5;66;03m#图像的转置\u001b[39;00m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(img_T\u001b[38;5;241m.\u001b[39mshape)   \u001b[38;5;66;03m#  (H, W, C)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28mprint\u001b[39m(img\u001b[38;5;241m.\u001b[39mshape)\n",
+      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'T'"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "# 读取图像\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img_T = img.T    #图像的转置\n",
+    "print(img_T.shape)   #  (H, W, C)\n",
+    "print(img.shape)\n",
+    "\n",
+    "# 图像的像素类型做转变:uint8 -> float\n",
+    "img_type = img.astype(np.float)\n",
+    "\n",
+    "plt.imshow(img_type)\n",
+    "\n",
+    "# 如果图像是整数,其值: 0——255,如果是浮点数, 其值: 0——1  (对小数越界,采用截断(clipping)的方式)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "fc4e944f-1e9c-403a-9888-400763560634",
+   "metadata": {},
+   "source": [
+    "## 4.颜色格式转换"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 40,
+   "id": "506bfa5d-45a3-4535-bdb5-a4e1679b178e",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[40], line 7\u001b[0m\n\u001b[0;32m      4\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# 格式转换\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m img_out \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# img_out = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m plt\u001b[38;5;241m.\u001b[39mimshow(img_out)\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "\n",
+    "# 格式转换\n",
+    "img_out = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "# img_out = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n",
+    "plt.imshow(img_out)\n",
+    "# plt.imshow(img_out,)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 36,
+   "id": "ec7c7d40-6754-4ea3-b9ef-b58570b9a2af",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[36], line 7\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[0;32m      5\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 绘制几何图形(边框,圆框,文本)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;66;03m# cv:rectangle 绘制方框\u001b[39;00m\n\u001b[0;32m     10\u001b[0m img_rect \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mrectangle(img,  \u001b[38;5;66;03m# 绘制的图像\u001b[39;00m\n\u001b[0;32m     11\u001b[0m                          (\u001b[38;5;241m100\u001b[39m, \u001b[38;5;241m100\u001b[39m),  \u001b[38;5;66;03m# 矩形左上角顶点\u001b[39;00m\n\u001b[0;32m     12\u001b[0m                          (\u001b[38;5;241m500\u001b[39m, \u001b[38;5;241m500\u001b[39m),  \u001b[38;5;66;03m# 矩形右下角顶点\u001b[39;00m\n\u001b[0;32m     13\u001b[0m                          (\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m255\u001b[39m, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m255\u001b[39m),  \u001b[38;5;66;03m# 方框的颜色\u001b[39;00m\n\u001b[0;32m     14\u001b[0m                          \u001b[38;5;241m5\u001b[39m  \u001b[38;5;66;03m# 线的粗细\u001b[39;00m\n\u001b[0;32m     15\u001b[0m )\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "\n",
+    "img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "# 绘制几何图形(边框,圆框,文本)\n",
+    "# cv:rectangle 绘制方框\n",
+    "img_rect = cv2.rectangle(img,  # 绘制的图像\n",
+    "                         (100, 100),  # 矩形左上角顶点\n",
+    "                         (500, 500),  # 矩形右下角顶点\n",
+    "                         (0, 255, 0, 255),  # 方框的颜色\n",
+    "                         5  # 线的粗细\n",
+    ")\n",
+    "# void cv::circle 绘制圆\n",
+    "img_circle = cv2.circle(img_rect, (500, 500), 100, (0, 0, 255), 20)\n",
+    "# cv::putText 绘制文本\n",
+    "img_text = cv2.putText(img_circle,  # 被绘制的图像\n",
+    "                       \"This is OpenCV\",  # 绘制的文本内容\n",
+    "                      (500, 500),  # 绘制的位置\n",
+    "                       cv2.FONT_HERSHEY_SIMPLEX,  # 字体\n",
+    "                       5.0,  # 字体放大倍数\n",
+    "                       (255, 0, 0),  #绘制颜色\n",
+    "                       20  # 字的粗细\n",
+    ")\n",
+    "plt.imshow(img_text)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 37,
+   "id": "c7e4c003-dd80-44c8-8b01-e1d7d65f6e43",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "SyntaxError",
+     "evalue": "invalid syntax (2487568721.py, line 1)",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;36m  Cell \u001b[1;32mIn[37], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    <matplotlib.image.AxesImage at 0x1a772d5a3d0>\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
+     ]
+    }
+   ],
+   "source": [
+    "<matplotlib.image.AxesImage at 0x1a772d5a3d0>"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "cc68b38e-4325-41bb-a298-6f9e2d5d750b",
+   "metadata": {},
+   "source": []
+  },
+  {
+   "cell_type": "markdown",
+   "id": "ae4f5cea-63dd-4a52-be6d-3f5ab052dc71",
+   "metadata": {},
+   "source": [
+    "## 6.利用矩阵分解压缩图像"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 38,
+   "id": "9b2b530d-82e6-4094-bb82-edad6554be1b",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[38], line 10\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 读取图像\u001b[39;00m\n\u001b[0;32m      9\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 10\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     11\u001b[0m new_img \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mndarray(img\u001b[38;5;241m.\u001b[39mshape)  \u001b[38;5;66;03m#生成一个空的图像,图像大小与加载的图像一样大小\u001b[39;00m\n\u001b[0;32m     12\u001b[0m \u001b[38;5;66;03m# 把图像当矩阵做奇异值分解\u001b[39;00m\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "n_dim = 200  #分解矩阵,只取特征值最大的5个合成新矩阵\n",
+    "\n",
+    "\n",
+    "# 读取图像\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "new_img = np.ndarray(img.shape)  #生成一个空的图像,图像大小与加载的图像一样大小\n",
+    "# 把图像当矩阵做奇异值分解\n",
+    "for i in range(3):\n",
+    "    # 每个通道都做奇异值分解(对二维矩阵,对其他维数不支持奇异值分解)\n",
+    "    img_tmp = img[:, :, i]\n",
+    "    # print(img_tmp.shape)\n",
+    "    # 分解\n",
+    "    u, s, v = np.linalg.svd(img_tmp, full_matrices=False)    \n",
+    "    # 利用奇异值,重新合成图像(只选择特征值大的酉阵,重新构建新的图像)\n",
+    "    u[:, n_dim:] = 0  # 左,把酉阵的前5列保留,其它的设置为0\n",
+    "    v[n_dim:, :] = 0  # 右\n",
+    "    s[n_dim:]=0\n",
+    "    \n",
+    "    ds = np.diag(s)\n",
+    "    img_tmp = np.dot(np.dot(u, ds), v) #u.s.v\n",
+    "    new_img[:, :, i] = img_tmp\n",
+    "    \n",
+    "# 显示新的图像\n",
+    "print(new_img.shape)\n",
+    "new_img = new_img.astype(np.uint8)\n",
+    "plt.imshow(new_img)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "b86fc448-bf4e-4eb3-9499-456286d9f9fb",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "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.9.13"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}

+ 101 - 0
.ipynb_checkpoints/卷积特征-checkpoint.ipynb

@@ -0,0 +1,101 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "46148d7d-fcfc-4778-a61e-d40d2023d976",
+   "metadata": {},
+   "source": [
+    "## 1.关于图像特征\n",
+    "\n",
+    "> 1.统计特征\n",
+    "> 2.数字特征(奇异值分解)\n",
+    "> 3.卷积特征\n",
+    "  \n",
+    "    \n",
+    "----\n",
+    "\n",
+    ">像素的差异:差异特征,微分(相邻的两个像素的差异)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "db07b6d1-82b3-476d-9693-d28cf1f71314",
+   "metadata": {},
+   "source": [
+    "## 2.特征运算"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "id": "37ea830a-e430-4955-aaa3-8a9f49f4fe8f",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[2], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# 读取图像:大矩阵\u001b[39;00m\n\u001b[0;32m      6\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 准备一个小矩阵==Kenel(算子:Sobel算子,Laplace算子)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \n\u001b[0;32m     10\u001b[0m \u001b[38;5;66;03m# 进行卷积运算()\u001b[39;00m\n\u001b[0;32m     11\u001b[0m img_sobel \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mSobel(img,  \u001b[38;5;66;03m# 被运算的图像\u001b[39;00m\n\u001b[0;32m     12\u001b[0m                       \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m,  \u001b[38;5;66;03m# -1表示输出图像的维数与输入的图像的维度保持一致\u001b[39;00m\n\u001b[0;32m     13\u001b[0m                       \u001b[38;5;241m2\u001b[39m,  \u001b[38;5;66;03m# 1表示x方向1阶偏微分\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     17\u001b[0m                       delta\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m50.0\u001b[39m  \u001b[38;5;66;03m# 加上一个相值:1.0*输出矩阵+100.0\u001b[39;00m\n\u001b[0;32m     18\u001b[0m                      )\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "# 读取图像:大矩阵\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "# 准备一个小矩阵==Kenel(算子:Sobel算子,Laplace算子)\n",
+    "\n",
+    "# 进行卷积运算()\n",
+    "img_sobel = cv2.Sobel(img,  # 被运算的图像\n",
+    "                      -1,  # -1表示输出图像的维数与输入的图像的维度保持一致\n",
+    "                      2,  # 1表示x方向1阶偏微分\n",
+    "                      0,  # 0表示y方向1阶偏微分\n",
+    "                      ksize=3,\n",
+    "                      scale=2.3,  # 输出矩阵做数乘运算1.0*矩阵\n",
+    "                      delta=50.0  # 加上一个相值:1.0*输出矩阵+100.0\n",
+    "                     )\n",
+    "# 显示运算以后的图像(体验什么是卷积特征;通过这个特征,能识别对象,把特征的提取变成自动学习)\n",
+    "plt.imshow(img_sobel)\n",
+    "# 特征的数学表示(小矩阵==算子)0\n",
+    "# 学习的目标找到一个小矩阵(算子),是抽取图像特征有卒于目标分类,识别,分割,跟踪"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "8a02eebd-d4bc-49a4-bc50-94d063a79fbb",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "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.9.13"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}

+ 13 - 0
day3/code/ex_qto1.py

@@ -0,0 +1,13 @@
+#引入模块
+from PyQt5.QtWidgets import QApplication
+from PyQt5.QtWidgets import QDialog
+
+#创建Qt应用
+app = QApplication([]) # 参考:命令行参数
+
+#Qt的应用必须在app之间
+
+dlg = QDialog()
+dlg.show()
+
+app.exec()  #  让应用程序进入消息循环

+ 11 - 0
day3/code/notes.txt

@@ -0,0 +1,11 @@
+1.条件
+    安装条件:PyQt5,PyQt5-tools
+    where python 确定Python安装路径
+    安装的模块在
+        ${PYTHON_HOME}/Lib/site-package/PyQt5
+        ${PYTHON_HOME}/Lib/site-package/PyQt5-tools
+        ${PYTHON_HOME}/Lib/site-package/PyQt5-applications
+        ${PYTHON_HOME}/Lib/site-package/PyQt5-applications/Qt/bin
+            |- designer.exe GUI(Graphic User Interface)
+            |- uic.exe 把我们设计的GUI转换为Python代码
+            |- Scripts\py

+ 321 - 0
day3/day3.ipynb

@@ -0,0 +1,321 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "62916051-266e-439b-9f14-5a16a9017622",
+   "metadata": {},
+   "source": [
+    "## 1. 回顾\n",
+    "\n",
+    ">1. Git上传\n",
+    ">2. 图像的处理\n",
+    ">   |-访问像素(元素)\n",
+    ">   |-修改像素(元素访问,块访问)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "8f5fbc7c-16f8-4800-9162-5f756a258f9f",
+   "metadata": {},
+   "source": [
+    "## 2.图像的像素\n",
+    "\n",
+    ">图像是矩阵(矩阵表达成一个数组,图像==矩阵,图像是三维矩阵(H,W,C))\n",
+    ">BMP:C=3, JPG:C=3, PNG:C=4(alpha通道), 灰度图像:C=1(结论:图像通道1,3,4:matplotlib把矩阵作为图像显示,只显示三维,第三维度必须是1,3,4)\n",
+    "\n",
+    ">图像的块操作\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 32,
+   "id": "d4884373-e046-4a0c-832c-1e9923c892ad",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import numpy as np              #数学运算:线性代数:向量运算,矩阵运算\n",
+    "import cv2                      #图像处理:调用numpy\n",
+    "import matplotlib.pyplot as plt #可视化:图像可视化,文本可视化,数据可视化(曲线,折线柱状图,饼图)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 41,
+   "id": "5c7ea9d2-edc6-49e3-a09c-fad2a08ae7d4",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "TypeError",
+     "evalue": "'NoneType' object does not support item assignment",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[41], line 4\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# %matplotlib inline\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# 加载图像\u001b[39;00m\n\u001b[0;32m      3\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 4\u001b[0m \u001b[43mimg\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m:\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m200\u001b[39;49m\u001b[43m:\u001b[49m\u001b[38;5;241;43m500\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m:\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m255\u001b[39m   \u001b[38;5;66;03m#对图像连续块进行访问:获取数据,修改数据\u001b[39;00m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# plt.imshow(img)\u001b[39;00m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# 把图像的红色通道像素全部设置为0\u001b[39;00m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m# img[要修改的像素][条件] = 值\u001b[39;00m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 注意:opencv的图像格式是:BGR\u001b[39;00m\n\u001b[0;32m      9\u001b[0m img[\u001b[38;5;241m200\u001b[39m:\u001b[38;5;241m500\u001b[39m, :, \u001b[38;5;241m1\u001b[39m:\u001b[38;5;241m3\u001b[39m][img[\u001b[38;5;241m200\u001b[39m:\u001b[38;5;241m500\u001b[39m, :, \u001b[38;5;241m1\u001b[39m:\u001b[38;5;241m3\u001b[39m] \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m255\u001b[39m  \u001b[38;5;66;03m# 0:red, 1:green, 2:blue\u001b[39;00m\n",
+      "\u001b[1;31mTypeError\u001b[0m: 'NoneType' object does not support item assignment"
+     ]
+    }
+   ],
+   "source": [
+    "# %matplotlib inline\n",
+    "# 加载图像\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img[0:100, 200:500, :] = 255   #对图像连续块进行访问:获取数据,修改数据\n",
+    "# plt.imshow(img)\n",
+    "# 把图像的红色通道像素全部设置为0\n",
+    "# img[要修改的像素][条件] = 值\n",
+    "# 注意:opencv的图像格式是:BGR\n",
+    "img[200:500, :, 1:3][img[200:500, :, 1:3] >= 0] = 255  # 0:red, 1:green, 2:blue\n",
+    "plt.imshow(img)\n",
+    "cv2.imwrite(\"2.jpg\",img)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "6057527b-29d4-411a-a6bc-fb9301837a8c",
+   "metadata": {},
+   "source": [
+    "## 3. 图像的操作\n",
+    "> 图像的操作==矩阵操作(转置,维度转换……)b"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 34,
+   "id": "e4a4a09c-7e27-4ba8-ba53-8ae0be53e05b",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "AttributeError",
+     "evalue": "'NoneType' object has no attribute 'T'",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[34], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# 读取图像\u001b[39;00m\n\u001b[0;32m      6\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m img_T \u001b[38;5;241m=\u001b[39m \u001b[43mimg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mT\u001b[49m    \u001b[38;5;66;03m#图像的转置\u001b[39;00m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(img_T\u001b[38;5;241m.\u001b[39mshape)   \u001b[38;5;66;03m#  (H, W, C)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28mprint\u001b[39m(img\u001b[38;5;241m.\u001b[39mshape)\n",
+      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'T'"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "# 读取图像\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img_T = img.T    #图像的转置\n",
+    "print(img_T.shape)   #  (H, W, C)\n",
+    "print(img.shape)\n",
+    "\n",
+    "# 图像的像素类型做转变:uint8 -> float\n",
+    "img_type = img.astype(np.float)\n",
+    "\n",
+    "plt.imshow(img_type)\n",
+    "\n",
+    "# 如果图像是整数,其值: 0——255,如果是浮点数, 其值: 0——1  (对小数越界,采用截断(clipping)的方式)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "fc4e944f-1e9c-403a-9888-400763560634",
+   "metadata": {},
+   "source": [
+    "## 4.颜色格式转换"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 40,
+   "id": "506bfa5d-45a3-4535-bdb5-a4e1679b178e",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[40], line 7\u001b[0m\n\u001b[0;32m      4\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# 格式转换\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m img_out \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# img_out = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m plt\u001b[38;5;241m.\u001b[39mimshow(img_out)\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "\n",
+    "# 格式转换\n",
+    "img_out = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "# img_out = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n",
+    "plt.imshow(img_out)\n",
+    "# plt.imshow(img_out,)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 36,
+   "id": "ec7c7d40-6754-4ea3-b9ef-b58570b9a2af",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[36], line 7\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[0;32m      5\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 绘制几何图形(边框,圆框,文本)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;66;03m# cv:rectangle 绘制方框\u001b[39;00m\n\u001b[0;32m     10\u001b[0m img_rect \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mrectangle(img,  \u001b[38;5;66;03m# 绘制的图像\u001b[39;00m\n\u001b[0;32m     11\u001b[0m                          (\u001b[38;5;241m100\u001b[39m, \u001b[38;5;241m100\u001b[39m),  \u001b[38;5;66;03m# 矩形左上角顶点\u001b[39;00m\n\u001b[0;32m     12\u001b[0m                          (\u001b[38;5;241m500\u001b[39m, \u001b[38;5;241m500\u001b[39m),  \u001b[38;5;66;03m# 矩形右下角顶点\u001b[39;00m\n\u001b[0;32m     13\u001b[0m                          (\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m255\u001b[39m, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m255\u001b[39m),  \u001b[38;5;66;03m# 方框的颜色\u001b[39;00m\n\u001b[0;32m     14\u001b[0m                          \u001b[38;5;241m5\u001b[39m  \u001b[38;5;66;03m# 线的粗细\u001b[39;00m\n\u001b[0;32m     15\u001b[0m )\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "\n",
+    "img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "# 绘制几何图形(边框,圆框,文本)\n",
+    "# cv:rectangle 绘制方框\n",
+    "img_rect = cv2.rectangle(img,  # 绘制的图像\n",
+    "                         (100, 100),  # 矩形左上角顶点\n",
+    "                         (500, 500),  # 矩形右下角顶点\n",
+    "                         (0, 255, 0, 255),  # 方框的颜色\n",
+    "                         5  # 线的粗细\n",
+    ")\n",
+    "# void cv::circle 绘制圆\n",
+    "img_circle = cv2.circle(img_rect, (500, 500), 100, (0, 0, 255), 20)\n",
+    "# cv::putText 绘制文本\n",
+    "img_text = cv2.putText(img_circle,  # 被绘制的图像\n",
+    "                       \"This is OpenCV\",  # 绘制的文本内容\n",
+    "                      (500, 500),  # 绘制的位置\n",
+    "                       cv2.FONT_HERSHEY_SIMPLEX,  # 字体\n",
+    "                       5.0,  # 字体放大倍数\n",
+    "                       (255, 0, 0),  #绘制颜色\n",
+    "                       20  # 字的粗细\n",
+    ")\n",
+    "plt.imshow(img_text)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 37,
+   "id": "c7e4c003-dd80-44c8-8b01-e1d7d65f6e43",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "SyntaxError",
+     "evalue": "invalid syntax (2487568721.py, line 1)",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;36m  Cell \u001b[1;32mIn[37], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    <matplotlib.image.AxesImage at 0x1a772d5a3d0>\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
+     ]
+    }
+   ],
+   "source": [
+    "<matplotlib.image.AxesImage at 0x1a772d5a3d0>"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "cc68b38e-4325-41bb-a298-6f9e2d5d750b",
+   "metadata": {},
+   "source": []
+  },
+  {
+   "cell_type": "markdown",
+   "id": "ae4f5cea-63dd-4a52-be6d-3f5ab052dc71",
+   "metadata": {},
+   "source": [
+    "## 6.利用矩阵分解压缩图像"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 38,
+   "id": "9b2b530d-82e6-4094-bb82-edad6554be1b",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[38], line 10\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 读取图像\u001b[39;00m\n\u001b[0;32m      9\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 10\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     11\u001b[0m new_img \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mndarray(img\u001b[38;5;241m.\u001b[39mshape)  \u001b[38;5;66;03m#生成一个空的图像,图像大小与加载的图像一样大小\u001b[39;00m\n\u001b[0;32m     12\u001b[0m \u001b[38;5;66;03m# 把图像当矩阵做奇异值分解\u001b[39;00m\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "n_dim = 200  #分解矩阵,只取特征值最大的5个合成新矩阵\n",
+    "\n",
+    "\n",
+    "# 读取图像\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "new_img = np.ndarray(img.shape)  #生成一个空的图像,图像大小与加载的图像一样大小\n",
+    "# 把图像当矩阵做奇异值分解\n",
+    "for i in range(3):\n",
+    "    # 每个通道都做奇异值分解(对二维矩阵,对其他维数不支持奇异值分解)\n",
+    "    img_tmp = img[:, :, i]\n",
+    "    # print(img_tmp.shape)\n",
+    "    # 分解\n",
+    "    u, s, v = np.linalg.svd(img_tmp, full_matrices=False)    \n",
+    "    # 利用奇异值,重新合成图像(只选择特征值大的酉阵,重新构建新的图像)\n",
+    "    u[:, n_dim:] = 0  # 左,把酉阵的前5列保留,其它的设置为0\n",
+    "    v[n_dim:, :] = 0  # 右\n",
+    "    s[n_dim:]=0\n",
+    "    \n",
+    "    ds = np.diag(s)\n",
+    "    img_tmp = np.dot(np.dot(u, ds), v) #u.s.v\n",
+    "    new_img[:, :, i] = img_tmp\n",
+    "    \n",
+    "# 显示新的图像\n",
+    "print(new_img.shape)\n",
+    "new_img = new_img.astype(np.uint8)\n",
+    "plt.imshow(new_img)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "b86fc448-bf4e-4eb3-9499-456286d9f9fb",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "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.9.13"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}

binární
day3/gpu.bmp


+ 101 - 0
day3/卷积特征.ipynb

@@ -0,0 +1,101 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "46148d7d-fcfc-4778-a61e-d40d2023d976",
+   "metadata": {},
+   "source": [
+    "## 1.关于图像特征\n",
+    "\n",
+    "> 1.统计特征\n",
+    "> 2.数字特征(奇异值分解)\n",
+    "> 3.卷积特征\n",
+    "  \n",
+    "    \n",
+    "----\n",
+    "\n",
+    ">像素的差异:差异特征,微分(相邻的两个像素的差异)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "db07b6d1-82b3-476d-9693-d28cf1f71314",
+   "metadata": {},
+   "source": [
+    "## 2.特征运算"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "id": "37ea830a-e430-4955-aaa3-8a9f49f4fe8f",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "error",
+     "evalue": "OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
+      "Cell \u001b[1;32mIn[2], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# 读取图像:大矩阵\u001b[39;00m\n\u001b[0;32m      6\u001b[0m img \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgpu.bmp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_RGB2BGR\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 准备一个小矩阵==Kenel(算子:Sobel算子,Laplace算子)\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \n\u001b[0;32m     10\u001b[0m \u001b[38;5;66;03m# 进行卷积运算()\u001b[39;00m\n\u001b[0;32m     11\u001b[0m img_sobel \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mSobel(img,  \u001b[38;5;66;03m# 被运算的图像\u001b[39;00m\n\u001b[0;32m     12\u001b[0m                       \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m,  \u001b[38;5;66;03m# -1表示输出图像的维数与输入的图像的维度保持一致\u001b[39;00m\n\u001b[0;32m     13\u001b[0m                       \u001b[38;5;241m2\u001b[39m,  \u001b[38;5;66;03m# 1表示x方向1阶偏微分\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     17\u001b[0m                       delta\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m50.0\u001b[39m  \u001b[38;5;66;03m# 加上一个相值:1.0*输出矩阵+100.0\u001b[39;00m\n\u001b[0;32m     18\u001b[0m                      )\n",
+      "\u001b[1;31merror\u001b[0m: OpenCV(4.7.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'\n"
+     ]
+    }
+   ],
+   "source": [
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "# 读取图像:大矩阵\n",
+    "img = cv2.imread(\"gpu.bmp\")\n",
+    "img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
+    "# 准备一个小矩阵==Kenel(算子:Sobel算子,Laplace算子)\n",
+    "\n",
+    "# 进行卷积运算()\n",
+    "img_sobel = cv2.Sobel(img,  # 被运算的图像\n",
+    "                      -1,  # -1表示输出图像的维数与输入的图像的维度保持一致\n",
+    "                      2,  # 1表示x方向1阶偏微分\n",
+    "                      0,  # 0表示y方向1阶偏微分\n",
+    "                      ksize=3,\n",
+    "                      scale=2.3,  # 输出矩阵做数乘运算1.0*矩阵\n",
+    "                      delta=50.0  # 加上一个相值:1.0*输出矩阵+100.0\n",
+    "                     )\n",
+    "# 显示运算以后的图像(体验什么是卷积特征;通过这个特征,能识别对象,把特征的提取变成自动学习)\n",
+    "plt.imshow(img_sobel)\n",
+    "# 特征的数学表示(小矩阵==算子)0\n",
+    "# 学习的目标找到一个小矩阵(算子),是抽取图像特征有卒于目标分类,识别,分割,跟踪"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "8a02eebd-d4bc-49a4-bc50-94d063a79fbb",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "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.9.13"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}

+ 65 - 0
day4/monitor/ui/monitor.ui

@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Dialog</class>
+ <widget class="QDialog" name="Dialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>300</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>智能交通监控系统</string>
+  </property>
+  <widget class="QLabel" name="label">
+   <property name="geometry">
+    <rect>
+     <x>40</x>
+     <y>70</y>
+     <width>221</width>
+     <height>121</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:3px;
+border-style:solid;
+border-radius:10px;
+border-top-color:red;
+border-bottom-color:green;
+border-left-color:yellow;
+border-right-color:pink;
+</string>
+   </property>
+   <property name="text">
+    <string>视频显示区</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButton">
+   <property name="geometry">
+    <rect>
+     <x>210</x>
+     <y>220</y>
+     <width>93</width>
+     <height>28</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:2px;
+border-style:solid;
+border-randius:10px;
+border-top-color:#ffffff;
+border-left-color:#ffffff;
+border-bottom-color:#888888;
+border-right-color:#888888;
+</string>
+   </property>
+   <property name="text">
+    <string>处理视频</string>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 48 - 0
day4/monitor/ui/monitor_ui.py

@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+
+# Form implementation generated from reading ui file 'monitor.ui'
+#
+# Created by: PyQt5 UI code generator 5.15.9
+#
+# WARNING: Any manual changes made to this file will be lost when pyuic5 is
+# run again.  Do not edit this file unless you know what you are doing.
+
+
+from PyQt5 import QtCore, QtGui, QtWidgets
+
+
+class Ui_Dialog(object):
+    def setupUi(self, Dialog):
+        Dialog.setObjectName("Dialog")
+        Dialog.resize(400, 300)
+        self.label = QtWidgets.QLabel(Dialog)
+        self.label.setGeometry(QtCore.QRect(40, 70, 221, 121))
+        self.label.setStyleSheet("border-width:3px;\n"
+"border-style:solid;\n"
+"border-radius:10px;\n"
+"border-top-color:red;\n"
+"border-bottom-color:green;\n"
+"border-left-color:yellow;\n"
+"border-right-color:pink;\n"
+"")
+        self.label.setObjectName("label")
+        self.pushButton = QtWidgets.QPushButton(Dialog)
+        self.pushButton.setGeometry(QtCore.QRect(210, 220, 93, 28))
+        self.pushButton.setStyleSheet("border-width:2px;\n"
+"border-style:solid;\n"
+"border-randius:10px;\n"
+"border-top-color:#ffffff;\n"
+"border-left-color:#ffffff;\n"
+"border-bottom-color:#888888;\n"
+"border-right-color:#888888;\n"
+"")
+        self.pushButton.setObjectName("pushButton")
+
+        self.retranslateUi(Dialog)
+        QtCore.QMetaObject.connectSlotsByName(Dialog)
+
+    def retranslateUi(self, Dialog):
+        _translate = QtCore.QCoreApplication.translate
+        Dialog.setWindowTitle(_translate("Dialog", "智能交通监控系统"))
+        self.label.setText(_translate("Dialog", "视频显示区"))
+        self.pushButton.setText(_translate("Dialog", "处理视频"))