{"id":1152178,"date":"2025-01-13T17:22:40","date_gmt":"2025-01-13T09:22:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152178.html"},"modified":"2025-01-13T17:22:42","modified_gmt":"2025-01-13T09:22:42","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e5%83%8f%e7%b4%a0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152178.html","title":{"rendered":"python\u5982\u4f55\u8868\u793a\u50cf\u7d20\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182227\/3937ad55-9949-4a3e-b388-b01f0444d654.webp\" alt=\"python\u5982\u4f55\u8868\u793a\u50cf\u7d20\u503c\" \/><\/p>\n<p><p> Python\u8868\u793a\u50cf\u7d20\u503c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c<strong>\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528NumPy\u6570\u7ec4\u3001PIL\uff08Python Imaging Library\uff09\u3001OpenCV<\/strong>\u7b49\u56fe\u50cf\u5904\u7406\u5e93\u3002\u5176\u4e2d\uff0cNumPy\u6570\u7ec4\u662f\u6700\u57fa\u672c\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u3001\u7075\u6d3b\u7684\u591a\u7ef4\u6570\u7ec4\u6570\u636e\u7ed3\u6784\u3002PIL\u548cOpenCV\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\u548c\u529f\u80fd\uff0c\u4f7f\u5f97\u56fe\u50cf\u5904\u7406\u66f4\u52a0\u4fbf\u6377\u3002<strong>NumPy\u3001PIL\u3001OpenCV<\/strong>\u662fPython\u4e2d\u5904\u7406\u56fe\u50cf\u7684\u4e09\u79cd\u4e3b\u8981\u5de5\u5177\uff0c\u4e86\u89e3\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u8868\u793a\u548c\u5904\u7406\u50cf\u7d20\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001NumPy\u8868\u793a\u50cf\u7d20\u503c<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u3002\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u88ab\u8868\u793a\u4e3aNumPy\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u4e00\u4e2a\u50cf\u7d20\u503c\u3002<\/p>\n<\/p>\n<p><h4>1. \u7070\u5ea6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u7070\u5ea6\u56fe\u50cf\u7684\u6bcf\u4e2a\u50cf\u7d20\u503c\u662f\u4e00\u4e2a0\u5230255\u4e4b\u95f4\u7684\u6574\u6570\uff0c\u8868\u793a\u8be5\u50cf\u7d20\u7684\u4eae\u5ea6\u3002\u4e00\u4e2a\u7070\u5ea6\u56fe\u50cf\u53ef\u4ee5\u8868\u793a\u4e3a\u4e00\u4e2a\u4e8c\u7ef4NumPy\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u50cf\u7d20\u7684\u4eae\u5ea6\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a5x5\u7684\u7070\u5ea6\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a128\uff08\u4e2d\u7b49\u7070\u5ea6\uff09<\/strong><\/h2>\n<p>gray_image = np.full((5, 5), 128, dtype=np.uint8)<\/p>\n<p>print(gray_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5f69\u8272\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u5f69\u8272\u56fe\u50cf\u7684\u6bcf\u4e2a\u50cf\u7d20\u503c\u901a\u5e38\u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5206\u91cf\u7684\u6570\u7ec4\uff0c\u5206\u522b\u8868\u793a\u7ea2\u8272\uff08R\uff09\u3001\u7eff\u8272\uff08G\uff09\u548c\u84dd\u8272\uff08B\uff09\u7684\u5f3a\u5ea6\u3002\u4e00\u4e2a\u5f69\u8272\u56fe\u50cf\u53ef\u4ee5\u8868\u793a\u4e3a\u4e00\u4e2a\u4e09\u7ef4NumPy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a5x5\u7684\u5f69\u8272\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a\u7ea2\u8272\uff08255, 0, 0\uff09<\/strong><\/h2>\n<p>color_image = np.zeros((5, 5, 3), dtype=np.uint8)<\/p>\n<p>color_image[:, :, 0] = 255  # Red channel<\/p>\n<p>print(color_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001PIL\u8868\u793a\u50cf\u7d20\u503c<\/h3>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662fPython\u4e2d\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684\u7ecf\u5178\u5e93\u3002\u867d\u7136PIL\u5df2\u88abPillow\u6240\u53d6\u4ee3\uff0c\u4f46\u5176\u57fa\u672c\u7528\u6cd5\u4ecd\u7136\u76f8\u540c\u3002PIL\u63d0\u4f9b\u4e86\u4e00\u4e2aImage\u7c7b\uff0c\u7528\u4e8e\u8868\u793a\u56fe\u50cf\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1. \u7070\u5ea6\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a5x5\u7684\u7070\u5ea6\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a128\uff08\u4e2d\u7b49\u7070\u5ea6\uff09<\/strong><\/h2>\n<p>gray_image = Image.new(&#39;L&#39;, (5, 5), 128)<\/p>\n<p>gray_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5f69\u8272\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a5x5\u7684\u5f69\u8272\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a\u7ea2\u8272\uff08255, 0, 0\uff09<\/strong><\/h2>\n<p>color_image = Image.new(&#39;RGB&#39;, (5, 5), (255, 0, 0))<\/p>\n<p>color_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001OpenCV\u8868\u793a\u50cf\u7d20\u503c<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u3002OpenCV\u4e2d\u7684\u56fe\u50cf\u4e5f\u662f\u8868\u793a\u4e3aNumPy\u6570\u7ec4\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u7070\u5ea6\u56fe\u50cf<\/h4>\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\u4e2a5x5\u7684\u7070\u5ea6\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a128\uff08\u4e2d\u7b49\u7070\u5ea6\uff09<\/strong><\/h2>\n<p>gray_image = np.full((5, 5), 128, dtype=np.uint8)<\/p>\n<p>cv2.imshow(&#39;Gray Image&#39;, gray_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><h4>2. \u5f69\u8272\u56fe\u50cf<\/h4>\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\u4e2a5x5\u7684\u5f69\u8272\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a\u7ea2\u8272\uff08255, 0, 0\uff09<\/strong><\/h2>\n<p>color_image = np.zeros((5, 5, 3), dtype=np.uint8)<\/p>\n<p>color_image[:, :, 2] = 255  # Red channel in OpenCV (BGR order)<\/p>\n<p>cv2.imshow(&#39;Color Image&#39;, color_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001NumPy\u4e2d\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u53ef\u4ee5\u7528\u6765\u8868\u793a\u50cf\u7d20\u503c\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u8bb8\u591a\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5982\u56fe\u50cf\u7684\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u6ee4\u6ce2\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u56fe\u50cf\u88c1\u526a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.randint(0, 256, (10, 10), dtype=np.uint8)<\/p>\n<h2><strong>\u88c1\u526a\u51fa\u4e00\u4e2a5x5\u7684\u5b50\u56fe\u50cf<\/strong><\/h2>\n<p>sub_image = image[2:7, 2:7]<\/p>\n<p>print(sub_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u65cb\u8f6c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.randint(0, 256, (10, 10), dtype=np.uint8)<\/p>\n<h2><strong>\u65cb\u8f6c90\u5ea6<\/strong><\/h2>\n<p>rotated_image = np.rot90(image)<\/p>\n<p>print(rotated_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001PIL\u4e2d\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>PIL\u63d0\u4f9b\u4e86\u8bb8\u591a\u56fe\u50cf\u5904\u7406\u51fd\u6570\uff0c\u5982\u56fe\u50cf\u7684\u7f29\u653e\u3001\u65cb\u8f6c\u3001\u6ee4\u6ce2\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u56fe\u50cf\u7f29\u653e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.fromarray(np.random.randint(0, 256, (10, 10), dtype=np.uint8))<\/p>\n<h2><strong>\u7f29\u653e\u81f35x5<\/strong><\/h2>\n<p>resized_image = image.resize((5, 5))<\/p>\n<p>resized_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u65cb\u8f6c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.fromarray(np.random.randint(0, 256, (10, 10), dtype=np.uint8))<\/p>\n<h2><strong>\u65cb\u8f6c45\u5ea6<\/strong><\/h2>\n<p>rotated_image = image.rotate(45)<\/p>\n<p>rotated_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001OpenCV\u4e2d\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u51fd\u6570\uff0c\u5982\u56fe\u50cf\u7684\u5e73\u6ed1\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u5f62\u6001\u5b66\u64cd\u4f5c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u56fe\u50cf\u5e73\u6ed1<\/h4>\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\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.randint(0, 256, (10, 10), dtype=np.uint8)<\/p>\n<h2><strong>\u5e94\u7528\u9ad8\u65af\u5e73\u6ed1<\/strong><\/h2>\n<p>smoothed_image = cv2.GaussianBlur(image, (3, 3), 0)<\/p>\n<p>cv2.imshow(&#39;Smoothed Image&#39;, smoothed_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8fb9\u7f18\u68c0\u6d4b<\/h4>\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\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.randint(0, 256, (10, 10), dtype=np.uint8)<\/p>\n<h2><strong>\u5e94\u7528Canny\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = cv2.Canny(image, 100, 200)<\/p>\n<p>cv2.imshow(&#39;Edges&#39;, edges)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u56fe\u50cf\u5b58\u50a8\u548c\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u5e93\uff0c\u56fe\u50cf\u7684\u5b58\u50a8\u548c\u8bfb\u53d6\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u3001PIL\u548cOpenCV\u8fdb\u884c\u56fe\u50cf\u7684\u5b58\u50a8\u548c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528NumPy<\/h4>\n<\/p>\n<p><p>NumPy\u672c\u8eab\u4e0d\u652f\u6301\u76f4\u63a5\u8bfb\u53d6\u548c\u5b58\u50a8\u56fe\u50cf\uff0c\u4f46\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.array(Image.open(&#39;example.jpg&#39;))<\/p>\n<h2><strong>\u5b58\u50a8\u56fe\u50cf<\/strong><\/h2>\n<p>Image.fromarray(image).save(&#39;output.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528PIL<\/h4>\n<\/p>\n<p><p>PIL\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u8bfb\u53d6\u548c\u5b58\u50a8\u56fe\u50cf\u3002<\/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;example.jpg&#39;)<\/p>\n<h2><strong>\u5b58\u50a8\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528OpenCV<\/h4>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u51fd\u6570\u6765\u8bfb\u53d6\u548c\u5b58\u50a8\u56fe\u50cf\u3002<\/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;example.jpg&#39;)<\/p>\n<h2><strong>\u5b58\u50a8\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;output.jpg&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u989c\u8272\u7a7a\u95f4<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u989c\u8272\u7a7a\u95f4\u662f\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u4e0d\u540c\u7684\u989c\u8272\u7a7a\u95f4\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. RGB\u989c\u8272\u7a7a\u95f4<\/h4>\n<\/p>\n<p><p>RGB\u989c\u8272\u7a7a\u95f4\u662f\u6700\u5e38\u89c1\u7684\u989c\u8272\u7a7a\u95f4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u50cf\u7d20\u7531\u7ea2\u3001\u7eff\u3001\u84dd\u4e09\u4e2a\u5206\u91cf\u7ec4\u6210\u3002<\/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\u4e2a5x5\u7684\u5f69\u8272\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a\u7ea2\u8272\uff08255, 0, 0\uff09<\/strong><\/h2>\n<p>color_image = np.zeros((5, 5, 3), dtype=np.uint8)<\/p>\n<p>color_image[:, :, 2] = 255  # Red channel in OpenCV (BGR order)<\/p>\n<p>cv2.imshow(&#39;RGB Image&#39;, color_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7070\u5ea6\u989c\u8272\u7a7a\u95f4<\/h4>\n<\/p>\n<p><p>\u7070\u5ea6\u989c\u8272\u7a7a\u95f4\u7528\u4e8e\u8868\u793a\u7070\u5ea6\u56fe\u50cf\uff0c\u5176\u4e2d\u6bcf\u4e2a\u50cf\u7d20\u503c\u662f\u4e00\u4e2a\u8868\u793a\u4eae\u5ea6\u7684\u6574\u6570\u3002<\/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\u4e2a5x5\u7684\u7070\u5ea6\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a128\uff08\u4e2d\u7b49\u7070\u5ea6\uff09<\/strong><\/h2>\n<p>gray_image = np.full((5, 5), 128, dtype=np.uint8)<\/p>\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><h4>3. HSV\u989c\u8272\u7a7a\u95f4<\/h4>\n<\/p>\n<p><p>HSV\u989c\u8272\u7a7a\u95f4\u8868\u793a\u8272\u8c03\uff08H\uff09\u3001\u9971\u548c\u5ea6\uff08S\uff09\u548c\u660e\u5ea6\uff08V\uff09\uff0c\u5728\u989c\u8272\u5206\u5272\u548c\u68c0\u6d4b\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/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\u4e2a5x5\u7684\u5f69\u8272\u56fe\u50cf\uff0c\u6240\u6709\u50cf\u7d20\u503c\u4e3a\u7ea2\u8272\uff08255, 0, 0\uff09<\/strong><\/h2>\n<p>color_image = np.zeros((5, 5, 3), dtype=np.uint8)<\/p>\n<p>color_image[:, :, 2] = 255  # Red channel in OpenCV (BGR order)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aHSV\u989c\u8272\u7a7a\u95f4<\/strong><\/h2>\n<p>hsv_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)<\/p>\n<p>cv2.imshow(&#39;HSV Image&#39;, hsv_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u5f62\u6001\u5b66\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5f62\u6001\u5b66\u64cd\u4f5c\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u4e3b\u8981\u5305\u62ec\u8150\u8680\u3001\u81a8\u80c0\u3001\u5f00\u8fd0\u7b97\u3001\u95ed\u8fd0\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u8150\u8680<\/h4>\n<\/p>\n<p><p>\u8150\u8680\u64cd\u4f5c\u7528\u4e8e\u53bb\u9664\u56fe\u50cf\u4e2d\u7684\u5c0f\u7269\u4f53\u548c\u566a\u58f0\uff0c\u4f7f\u5f97\u56fe\u50cf\u4e2d\u7684\u7269\u4f53\u53d8\u5c0f\u3002<\/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\u4e2a10x10\u7684\u4e8c\u503c\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((10, 10), dtype=np.uint8)<\/p>\n<p>image[3:7, 3:7] = 255<\/p>\n<h2><strong>\u5e94\u7528\u8150\u8680\u64cd\u4f5c<\/strong><\/h2>\n<p>kernel = np.ones((3, 3), np.uint8)<\/p>\n<p>eroded_image = cv2.erode(image, kernel)<\/p>\n<p>cv2.imshow(&#39;Eroded Image&#39;, eroded_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81a8\u80c0<\/h4>\n<\/p>\n<p><p>\u81a8\u80c0\u64cd\u4f5c\u7528\u4e8e\u586b\u8865\u56fe\u50cf\u4e2d\u7684\u5c0f\u5b54\u6d1e\u548c\u65ad\u88c2\uff0c\u4f7f\u5f97\u56fe\u50cf\u4e2d\u7684\u7269\u4f53\u53d8\u5927\u3002<\/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\u4e2a10x10\u7684\u4e8c\u503c\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((10, 10), dtype=np.uint8)<\/p>\n<p>image[3:7, 3:7] = 255<\/p>\n<h2><strong>\u5e94\u7528\u81a8\u80c0\u64cd\u4f5c<\/strong><\/h2>\n<p>kernel = np.ones((3, 3), np.uint8)<\/p>\n<p>dilated_image = cv2.dilate(image, kernel)<\/p>\n<p>cv2.imshow(&#39;Dilated Image&#39;, dilated_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u6ee4\u6ce2\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6ee4\u6ce2\u64cd\u4f5c\u7528\u4e8e\u5e73\u6ed1\u56fe\u50cf\u3001\u53bb\u9664\u566a\u58f0\u3001\u589e\u5f3a\u56fe\u50cf\u7ec6\u8282\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5747\u503c\u6ee4\u6ce2<\/h4>\n<\/p>\n<p><p>\u5747\u503c\u6ee4\u6ce2\u901a\u8fc7\u53d6\u5c40\u90e8\u90bb\u57df\u5185\u50cf\u7d20\u503c\u7684\u5e73\u5747\u503c\u6765\u5e73\u6ed1\u56fe\u50cf\u3002<\/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\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.randint(0, 256, (10, 10), dtype=np.uint8)<\/p>\n<h2><strong>\u5e94\u7528\u5747\u503c\u6ee4\u6ce2<\/strong><\/h2>\n<p>blurred_image = cv2.blur(image, (3, 3))<\/p>\n<p>cv2.imshow(&#39;Blurred Image&#39;, blurred_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9ad8\u65af\u6ee4\u6ce2<\/h4>\n<\/p>\n<p><p>\u9ad8\u65af\u6ee4\u6ce2\u901a\u8fc7\u9ad8\u65af\u51fd\u6570\u52a0\u6743\u5c40\u90e8\u90bb\u57df\u5185\u7684\u50cf\u7d20\u503c\u6765\u5e73\u6ed1\u56fe\u50cf\u3002<\/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\u4e2a10x10\u7684\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.randint(0, 256, (10, 10), dtype=np.uint8)<\/p>\n<h2><strong>\u5e94\u7528\u9ad8\u65af\u6ee4\u6ce2<\/strong><\/h2>\n<p>gaussian_blurred_image = cv2.GaussianBlur(image, (3, 3), 0)<\/p>\n<p>cv2.imshow(&#39;Gaussian Blurred Image&#39;, gaussian_blurred_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86Python\u4e2d\u5982\u4f55\u4f7f\u7528NumPy\u3001PIL\u548cOpenCV\u6765\u8868\u793a\u548c\u5904\u7406\u50cf\u7d20\u503c\u3002<strong>NumPy\u3001PIL\u3001OpenCV<\/strong>\u662fPython\u4e2d\u5904\u7406\u56fe\u50cf\u7684\u4e09\u79cd\u4e3b\u8981\u5de5\u5177\uff0c\u6bcf\u79cd\u5de5\u5177\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u57fa\u672c\u7684\u56fe\u50cf\u8868\u793a\uff0c\u8fd8\u662f\u8fdb\u884c\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u5de5\u5177\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u4f60\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\u7684\u5b66\u4e60\u548c\u5e94\u7528\u63d0\u4f9b\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8868\u793a\u56fe\u50cf\u7684\u50cf\u7d20\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u901a\u5e38\u4ee5\u4e8c\u7ef4\u6570\u7ec4\u7684\u5f62\u5f0f\u8868\u793a\u3002\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u56fe\u50cf\u4e2d\u4e00\u4e2a\u50cf\u7d20\u7684\u503c\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u8303\u56f4\u4ece0\u5230255\uff08\u5bf9\u4e8e8\u4f4d\u7070\u5ea6\u56fe\u50cf\uff09\u3002\u5bf9\u4e8e\u5f69\u8272\u56fe\u50cf\uff0c\u50cf\u7d20\u503c\u901a\u5e38\u7531\u4e09\u4e2a\u6574\u6570\u6784\u6210\uff0c\u5206\u522b\u8868\u793a\u7ea2\u8272\u3001\u7eff\u8272\u548c\u84dd\u8272\uff08RGB\uff09\u901a\u9053\u7684\u5f3a\u5ea6\u3002\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u8fd9\u4e9b\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u56fe\u50cf\u65f6\uff0cPython\u6709\u54ea\u4e9b\u5e38\u7528\u5e93\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><br \/>Python\u6709\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\u53ef\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u50cf\u7d20\u503c\u64cd\u4f5c\u3002OpenCV\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002Pillow\uff08PIL\u7684\u4e00\u4e2a\u5206\u652f\uff09\u5219\u662f\u4e00\u4e2a\u7528\u6237\u53cb\u597d\u7684\u5e93\uff0c\u9002\u5408\u56fe\u50cf\u6253\u5f00\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u3002\u8fd8\u6709Matplotlib\u53ef\u4ee5\u7528\u4e8e\u53ef\u89c6\u5316\u56fe\u50cf\u6570\u636e\uff0c\u4ee5\u53caNumPy\u53ef\u4ee5\u7528\u4e8e\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u548c\u6570\u7ec4\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bfb\u53d6\u548c\u4fee\u6539\u56fe\u50cf\u7684\u50cf\u7d20\u503c\uff1f<\/strong><br \/>\u8bfb\u53d6\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u7684<code>Image<\/code>\u6a21\u5757\u3002\u901a\u8fc7<code>Image.open()<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>load()<\/code>\u65b9\u6cd5\u83b7\u53d6\u50cf\u7d20\u503c\u7684\u8bbf\u95ee\u5bf9\u8c61\u3002\u82e5\u8981\u4fee\u6539\u50cf\u7d20\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7279\u5b9a\u4f4d\u7f6e\u7684\u50cf\u7d20\u5e76\u8fdb\u884c\u8d4b\u503c\u3002\u4f7f\u7528NumPy\u4e5f\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8bfb\u53d6\u548c\u4fee\u6539\u50cf\u7d20\u503c\uff0c\u901a\u8fc7\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u5229\u7528\u5176\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u8fdb\u884c\u6279\u91cf\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8868\u793a\u50cf\u7d20\u503c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528NumPy\u6570\u7ec4\u3001PIL\uff08Python Imaging L [&hellip;]","protected":false},"author":3,"featured_media":1152187,"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\/1152178"}],"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=1152178"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152178\/revisions"}],"predecessor-version":[{"id":1152189,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152178\/revisions\/1152189"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152187"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}