{"id":1067783,"date":"2024-12-31T16:37:36","date_gmt":"2024-12-31T08:37:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1067783.html"},"modified":"2024-12-31T16:37:39","modified_gmt":"2024-12-31T08:37:39","slug":"python-%e5%a6%82%e4%bd%95%e5%b0%86%e7%9f%a9%e9%98%b5%e8%bd%ac%e6%8d%a2%e6%88%90%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1067783.html","title":{"rendered":"python \u5982\u4f55\u5c06\u77e9\u9635\u8f6c\u6362\u6210\u56fe\u50cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/fd3680ba-4da6-4fcf-8b08-d3703abfcd99.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python \u5982\u4f55\u5c06\u77e9\u9635\u8f6c\u6362\u6210\u56fe\u50cf\" \/><\/p>\n<p><p> <strong>Python \u5c06\u77e9\u9635\u8f6c\u6362\u6210\u56fe\u50cf\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001Matplotlib\u5e93\u3001OpenCV\u5e93\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u4f7f\u7528PIL\u5e93\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u89e3\u91ca\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIL\u5e93<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684\u529f\u80fd\uff0c\u5305\u62ec\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PIL\u5e93\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5PIL\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86PIL\u5e93\u3002PIL\u5e93\u73b0\u5728\u88ab\u79f0\u4e3aPillow\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pillow\u7684<code>Image.fromarray<\/code>\u65b9\u6cd5\u5c06NumPy\u6570\u7ec4\uff08\u77e9\u9635\uff09\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[0, 255, 0], [255, 0, 255], [0, 255, 0]], dtype=np.uint8)<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.fromarray(matrix)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&quot;matrix_image.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002\u7136\u540e\uff0c\u6211\u4eec\u663e\u793a\u5e76\u4fdd\u5b58\u8be5\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u7684\u7c7b\u578b\u548c\u503c\u8303\u56f4\u4f1a\u5f71\u54cd\u56fe\u50cf\u7684\u663e\u793a\u3002\u4f8b\u5982\uff0c\u5982\u679c\u77e9\u9635\u5305\u542b\u6d6e\u70b9\u6570\u6216\u8d1f\u6570\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u6d6e\u70b9\u6570\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6d6e\u70b9\u6570\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]], dtype=np.float32)<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u77e9\u9635\u8f6c\u6362\u4e3a8\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u77e9\u9635<\/strong><\/h2>\n<p>matrix = (matrix * 255).astype(np.uint8)<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.fromarray(matrix)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&quot;float_matrix_image.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u6d6e\u70b9\u6570\u77e9\u9635\uff0c\u5e76\u5c06\u5176\u7f29\u653e\u52300\u5230255\u7684\u8303\u56f4\u5185\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u5904\u7406\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u77e9\u9635\u8868\u793a\u5f69\u8272\u56fe\u50cf\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u77e9\u9635\u7684\u5f62\u72b6\u4e3a\uff08height, width, 3\uff09\uff0c\u5176\u4e2d\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u8868\u793aRGB\u989c\u8272\u901a\u9053\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5f69\u8272\u56fe\u50cf\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],<\/p>\n<p>                   [[0, 255, 0], [0, 0, 255], [255, 0, 0]],<\/p>\n<p>                   [[0, 0, 255], [255, 0, 0], [0, 255, 0]]], dtype=np.uint8)<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.fromarray(matrix)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&quot;color_matrix_image.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u5f69\u8272\u56fe\u50cf\u77e9\u9635\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Matplotlib\u5e93<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u5e93\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>imshow<\/code>\u65b9\u6cd5\u5c06\u77e9\u9635\u663e\u793a\u4e3a\u56fe\u50cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[0, 255, 0], [255, 0, 255], [0, 255, 0]], dtype=np.uint8)<\/p>\n<h2><strong>\u663e\u793a\u77e9\u9635\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(matrix, cmap=&#39;gray&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imsave(&quot;matrix_image_matplotlib.png&quot;, matrix, cmap=&#39;gray&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u5e76\u4f7f\u7528Matplotlib\u5c06\u5176\u663e\u793a\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u4e0ePIL\u5e93\u7c7b\u4f3c\uff0c\u5982\u679c\u77e9\u9635\u5305\u542b\u6d6e\u70b9\u6570\u6216\u8d1f\u6570\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u6d6e\u70b9\u6570\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6d6e\u70b9\u6570\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]], dtype=np.float32)<\/p>\n<h2><strong>\u663e\u793a\u6d6e\u70b9\u6570\u77e9\u9635\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(matrix, cmap=&#39;gray&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imsave(&quot;float_matrix_image_matplotlib.png&quot;, matrix, cmap=&#39;gray&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u6d6e\u70b9\u6570\u77e9\u9635\uff0c\u5e76\u4f7f\u7528Matplotlib\u5c06\u5176\u663e\u793a\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u5904\u7406\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u77e9\u9635\u8868\u793a\u5f69\u8272\u56fe\u50cf\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u77e9\u9635\u7684\u5f62\u72b6\u4e3a\uff08height, width, 3\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5f69\u8272\u56fe\u50cf\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],<\/p>\n<p>                   [[0, 255, 0], [0, 0, 255], [255, 0, 0]],<\/p>\n<p>                   [[0, 0, 255], [255, 0, 0], [0, 255, 0]]], dtype=np.uint8)<\/p>\n<h2><strong>\u663e\u793a\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/strong><\/h2>\n<p>plt.imshow(matrix)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imsave(&quot;color_matrix_image_matplotlib.png&quot;, matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u5f69\u8272\u56fe\u50cf\u77e9\u9635\uff0c\u5e76\u4f7f\u7528Matplotlib\u5c06\u5176\u663e\u793a\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528OpenCV\u5e93<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528OpenCV\u5e93\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684<code>imshow<\/code>\u65b9\u6cd5\u5c06\u77e9\u9635\u663e\u793a\u4e3a\u56fe\u50cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[0, 255, 0], [255, 0, 255], [0, 255, 0]], dtype=np.uint8)<\/p>\n<h2><strong>\u663e\u793a\u77e9\u9635\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Matrix Image&#39;, matrix)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&quot;matrix_image_opencv.png&quot;, matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u5e76\u4f7f\u7528OpenCV\u5c06\u5176\u663e\u793a\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u4e0ePIL\u5e93\u548cMatplotlib\u5e93\u7c7b\u4f3c\uff0c\u5982\u679c\u77e9\u9635\u5305\u542b\u6d6e\u70b9\u6570\u6216\u8d1f\u6570\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u6d6e\u70b9\u6570\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6d6e\u70b9\u6570\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]], dtype=np.float32)<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u77e9\u9635\u8f6c\u6362\u4e3a8\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u77e9\u9635<\/strong><\/h2>\n<p>matrix = (matrix * 255).astype(np.uint8)<\/p>\n<h2><strong>\u663e\u793a\u77e9\u9635\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Float Matrix Image&#39;, matrix)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&quot;float_matrix_image_opencv.png&quot;, matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u6d6e\u70b9\u6570\u77e9\u9635\uff0c\u5e76\u5c06\u5176\u7f29\u653e\u52300\u5230255\u7684\u8303\u56f4\u5185\uff0c\u7136\u540e\u4f7f\u7528OpenCV\u5c06\u5176\u663e\u793a\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u5904\u7406\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u77e9\u9635\u8868\u793a\u5f69\u8272\u56fe\u50cf\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u77e9\u9635\u7684\u5f62\u72b6\u4e3a\uff08height, width, 3\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5f69\u8272\u56fe\u50cf\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],<\/p>\n<p>                   [[0, 255, 0], [0, 0, 255], [255, 0, 0]],<\/p>\n<p>                   [[0, 0, 255], [255, 0, 0], [0, 255, 0]]], dtype=np.uint8)<\/p>\n<h2><strong>\u663e\u793a\u5f69\u8272\u56fe\u50cf\u77e9\u9635<\/strong><\/h2>\n<p>cv2.imshow(&#39;Color Matrix Image&#39;, matrix)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&quot;color_matrix_image_opencv.png&quot;, matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u5f69\u8272\u56fe\u50cf\u77e9\u9635\uff0c\u5e76\u4f7f\u7528OpenCV\u5c06\u5176\u663e\u793a\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001Matplotlib\u5e93\u548cOpenCV\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u9700\u6c42\u548c\u504f\u597d\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u8be6\u7ec6\u89e3\u91ca\u80fd\u5e2e\u52a9\u60a8\u987a\u5229\u5730\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u4e2a\u5e93\u6765\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u683c\u5f0f\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5e93\u5305\u62ecNumPy\u548cPIL\uff08Python Imaging Library\uff09\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5c06\u77e9\u9635\u6570\u636e\u8f6c\u6362\u4e3a\u9002\u5408\u56fe\u50cf\u8868\u793a\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u5c06\u5176\u5f52\u4e00\u5316\u52300\u5230255\u7684\u8303\u56f4\u5185\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u4e2d\u7684Image\u6a21\u5757\u5c06\u5904\u7406\u540e\u7684\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u50cf\u6587\u4ef6\uff0c\u5982JPEG\u6216PNG\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u77e9\u9635\u4e3a\u56fe\u50cf\u65f6\uff0c\u5982\u4f55\u5904\u7406\u989c\u8272\u901a\u9053\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5f69\u8272\u56fe\u50cf\u65f6\uff0c\u77e9\u9635\u901a\u5e38\u5305\u542b\u591a\u4e2a\u989c\u8272\u901a\u9053\uff08\u5982RGB\uff09\u3002\u60a8\u53ef\u4ee5\u5c06\u4e00\u4e2a3D\u77e9\u9635\u89c6\u4e3a\u4e00\u4e2a\u5f69\u8272\u56fe\u50cf\uff0c\u5176\u4e2d\u6bcf\u4e2a\u7ef4\u5ea6\u4ee3\u8868\u4e00\u4e2a\u989c\u8272\u901a\u9053\u3002\u786e\u4fdd\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u6b63\u786e\u6392\u5217\u77e9\u9635\u7684\u7ef4\u5ea6\uff0c\u4ee5\u4fbf\u6bcf\u4e2a\u989c\u8272\u901a\u9053\u5bf9\u5e94\u6b63\u786e\u7684\u56fe\u50cf\u989c\u8272\u3002\u4f7f\u7528NumPy\u7684reshape\u529f\u80fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u6574\u77e9\u9635\u7684\u5f62\u72b6\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u6765\u5b9e\u73b0\u77e9\u9635\u5230\u56fe\u50cf\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u9664\u4e86NumPy\u548cPIL\uff0cOpenCV\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u80fd\u591f\u5b9e\u73b0\u77e9\u9635\u5230\u56fe\u50cf\u7684\u8f6c\u6362\u3002OpenCV\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u56fe\u50cf\u5904\u7406\u6b65\u9aa4\uff0c\u4f8b\u5982\u5e73\u6ed1\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\u3002\u8fd9\u4f7f\u5f97\u5b83\u5728\u9700\u8981\u590d\u6742\u56fe\u50cf\u64cd\u4f5c\u65f6\u6210\u4e3a\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002\u5728\u4f7f\u7528OpenCV\u65f6\uff0c\u8bf7\u786e\u4fdd\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\uff0c\u5e76\u4e86\u89e3\u5176\u56fe\u50cf\u8868\u793a\u7684\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5c06\u77e9\u9635\u8f6c\u6362\u6210\u56fe\u50cf\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001Matplotlib\u5e93\u3001OpenCV\u5e93\u3002 \u5728\u672c\u6587\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1067796,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067783"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1067783"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067783\/revisions"}],"predecessor-version":[{"id":1067798,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067783\/revisions\/1067798"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1067796"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1067783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1067783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1067783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}