{ "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 \u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "" ] }, { "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 }