{"id":1103427,"date":"2025-01-08T16:10:22","date_gmt":"2025-01-08T08:10:22","guid":{"rendered":""},"modified":"2025-01-08T16:10:31","modified_gmt":"2025-01-08T08:10:31","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e5%87%ba%e4%b8%80%e5%bc%a0%e7%85%a7%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1103427.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u51fa\u4e00\u5f20\u7167\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065240\/04910ac8-2ba6-478a-914d-e705d0699298.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u51fa\u4e00\u5f20\u7167\u7247\" \/><\/p>\n<p><p> <strong>\u4e00\u3001\u6982\u8ff0<\/strong><\/p>\n<\/p>\n<p><p><strong>\u8981\u7528Python\u753b\u51fa\u4e00\u5f20\u7167\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u3001OpenCV\u3001Matplotlib\u7b49\u5e93\uff0c\u8fd9\u4e9b\u5e93\u5404\u6709\u4f18\u70b9\uff0c\u53ef\u4ee5\u5b9e\u73b0\u56fe\u50cf\u7684\u8bfb\u53d6\u3001\u5904\u7406\u548c\u663e\u793a<\/strong>\u3002\u5176\u4e2d\uff0cPillow\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u64cd\u4f5c\u529f\u80fd\uff1bOpenCV\u662f\u4e00\u4e2a\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5177\u6709\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u80fd\u529b\uff1bMatplotlib\u662f\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u663e\u793a\u56fe\u50cf\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u753b\u51fa\u4e00\u5f20\u7167\u7247\u3002<\/p>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u9996\u5148\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Pillow\u8fd9\u4e2a\u56fe\u50cf\u5904\u7406\u5e93\u3002Pillow\uff08PIL Fork\uff09\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5728\u56fe\u50cf\u5904\u7406\u65b9\u9762\u63d0\u4f9b\u4e86\u8bb8\u591a\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u5305\u62ec\u56fe\u50cf\u7684\u6253\u5f00\u3001\u663e\u793a\u3001\u4fdd\u5b58\u3001\u53d8\u6362\u548c\u6ee4\u955c\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528Pillow\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/strong><\/p>\n<\/p>\n<p><p>Pillow\u5e93\u662fPython\u4e2d\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u8bfb\u53d6\u3001\u5904\u7406\u548c\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Pillow\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8fd9\u4e2a\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Pillow\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Pillow\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6765\u6253\u5f00\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>image.show()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4fdd\u5b58\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u5bf9\u56fe\u50cf\u8fdb\u884c\u4e86\u5904\u7406\u5e76\u5e0c\u671b\u4fdd\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528<code>image.save()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>image.save(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528OpenCV\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/strong><\/p>\n<\/p>\n<p><p>OpenCV\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\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5OpenCV\u5e93<\/li>\n<\/ol>\n<p><p>\u540c\u6837\u5730\uff0c\u5728\u5f00\u59cb\u4f7f\u7528OpenCV\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8fd9\u4e2a\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5OpenCV\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528OpenCV\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\u7684\u57fa\u672c\u793a\u4f8b\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_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, image)<\/p>\n<h2><strong>\u7b49\u5f85\u6309\u952e\u4e8b\u4ef6<\/strong><\/h2>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<h2><strong>\u5173\u95ed\u6240\u6709\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>cv2.imread()<\/code>\u65b9\u6cd5\u6765\u8bfb\u53d6\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>cv2.imshow()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u56fe\u50cf\u3002<code>cv2.waitKey(0)<\/code>\u65b9\u6cd5\u7528\u4e8e\u7b49\u5f85\u6309\u952e\u4e8b\u4ef6\uff0c\u4ee5\u4fbf\u5728\u663e\u793a\u56fe\u50cf\u7a97\u53e3\u65f6\u7a0b\u5e8f\u4e0d\u4f1a\u7acb\u5373\u7ed3\u675f\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4fdd\u5b58\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u5bf9\u56fe\u50cf\u8fdb\u884c\u4e86\u5904\u7406\u5e76\u5e0c\u671b\u4fdd\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.imwrite()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>cv2.imwrite(&#39;output_image.jpg&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528Matplotlib\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/strong><\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\uff0c\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u56fe\u8868\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Matplotlib\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Matplotlib\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8fd9\u4e2a\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Matplotlib\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Matplotlib\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\u7684\u57fa\u672c\u793a\u4f8b\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\u56fe\u50cf<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;) # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>mpimg.imread()<\/code>\u65b9\u6cd5\u6765\u8bfb\u53d6\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>plt.imshow()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u56fe\u50cf\u5904\u7406\u548c\u53d8\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u4e2a\u5e93\uff0c\u56fe\u50cf\u5904\u7406\u548c\u53d8\u6362\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/li>\n<\/ol>\n<p><p>\u5728Pillow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>resize()<\/code>\u65b9\u6cd5\u6765\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = image.resize((width, height))<\/p>\n<h2><strong>\u663e\u793a\u8c03\u6574\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>resized_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.resize()<\/code>\u65b9\u6cd5\u6765\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\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_image.jpg&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = cv2.resize(image, (width, height))<\/p>\n<h2><strong>\u663e\u793a\u8c03\u6574\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Resized Image&#39;, resized_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u56fe\u50cf\u65cb\u8f6c<\/li>\n<\/ol>\n<p><p>\u5728Pillow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>rotate()<\/code>\u65b9\u6cd5\u6765\u65cb\u8f6c\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u50cf<\/p>\n<p>rotated_image = image.rotate(angle)<\/p>\n<h2><strong>\u663e\u793a\u65cb\u8f6c\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.getRotationMatrix2D()<\/code>\u548c<code>cv2.warpAffine()<\/code>\u65b9\u6cd5\u6765\u65cb\u8f6c\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u65cb\u8f6c\u77e9\u9635<\/p>\n<p>rotation_matrix = cv2.getRotationMatrix2D((center_x, center_y), angle, scale)<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))<\/p>\n<h2><strong>\u663e\u793a\u65cb\u8f6c\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Rotated Image&#39;, rotated_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u56fe\u50cf\u88c1\u526a<\/li>\n<\/ol>\n<p><p>\u5728Pillow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u7684<code>crop()<\/code>\u65b9\u6cd5\u6765\u88c1\u526a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u50cf<\/p>\n<p>cropped_image = image.crop((left, upper, right, lower))<\/p>\n<h2><strong>\u663e\u793a\u88c1\u526a\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cropped_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u6570\u7ec4\u5207\u7247\u6765\u88c1\u526a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u50cf<\/p>\n<p>cropped_image = image[upper:lower, left:right]<\/p>\n<h2><strong>\u663e\u793a\u88c1\u526a\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Cropped Image&#39;, cropped_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516d\u3001\u56fe\u50cf\u6ee4\u955c\u548c\u6548\u679c<\/strong><\/p>\n<\/p>\n<p><p>\u56fe\u50cf\u6ee4\u955c\u548c\u6548\u679c\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u91cd\u8981\u90e8\u5206\uff0c\u53ef\u4ee5\u7528\u6765\u589e\u5f3a\u56fe\u50cf\u6216\u8005\u521b\u5efa\u7279\u6b8a\u6548\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u5728Pillow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>convert(&#39;L&#39;)<\/code>\u65b9\u6cd5\u6765\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\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><p>\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.cvtColor()<\/code>\u65b9\u6cd5\u6765\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\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<ol start=\"2\">\n<li>\u5e94\u7528\u6a21\u7cca\u6548\u679c<\/li>\n<\/ol>\n<p><p>\u5728Pillow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>filter()<\/code>\u65b9\u6cd5\u548c<code>ImageFilter<\/code>\u6a21\u5757\u6765\u5e94\u7528\u6a21\u7cca\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageFilter<\/p>\n<h2><strong>\u5e94\u7528\u6a21\u7cca\u6548\u679c<\/strong><\/h2>\n<p>blurred_image = image.filter(ImageFilter.BLUR)<\/p>\n<h2><strong>\u663e\u793a\u6a21\u7cca\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>blurred_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.GaussianBlur()<\/code>\u65b9\u6cd5\u6765\u5e94\u7528\u9ad8\u65af\u6a21\u7cca\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5e94\u7528\u9ad8\u65af\u6a21\u7cca\u6548\u679c<\/p>\n<p>blurred_image = cv2.GaussianBlur(image, (kernel_width, kernel_height), sigma)<\/p>\n<h2><strong>\u663e\u793a\u6a21\u7cca\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\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><p><strong>\u4e03\u3001\u56fe\u50cf\u7684\u9ad8\u7ea7\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0cPython\u8fd8\u53ef\u4ee5\u8fdb\u884c\u66f4\u52a0\u590d\u6742\u548c\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u6bd4\u5982\u8fb9\u7f18\u68c0\u6d4b\u3001\u56fe\u50cf\u5206\u5272\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8fb9\u7f18\u68c0\u6d4b<\/li>\n<\/ol>\n<p><p>\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\u6765\u68c0\u6d4b\u56fe\u50cf\u7684\u8fb9\u7f18\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528Canny\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = cv2.Canny(gray_image, threshold1, threshold2)<\/p>\n<h2><strong>\u663e\u793a\u8fb9\u7f18\u68c0\u6d4b\u7ed3\u679c<\/strong><\/h2>\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<ol start=\"2\">\n<li>\u56fe\u50cf\u5206\u5272<\/li>\n<\/ol>\n<p><p>\u56fe\u50cf\u5206\u5272\u662f\u5c06\u56fe\u50cf\u5206\u6210\u591a\u4e2a\u6709\u610f\u4e49\u7684\u90e8\u5206\u3002\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528KMeans\u805a\u7c7b\u7b97\u6cd5\u6765\u8fdb\u884c\u56fe\u50cf\u5206\u5272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>pixel_values = image.reshape((-1, 3))<\/p>\n<p>pixel_values = np.float32(pixel_values)<\/p>\n<h2><strong>\u5b9a\u4e49KMeans\u53c2\u6570<\/strong><\/h2>\n<p>criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)<\/p>\n<p>k = 3<\/p>\n<h2><strong>\u5e94\u7528KMeans\u805a\u7c7b<\/strong><\/h2>\n<p>_, labels, centers = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)<\/p>\n<h2><strong>\u8f6c\u6362\u56deuint8\u683c\u5f0f<\/strong><\/h2>\n<p>centers = np.uint8(centers)<\/p>\n<p>segmented_image = centers[labels.flatten()]<\/p>\n<p>segmented_image = segmented_image.reshape(image.shape)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf\u5206\u5272\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Segmented Image&#39;, segmented_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684Pillow\u3001OpenCV\u548cMatplotlib\u5e93\u6765\u8bfb\u53d6\u3001\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\uff0c\u5e76\u8fdb\u884c\u4e86\u56fe\u50cf\u7684\u57fa\u672c\u5904\u7406\u548c\u53d8\u6362\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u5e94\u7528\u56fe\u50cf\u6ee4\u955c\u548c\u6548\u679c\u4ee5\u53ca\u4e00\u4e9b\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<strong>Python\u7684\u8fd9\u4e9b\u56fe\u50cf\u5904\u7406\u5e93\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5b9e\u73b0\u5404\u79cd\u56fe\u50cf\u5904\u7406\u9700\u6c42<\/strong>\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u4f60\u5728Python\u56fe\u50cf\u5904\u7406\u7684\u9053\u8def\u4e0a\u8d70\u5f97\u66f4\u8fdc\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u7ed8\u5236\u548c\u5904\u7406\u7167\u7247\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7ed8\u5236\u548c\u5904\u7406\u7167\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u5b9e\u73b0\uff0c\u4f8b\u5982Matplotlib\u3001PIL\uff08Pillow\uff09\u548cOpenCV\u7b49\u3002Matplotlib\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u548c\u5904\u7406\u56fe\u50cf\uff0c\u800cPIL\u5219\u9002\u5408\u4e8e\u5bf9\u56fe\u50cf\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u5982\u526a\u88c1\u3001\u65cb\u8f6c\u548c\u8c03\u6574\u5927\u5c0f\u3002OpenCV\u5219\u66f4\u9002\u5408\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u9009\u62e9\u9002\u5408\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u7167\u7247\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u6b65\u9aa4\uff1f<\/strong><br \/>\u7ed8\u5236\u7167\u7247\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff0c\u52a0\u8f7d\u56fe\u50cf\u6587\u4ef6\uff0c\u8fdb\u884c\u5fc5\u8981\u7684\u5904\u7406\uff08\u5982\u8c03\u6574\u5927\u5c0f\u6216\u6ee4\u955c\u5e94\u7528\uff09\uff0c\u7136\u540e\u4f7f\u7528\u7ed8\u56fe\u5e93\u663e\u793a\u56fe\u50cf\u3002\u5177\u4f53\u6d41\u7a0b\u53ef\u4ee5\u662f\uff1a\u52a0\u8f7d\u56fe\u50cf\u6587\u4ef6\uff0c\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u8fdb\u884c\u4fee\u6539\uff0c\u6700\u540e\u901a\u8fc7\u7ed8\u56fe\u5e93\u5c55\u793a\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8\u4f7f\u7528Python\u7ed8\u5236\u7167\u7247\u7684\u6548\u7387\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528Numpy\u6570\u7ec4\u6765\u8fdb\u884c\u56fe\u50cf\u64cd\u4f5c\uff0c\u56e0\u4e3a\u8bb8\u591a\u56fe\u50cf\u5904\u7406\u5e93\u90fd\u4e0eNumpy\u517c\u5bb9\u3002\u5408\u7406\u5229\u7528\u56fe\u50cf\u7f13\u5b58\u548c\u5e76\u884c\u5904\u7406\u6280\u672f\u4e5f\u80fd\u663e\u8457\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u9884\u5148\u5b9a\u4e49\u7684\u51fd\u6570\u548c\u6a21\u5757\u6765\u91cd\u590d\u6267\u884c\u76f8\u540c\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u6709\u52a9\u4e8e\u52a0\u5feb\u5f00\u53d1\u548c\u6267\u884c\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u6982\u8ff0 \u8981\u7528Python\u753b\u51fa\u4e00\u5f20\u7167\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u3001OpenCV\u3001Matplotlib\u7b49\u5e93\uff0c\u8fd9\u4e9b\u5e93\u5404 [&hellip;]","protected":false},"author":3,"featured_media":1103448,"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\/1103427"}],"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=1103427"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103427\/revisions"}],"predecessor-version":[{"id":1103450,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103427\/revisions\/1103450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1103448"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1103427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1103427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1103427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}