{"id":1080224,"date":"2025-01-08T12:28:07","date_gmt":"2025-01-08T04:28:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080224.html"},"modified":"2025-01-08T12:28:09","modified_gmt":"2025-01-08T04:28:09","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e9%bb%91%e8%89%b2%e7%9a%84%e5%86%85%e5%ae%b9%e5%8e%bb%e6%8e%89-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080224.html","title":{"rendered":"python\u5982\u4f55\u5c06\u9ed1\u8272\u7684\u5185\u5bb9\u53bb\u6389"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182855\/9a4fb64c-4097-4609-a7ff-94dfb98aab83.webp\" alt=\"python\u5982\u4f55\u5c06\u9ed1\u8272\u7684\u5185\u5bb9\u53bb\u6389\" \/><\/p>\n<p><h3>PYTHON\u5982\u4f55\u5c06\u9ed1\u8272\u7684\u5185\u5bb9\u53bb\u6389<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u5982OpenCV\u3001Pillow\u7b49\u6765\u53bb\u9664\u56fe\u50cf\u4e2d\u7684\u9ed1\u8272\u5185\u5bb9<\/strong>\u3002\u901a\u5e38\u7684\u505a\u6cd5\u662f\u904d\u5386\u56fe\u50cf\u7684\u6bcf\u4e2a\u50cf\u7d20\uff0c\u5c06\u9ed1\u8272\uff08\u6216\u63a5\u8fd1\u9ed1\u8272\uff09\u7684\u50cf\u7d20\u66ff\u6362\u4e3a\u900f\u660e\u6216\u5176\u4ed6\u989c\u8272\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u4e00\u3001\u4f7f\u7528OpenCV\u53bb\u9664\u9ed1\u8272\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528OpenCV\u53bb\u9664\u56fe\u50cf\u4e2d\u9ed1\u8272\u5185\u5bb9\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/li>\n<li><strong>\u5b9a\u4e49\u9ed1\u8272\u8303\u56f4<\/strong><\/li>\n<li><strong>\u521b\u5efa\u63a9\u7801<\/strong><\/li>\n<li><strong>\u66ff\u6362\u9ed1\u8272\u50cf\u7d20<\/strong><\/li>\n<\/ol>\n<p><h5>1. \u8bfb\u53d6\u56fe\u50cf<\/h5>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u3002OpenCV\u7684<code>cv2.imread<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_your_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>2. \u5b9a\u4e49\u9ed1\u8272\u8303\u56f4<\/h5>\n<\/p>\n<p><p>\u4e3a\u4e86\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u9ed1\u8272\u50cf\u7d20\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u9ed1\u8272\u7684\u8303\u56f4\u3002\u9ed1\u8272\u5728RGB\u989c\u8272\u7a7a\u95f4\u4e2d\u901a\u5e38\u662f(0, 0, 0)\uff0c\u4f46\u4e3a\u4e86\u5904\u7406\u6709\u566a\u58f0\u7684\u56fe\u50cf\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u8bbe\u7f6e\u4e00\u4e2a\u8f83\u5c0f\u7684\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u9ed1\u8272\u7684\u4e0a\u4e0b\u9650<\/p>\n<p>lower_black = (0, 0, 0)<\/p>\n<p>upper_black = (50, 50, 50)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>3. \u521b\u5efa\u63a9\u7801<\/h5>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684<code>cv2.inRange<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u63a9\u7801\uff0c\u63a9\u7801\u4e2d\u9ed1\u8272\u50cf\u7d20\u7684\u4f4d\u7f6e\u4f1a\u88ab\u6807\u8bb0\u4e3a\u767d\u8272\uff08255\uff09\uff0c\u5176\u4ed6\u50cf\u7d20\u4f1a\u88ab\u6807\u8bb0\u4e3a\u9ed1\u8272\uff080\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u9ed1\u8272\u50cf\u7d20\u7684\u63a9\u7801<\/p>\n<p>mask = cv2.inRange(image, lower_black, upper_black)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>4. \u66ff\u6362\u9ed1\u8272\u50cf\u7d20<\/h5>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u63a9\u7801\u5c06\u9ed1\u8272\u50cf\u7d20\u66ff\u6362\u4e3a\u767d\u8272\u6216\u5176\u4ed6\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u9ed1\u8272\u50cf\u7d20\u66ff\u6362\u4e3a\u767d\u8272<\/p>\n<p>image[mask == 255] = [255, 255, 255]<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;output_image.jpg&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e8c\u3001\u4f7f\u7528Pillow\u53bb\u9664\u9ed1\u8272\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>Pillow\u662fPython\u7684\u53e6\u4e00\u4e2a\u6d41\u884c\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528Pillow\u53bb\u9664\u56fe\u50cf\u4e2d\u9ed1\u8272\u5185\u5bb9\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/li>\n<li><strong>\u904d\u5386\u50cf\u7d20<\/strong><\/li>\n<li><strong>\u66ff\u6362\u9ed1\u8272\u50cf\u7d20<\/strong><\/li>\n<\/ol>\n<p><h5>1. \u8bfb\u53d6\u56fe\u50cf<\/h5>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u3002Pillow\u7684<code>Image.open<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path_to_your_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>2. \u904d\u5386\u50cf\u7d20<\/h5>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>image.load<\/code>\u65b9\u6cd5\u83b7\u53d6\u50cf\u7d20\u6570\u636e\uff0c\u5e76\u904d\u5386\u6bcf\u4e2a\u50cf\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u50cf\u7d20\u6570\u636e<\/p>\n<p>pixels = image.load()<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>width, height = image.size<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>3. \u66ff\u6362\u9ed1\u8272\u50cf\u7d20<\/h5>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u6bcf\u4e2a\u50cf\u7d20\uff0c\u5e76\u5c06\u9ed1\u8272\u50cf\u7d20\u66ff\u6362\u4e3a\u767d\u8272\u6216\u5176\u4ed6\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for x in range(width):<\/p>\n<p>    for y in range(height):<\/p>\n<p>        r, g, b = pixels[x, y]<\/p>\n<p>        if r &lt; 50 and g &lt; 50 and b &lt; 50:  # \u8fd9\u91cc\u768450\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574<\/p>\n<p>            pixels[x, y] = (255, 255, 255)<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e09\u3001\u603b\u7ed3<\/h4>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684OpenCV\u548cPillow\u5e93\u53bb\u9664\u56fe\u50cf\u4e2d\u7684\u9ed1\u8272\u5185\u5bb9\u3002<strong>\u901a\u8fc7\u8bfb\u53d6\u56fe\u50cf\u3001\u5b9a\u4e49\u9ed1\u8272\u8303\u56f4\u3001\u521b\u5efa\u63a9\u7801\u6216\u904d\u5386\u50cf\u7d20\u5e76\u66ff\u6362\u9ed1\u8272\u50cf\u7d20<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u56fe\u50cf\u4e2d\u7684\u9ed1\u8272\u533a\u57df\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u56fe\u50cf\u5904\u7406\u9700\u6c42\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8c03\u6574\u4ee3\u7801\u4e2d\u7684\u53c2\u6570\u548c\u903b\u8f91\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u53bb\u9664\u56fe\u50cf\u4e2d\u7684\u9ed1\u8272\u5185\u5bb9\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u56fe\u50cf\u5904\u7406\u5e93\u5982Pillow\u548cOpenCV\u6765\u5904\u7406\u56fe\u50cf\u3002\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u9608\u503c\uff0c\u8bc6\u522b\u9ed1\u8272\u533a\u57df\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u900f\u660e\u6216\u5176\u4ed6\u989c\u8272\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u6d89\u53ca\u5230\u56fe\u50cf\u7684\u50cf\u7d20\u7ea7\u64cd\u4f5c\uff0c\u80fd\u591f\u6709\u6548\u5730\u53bb\u9664\u4e0d\u9700\u8981\u7684\u9ed1\u8272\u90e8\u5206\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684Python\u5e93\u53ef\u4ee5\u7b80\u5316\u53bb\u9664\u9ed1\u8272\u5185\u5bb9\u7684\u8fc7\u7a0b\uff1f<\/strong><br \/>\u662f\u7684\uff0cPillow\u548cOpenCV\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u3002Pillow\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\uff0c\u800cOpenCV\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u4e24\u4e2a\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u56fe\u50cf\u7684\u5904\u7406\u548c\u9ed1\u8272\u533a\u57df\u7684\u53bb\u9664\u3002<\/p>\n<p><strong>\u53bb\u9664\u9ed1\u8272\u5185\u5bb9\u540e\uff0c\u5982\u4f55\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u5904\u7406\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u7684<code>save()<\/code>\u65b9\u6cd5\u6216OpenCV\u7684<code>imwrite()<\/code>\u51fd\u6570\u5c06\u7ed3\u679c\u4fdd\u5b58\u4e3a\u65b0\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\uff08\u5982PNG\u6216JPEG\uff09\u4e5f\u5f88\u91cd\u8981\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u4fdd\u7559\u900f\u660e\u80cc\u666f\u65f6\uff0cPNG\u683c\u5f0f\u662f\u9996\u9009\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON\u5982\u4f55\u5c06\u9ed1\u8272\u7684\u5185\u5bb9\u53bb\u6389 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u5982OpenCV\u3001Pillow\u7b49\u6765\u53bb\u9664\u56fe\u50cf [&hellip;]","protected":false},"author":3,"featured_media":1080229,"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\/1080224"}],"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=1080224"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080224\/revisions"}],"predecessor-version":[{"id":1080231,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080224\/revisions\/1080231"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080229"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}