{"id":1064738,"date":"2024-12-31T16:11:42","date_gmt":"2024-12-31T08:11:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064738.html"},"modified":"2024-12-31T16:11:44","modified_gmt":"2024-12-31T08:11:44","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%9b%be%e7%89%87%e9%a2%9c%e8%89%b2%e5%8e%bb%e6%8e%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064738.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u56fe\u7247\u989c\u8272\u53bb\u6389"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d2542c3b-4864-4868-82f9-74c5ea86141e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u56fe\u7247\u989c\u8272\u53bb\u6389\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u6765\u53bb\u6389\u989c\u8272\uff0c\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528OpenCV\u5e93\u7684\u65b9\u6cd5\u6700\u4e3a\u5e38\u7528\u548c\u9ad8\u6548\u3002OpenCV\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002\u6b64\u5916\uff0cPIL\u5e93\u548cMatplotlib\u5e93\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u9002\u5408\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u4e4b\u524d\uff0c\u5148\u6765\u4e86\u89e3\u4e00\u4e0b\u7070\u5ea6\u56fe\u50cf\u7684\u6982\u5ff5\u3002\u7070\u5ea6\u56fe\u50cf\u662f\u6307\u56fe\u50cf\u4e2d\u6bcf\u4e2a\u50cf\u7d20\u70b9\u53ea\u6709\u4e00\u4e2a\u7070\u5ea6\u503c\uff0c\u4e0d\u5305\u542b\u989c\u8272\u4fe1\u606f\u3002\u7070\u5ea6\u503c\u901a\u5e38\u75280\u5230255\u4e4b\u95f4\u7684\u6574\u6570\u8868\u793a\uff0c\u5176\u4e2d0\u8868\u793a\u9ed1\u8272\uff0c255\u8868\u793a\u767d\u8272\uff0c\u4e2d\u95f4\u7684\u503c\u8868\u793a\u4e0d\u540c\u7a0b\u5ea6\u7684\u7070\u8272\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528OpenCV\u5e93\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8f6f\u4ef6\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u51fd\u6570\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5305\u62ecPython\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5OpenCV\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\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>\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528OpenCV\u5e93\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u539f\u59cb\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Original Image&#39;, image)<\/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<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>cv2.cvtColor<\/code>\u51fd\u6570\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u663e\u793a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Gray Image&#39;, gray_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fdd\u5b58\u7070\u5ea6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>cv2.imwrite<\/code>\u51fd\u6570\u5c06\u7070\u5ea6\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u4fdd\u5b58\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;path\/to\/save\/gray_image.jpg&#39;, gray_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PIL\u5e93\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\u3002Pillow\u662fPIL\u7684\u4e00\u4e2a\u53cb\u597d\u7684\u5206\u652f\uff0c\u5efa\u8bae\u4f7f\u7528Pillow\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pillow\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Pillow\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Pillow\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\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>\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528Pillow\u5e93\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u539f\u59cb\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>convert<\/code>\u51fd\u6570\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = image.convert(&#39;L&#39;)<\/p>\n<h2><strong>\u663e\u793a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fdd\u5b58\u7070\u5ea6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>save<\/code>\u51fd\u6570\u5c06\u7070\u5ea6\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = image.convert(&#39;L&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image.save(&#39;path\/to\/save\/gray_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Matplotlib\u5e93\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684Python\u5e93\uff0c\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528Matplotlib\u5e93\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u539f\u59cb\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.imshow<\/code>\u51fd\u6570\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = np.dot(image[...,:3], [0.299, 0.587, 0.114])<\/p>\n<h2><strong>\u663e\u793a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(gray_image, cmap=plt.get_cmap(&#39;gray&#39;))<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fdd\u5b58\u7070\u5ea6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>plt.imsave<\/code>\u51fd\u6570\u5c06\u7070\u5ea6\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = np.dot(image[...,:3], [0.299, 0.587, 0.114])<\/p>\n<h2><strong>\u4fdd\u5b58\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imsave(&#39;path\/to\/save\/gray_image.jpg&#39;, gray_image, cmap=plt.get_cmap(&#39;gray&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528OpenCV\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u51fd\u6570\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002Pillow\u5e93\u548cMatplotlib\u5e93\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u9002\u5408\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5e93\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u5e0c\u671b\u4f60\u80fd\u591f\u638c\u63e1\u5982\u4f55\u5728Python\u4e2d\u5c06\u56fe\u7247\u989c\u8272\u53bb\u6389\uff0c\u5e76\u80fd\u591f\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3002\u5982\u679c\u4f60\u5bf9\u56fe\u50cf\u5904\u7406\u6709\u66f4\u6df1\u5165\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b66\u4e60OpenCV\u3001Pillow\u548cMatplotlib\u5e93\u7684\u9ad8\u7ea7\u529f\u80fd\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5f69\u8272\u56fe\u7247\u8f6c\u6362\u4e3a\u9ed1\u767d\u56fe\u50cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684PIL\uff08Pillow\uff09\u5e93\u6765\u8f7b\u677e\u5b9e\u73b0\u5c06\u5f69\u8272\u56fe\u7247\u8f6c\u6362\u4e3a\u9ed1\u767d\u56fe\u50cf\u3002\u9996\u5148\uff0c\u5b89\u88c5Pillow\u5e93\uff0c\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u3002\u63a5\u7740\uff0c\u6253\u5f00\u56fe\u7247\u5e76\u4f7f\u7528<code>convert(&#39;L&#39;)<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u6a21\u5f0f\u3002\u6700\u540e\uff0c\u4fdd\u5b58\u6216\u663e\u793a\u7ed3\u679c\u56fe\u7247\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00\u56fe\u7247\nimage = Image.open(&#39;path_to_image.jpg&#39;)\n# \u8f6c\u6362\u4e3a\u7070\u5ea6\nbw_image = image.convert(&#39;L&#39;)\n# \u4fdd\u5b58\u56fe\u7247\nbw_image.save(&#39;path_to_bw_image.jpg&#39;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u53bb\u6389\u56fe\u7247\u7684\u7279\u5b9a\u989c\u8272\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u6765\u53bb\u6389\u56fe\u7247\u4e2d\u7684\u7279\u5b9a\u989c\u8272\u3002\u9996\u5148\uff0c\u901a\u8fc7<code>cv2.inRange()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u989c\u8272\u8303\u56f4\u63a9\u819c\uff0c\u7136\u540e\u5229\u7528<code>cv2.bitwise_and()<\/code>\u51fd\u6570\u5c06\u63a9\u819c\u5e94\u7528\u4e8e\u539f\u59cb\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\nimport numpy as np\n\n# \u8bfb\u53d6\u56fe\u7247\nimage = cv2.imread(&#39;path_to_image.jpg&#39;)\n\n# \u5b9a\u4e49\u989c\u8272\u8303\u56f4\nlower_color = np.array([0, 100, 100])  # \u989c\u8272\u4e0b\u754c\nupper_color = np.array([10, 255, 255])  # \u989c\u8272\u4e0a\u754c\n\n# \u521b\u5efa\u63a9\u819c\nmask = cv2.inRange(image, lower_color, upper_color)\n# \u53bb\u6389\u7279\u5b9a\u989c\u8272\nresult = cv2.bitwise_and(image, image, mask=mask)\n\n# \u4fdd\u5b58\u7ed3\u679c\ncv2.imwrite(&#39;path_to_result_image.jpg&#39;, result)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u56fe\u7247\u989c\u8272\u5904\u7406\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u5e93\u53ef\u4ee5\u9009\u62e9\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fdb\u884c\u56fe\u7247\u989c\u8272\u5904\u7406\u65f6\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecPillow\u3001OpenCV\u548cMatplotlib\u3002Pillow\u9002\u5408\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\u548c\u5904\u7406\uff1bOpenCV\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff1bMatplotlib\u5219\u5e38\u7528\u4e8e\u56fe\u50cf\u7684\u53ef\u89c6\u5316\u548c\u5c55\u793a\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u5c06\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u6765\u53bb\u6389\u989c\u8272\uff0c\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1064747,"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\/1064738"}],"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=1064738"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064738\/revisions"}],"predecessor-version":[{"id":1064752,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064738\/revisions\/1064752"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064747"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}