{"id":1030130,"date":"2024-12-31T11:14:28","date_gmt":"2024-12-31T03:14:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1030130.html"},"modified":"2024-12-31T11:14:31","modified_gmt":"2024-12-31T03:14:31","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%9b%be%e7%89%87%e8%bd%ac%e5%8c%96%e4%b8%ba%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1030130.html","title":{"rendered":"python\u5982\u4f55\u5c06\u56fe\u7247\u8f6c\u5316\u4e3a\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/29884e5b-1c31-435f-8cf9-a773fec506ae.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u56fe\u7247\u8f6c\u5316\u4e3a\u77e9\u9635\" \/><\/p>\n<p><p> Python\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u548cNumPy\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u7ed3\u5408\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u548cNumPy\u5e93\uff0c\u56e0\u4e3a\u8fd9\u6837\u4e0d\u4ec5\u80fd\u7b80\u5316\u4ee3\u7801\uff0c\u8fd8\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u64cd\u4f5c\u3002<strong>\u9996\u5148\uff0c\u901a\u8fc7PIL\u5e93\u8bfb\u53d6\u56fe\u7247\uff0c\u7136\u540e\u901a\u8fc7NumPy\u5e93\u5c06\u56fe\u7247\u8f6c\u5316\u4e3a\u6570\u7ec4<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PIL\u548cNumPy<\/h3>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u8bb8\u591a\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PIL\u548cNumPy\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u56fe\u7247\u5e76\u8f6c\u6362\u4e3a\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528PIL\u548cNumPy\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\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>\u6253\u5f00\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff08\u53ef\u9009\uff09<\/strong><\/h2>\n<p>image = image.convert(&#39;L&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>image_matrix = np.array(image)<\/p>\n<p>print(image_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>Image.open()<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u7247\uff0c\u7136\u540e\u4f7f\u7528<code>convert(&#39;L&#39;)<\/code>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff08\u8fd9\u4e00\u884c\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u5f69\u8272\u56fe\u50cf\uff0c\u53ef\u4ee5\u8df3\u8fc7\u8fd9\u4e00\u884c\uff09\u3002\u6700\u540e\uff0c\u5c06\u56fe\u7247\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u5f97\u5230\u7684<code>image_matrix<\/code>\u5373\u4e3a\u56fe\u7247\u7684\u77e9\u9635\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OpenCV\u548cNumPy<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u51fd\u6570\u3002\u7ed3\u5408NumPy\u4f7f\u7528\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u56fe\u7247\u5230\u77e9\u9635\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86OpenCV\u548cNumPy\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u56fe\u7247\u5e76\u8f6c\u6362\u4e3a\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528OpenCV\u548cNumPy\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\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>\u8bfb\u53d6\u56fe\u7247<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>image_matrix = np.array(image)<\/p>\n<p>print(image_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>cv2.imread()<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>cv2.IMREAD_GRAYSCALE<\/code>\u5c06\u56fe\u7247\u8bfb\u53d6\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002\u6700\u540e\uff0c\u5c06\u56fe\u7247\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u5f97\u5230\u7684<code>image_matrix<\/code>\u5373\u4e3a\u56fe\u7247\u7684\u77e9\u9635\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u77e9\u9635\u64cd\u4f5c\u548c\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u77e9\u9635\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u77e9\u9635\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u77e9\u9635\u7684\u5f62\u72b6<\/p>\n<p>print(&quot;Shape:&quot;, image_matrix.shape)<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u5747\u503c<\/strong><\/h2>\n<p>mean_value = np.mean(image_matrix)<\/p>\n<p>print(&quot;Mean:&quot;, mean_value)<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_value = np.std(image_matrix)<\/p>\n<p>print(&quot;Standard Deviation:&quot;, std_value)<\/p>\n<h2><strong>\u77e9\u9635\u7684\u8f6c\u7f6e<\/strong><\/h2>\n<p>transpose_matrix = np.transpose(image_matrix)<\/p>\n<p>print(&quot;Transpose Matrix:&quot;, transpose_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u5904\u7406\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u56fe\u50cf\u7684\u4e8c\u503c\u5316<\/p>\n<p>threshold = 128<\/p>\n<p>binary_image = (image_matrix &gt; threshold) * 255<\/p>\n<h2><strong>\u56fe\u50cf\u7684\u8fb9\u7f18\u68c0\u6d4b\uff08\u4f7f\u7528Sobel\u7b97\u5b50\uff09<\/strong><\/h2>\n<p>sobel_x = cv2.Sobel(image_matrix, cv2.CV_64F, 1, 0, ksize=3)<\/p>\n<p>sobel_y = cv2.Sobel(image_matrix, cv2.CV_64F, 0, 1, ksize=3)<\/p>\n<p>edges = np.hypot(sobel_x, sobel_y)<\/p>\n<h2><strong>\u56fe\u50cf\u7684\u6a21\u7cca\u5904\u7406\uff08\u4f7f\u7528\u9ad8\u65af\u6a21\u7cca\uff09<\/strong><\/h2>\n<p>blurred_image = cv2.GaussianBlur(image_matrix, (5, 5), 0)<\/p>\n<h2><strong>\u663e\u793a\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Binary Image&#39;, binary_image)<\/p>\n<p>cv2.imshow(&#39;Edges&#39;, edges)<\/p>\n<p>cv2.imshow(&#39;Blurred Image&#39;, blurred_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>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7PIL\u3001OpenCV\u548cNumPy\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u77e9\u9635\u64cd\u4f5c\u548c\u56fe\u50cf\u5904\u7406\u3002<strong>\u8fd9\u4e0d\u4ec5\u7b80\u5316\u4e86\u56fe\u50cf\u5904\u7406\u7684\u6d41\u7a0b\uff0c\u8fd8\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8ba1\u7b97\u80fd\u529b<\/strong>\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u57fa\u672c\u7684\u77e9\u9635\u8fd0\u7b97\uff0c\u8fd8\u662f\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0cPython\u7684\u8fd9\u4e9b\u5e93\u90fd\u80fd\u591f\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u4f8b\u5982NumPy\u548cPIL\uff08Pillow\uff09\u3002\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport numpy as np\n\n# \u6253\u5f00\u56fe\u50cf\nimg = Image.open(&#39;image.jpg&#39;)\n\n# \u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\nimg_matrix = np.array(img)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u5c06\u56fe\u50cf\u52a0\u8f7d\u5e76\u8f6c\u5316\u4e3a\u4e00\u4e2a\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e2a\u50cf\u7d20\u7684RGB\u503c\u5c06\u4f5c\u4e3a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u77e9\u9635\u7684\u7ef4\u5ea6\u662f\u4ec0\u4e48\u6837\u7684\uff1f<\/strong><br \/>\u8f6c\u6362\u540e\u7684\u77e9\u9635\u7ef4\u5ea6\u901a\u5e38\u53d6\u51b3\u4e8e\u56fe\u50cf\u7684\u5927\u5c0f\u548c\u989c\u8272\u6a21\u5f0f\u3002\u5982\u679c\u662fRGB\u56fe\u50cf\uff0c\u8f6c\u6362\u540e\u7684\u77e9\u9635\u5c06\u662f\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\uff0c\u5f62\u72b6\u4e3a(\u9ad8\u5ea6, \u5bbd\u5ea6, 3)\uff0c\u5176\u4e2d3\u8868\u793a\u7ea2\u8272\u3001\u7eff\u8272\u548c\u84dd\u8272\u901a\u9053\u3002\u5982\u679c\u662f\u7070\u5ea6\u56fe\u50cf\uff0c\u77e9\u9635\u5c06\u662f\u4e8c\u7ef4\u7684\uff0c\u5f62\u72b6\u4e3a(\u9ad8\u5ea6, \u5bbd\u5ea6)\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8f6c\u6362\u540e\u7684\u77e9\u9635\u6570\u636e\uff1f<\/strong><br \/>\u8f6c\u6362\u540e\u7684\u77e9\u9635\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u5e94\u7528\uff0c\u5305\u62ec\u56fe\u50cf\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3002\u53ef\u4ee5\u5bf9\u77e9\u9635\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406\u3001\u5207\u7247\u63d0\u53d6\u7279\u5b9a\u533a\u57df\u3001\u5e94\u7528\u6ee4\u6ce2\u5668\u3001\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\u7b49\u3002\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e2e\u52a9\u5728\u540e\u7eed\u7684\u5206\u6790\u4e2d\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u77e9\u9635\u91cd\u65b0\u8f6c\u6362\u4e3a\u56fe\u7247\uff1f<\/strong><br \/>\u5c06\u77e9\u9635\u91cd\u65b0\u8f6c\u6362\u4e3a\u56fe\u7247\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u4e2d\u7684<code>Image.fromarray()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">img_back = Image.fromarray(img_matrix)\nimg_back.save(&#39;output_image.jpg&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u5904\u7406\u540e\u7684\u77e9\u9635\u4fdd\u5b58\u4e3a\u4e00\u5f20\u65b0\u7684\u56fe\u50cf\u3002\u786e\u4fdd\u5728\u8f6c\u6362\u4e4b\u524d\u5bf9\u77e9\u9635\u7684\u6570\u636e\u7c7b\u578b\u548c\u8303\u56f4\u8fdb\u884c\u9002\u5f53\u5904\u7406\uff0c\u4ee5\u4fdd\u8bc1\u751f\u6210\u7684\u56fe\u50cf\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u548cNumPy\u5e93\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1030139,"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\/1030130"}],"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=1030130"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030130\/revisions"}],"predecessor-version":[{"id":1030141,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030130\/revisions\/1030141"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1030139"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1030130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1030130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1030130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}