{"id":1179445,"date":"2025-01-15T18:23:49","date_gmt":"2025-01-15T10:23:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179445.html"},"modified":"2025-01-15T18:23:52","modified_gmt":"2025-01-15T10:23:52","slug":"python%e4%b8%ad%e7%81%b0%e5%ba%a6%e7%9b%b4%e6%96%b9%e5%9b%be%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179445.html","title":{"rendered":"python\u4e2d\u7070\u5ea6\u76f4\u65b9\u56fe\u5982\u4f55\u4fdd\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113833\/fcd27d3c-9cf5-45dd-98b1-82ee653b7aed.webp\" alt=\"python\u4e2d\u7070\u5ea6\u76f4\u65b9\u56fe\u5982\u4f55\u4fdd\u5b58\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7070\u5ea6\u76f4\u65b9\u56fe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4fdd\u5b58\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u751f\u6210\u56fe\u50cf\u5e76\u4fdd\u5b58\u3001\u4f7f\u7528OpenCV\u5e93\u8ba1\u7b97\u548c\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u3001\u4ee5\u53ca\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u751f\u6210\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u3002Matplotlib\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u80fd\u591f\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u751f\u6210\u7684\u56fe\u8868\u4fdd\u5b58\u4e3a\u591a\u79cd\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Matplotlib\u751f\u6210\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>hist = cv2.calcHist([image], [0], None, [256], [0, 256])<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.title(&quot;Grayscale Histogram&quot;)<\/p>\n<p>plt.xlabel(&quot;Bins&quot;)<\/p>\n<p>plt.ylabel(&quot;# of Pixels&quot;)<\/p>\n<p>plt.plot(hist)<\/p>\n<p>plt.xlim([0, 256])<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;histogram.png&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>cv2.calcHist<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c<code>plt.plot<\/code>\u51fd\u6570\u7528\u4e8e\u7ed8\u5236\u76f4\u65b9\u56fe\uff0c\u800c<code>plt.savefig<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u751f\u6210\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u8ba1\u7b97\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e\u3002OpenCV\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528OpenCV\u8ba1\u7b97\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e\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\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>hist = cv2.calcHist([image], [0], None, [256], [0, 256])<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3atxt\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data.txt&#39;, hist)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>cv2.calcHist<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u800c<code>np.savetxt<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u53ef\u4ee5\u5c06\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u8fd9\u5bf9\u4e8e\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\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\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>hist = cv2.calcHist([image], [0], None, [256], [0, 256])<\/p>\n<h2><strong>\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3acsv\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data.csv&#39;, hist, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>cv2.calcHist<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u800c<code>np.savetxt<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3acsv\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001MATPLOTLIB\u751f\u6210\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u4e0d\u4ec5\u652f\u6301\u7b80\u5355\u7684\u56fe\u8868\u7ed8\u5236\uff0c\u8fd8\u53ef\u4ee5\u751f\u6210\u590d\u6742\u7684\u53ef\u89c6\u5316\u56fe\u8868\u3002\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0cMatplotlib\u5e38\u7528\u4e8e\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\u53ca\u5176\u76f8\u5173\u7684\u56fe\u8868\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u5e76\u8f6c\u6362\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u56fe\u50cf\u5df2\u7ecf\u88ab\u6b63\u786e\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002\u5982\u679c\u56fe\u50cf\u662f\u5f69\u8272\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/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;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u5e93\u7684<code>calcHist<\/code>\u51fd\u6570\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u50cf\u7d20\u503c\u5206\u5e03\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u5e76\u4fdd\u5b58\u76f4\u65b9\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.title(&quot;Grayscale Histogram&quot;)<\/p>\n<p>plt.xlabel(&quot;Bins&quot;)<\/p>\n<p>plt.ylabel(&quot;# of Pixels&quot;)<\/p>\n<p>plt.plot(hist)<\/p>\n<p>plt.xlim([0, 256])<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;histogram.png&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c<code>plt.savefig<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u7ed8\u5236\u7684\u76f4\u65b9\u56fe\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u683c\u5f0f\uff0c\u4f8b\u5982<code>histogram.png<\/code>\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001OPENCV\u8ba1\u7b97\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e<\/h3>\n<\/p>\n<p><p>OpenCV\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u548c\u4fdd\u5b58\u56fe\u50cf\u7684\u76f4\u65b9\u56fe\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u5e76\u8f6c\u6362\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4e0e\u524d\u9762\u7684\u6b65\u9aa4\u7c7b\u4f3c\uff0c\u9996\u5148\u9700\u8981\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/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;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u5e93\u7684<code>calcHist<\/code>\u51fd\u6570\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u7684<code>savetxt<\/code>\u51fd\u6570\u5c06\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\u6216CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3atxt\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data.txt&#39;, hist)<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3acsv\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data.csv&#39;, hist, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c<code>np.savetxt<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u8ba1\u7b97\u5f97\u5230\u7684\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u6216csv\u6587\u4ef6\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3a\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5c06\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u5bf9\u4e8e\u540e\u7eed\u5206\u6790\u548c\u5904\u7406\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u5e76\u8f6c\u6362\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4e0e\u524d\u9762\u7684\u6b65\u9aa4\u7c7b\u4f3c\uff0c\u9996\u5148\u9700\u8981\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/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;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u5e93\u7684<code>calcHist<\/code>\u51fd\u6570\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u7684<code>savetxt<\/code>\u51fd\u6570\u5c06\u7070\u5ea6\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3acsv\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data.csv&#39;, hist, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c<code>np.savetxt<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u8ba1\u7b97\u5f97\u5230\u7684\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6e05\u6670\u4e14\u6613\u4e8e\u8bfb\u53d6\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u7ed3\u5408\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u5c55\u793a\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b\u3002\u8be5\u5b9e\u4f8b\u5c06\u8bfb\u53d6\u56fe\u50cf\uff0c\u8ba1\u7b97\u5e76\u7ed8\u5236\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u540c\u65f6\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6\u5f69\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\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>\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.title(&quot;Grayscale Histogram&quot;)<\/p>\n<p>plt.xlabel(&quot;Bins&quot;)<\/p>\n<p>plt.ylabel(&quot;# of Pixels&quot;)<\/p>\n<p>plt.plot(hist)<\/p>\n<p>plt.xlim([0, 256])<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;histogram.png&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3atxt\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data.txt&#39;, hist)<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3acsv\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data.csv&#39;, hist, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8bfb\u53d6\u5e76\u8f6c\u6362\u56fe\u50cf\uff0c\u7136\u540e\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u63a5\u7740\u7ed8\u5236\u5e76\u4fdd\u5b58\u76f4\u65b9\u56fe\u56fe\u50cf\uff0c\u6700\u540e\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3atxt\u548ccsv\u6587\u4ef6\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u4f7f\u7528Matplotlib\u548cOpenCV\u5e93\u6765\u5b8c\u6210\u7070\u5ea6\u76f4\u65b9\u56fe\u7684\u751f\u6210\u548c\u4fdd\u5b58\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u76f8\u5173\u6280\u672f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u76f8\u5173\u6280\u672f\u548c\u5de5\u5177\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u548c\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6280\u672f\u548c\u5de5\u5177\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528PIL\u5e93<\/h4>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u867d\u7136\u5b83\u5728\u529f\u80fd\u4e0a\u4e0d\u5982OpenCV\uff0c\u4f46\u5728\u4e00\u4e9b\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u4e2d\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PIL\u5e93\u751f\u6210\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;image.jpg&#39;).convert(&#39;L&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>hist = image.histogram()<\/p>\n<h2><strong>\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>hist = np.array(hist).reshape(256, 1)<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.title(&quot;Grayscale Histogram&quot;)<\/p>\n<p>plt.xlabel(&quot;Bins&quot;)<\/p>\n<p>plt.ylabel(&quot;# of Pixels&quot;)<\/p>\n<p>plt.plot(hist)<\/p>\n<p>plt.xlim([0, 256])<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;histogram_pil.png&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3acsv\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;histogram_data_pil.csv&#39;, hist, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PIL\u5e93\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u548c\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u3002<\/strong><\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Scikit-Image\u5e93<\/h4>\n<\/p>\n<p><p>Scikit-Image\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Scikit-Image\u5e93\u751f\u6210\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import io, color, exposure<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = io.imread(&#39;image.jpg&#39;)<\/p>\n<p>gray_image = color.rgb2gray(image)<\/p>\n<h2><strong>\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>hist, bins = exposure.histogram(gray_image, nbins=256)<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.title(&quot;Grayscale Histogram&quot;)<\/p>\n<p>plt.xlabel(&quot;Bins&quot;)<\/p>\n<p>plt.ylabel(&quot;# of Pixels&quot;)<\/p>\n<p>plt.plot(bins, hist)<\/p>\n<p>plt.xlim([0, 1])<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;histogram_skimage.png&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u4e3acsv\u6587\u4ef6<\/strong><\/h2>\n<p>hist_data = np.column_stack((bins, hist))<\/p>\n<p>np.savetxt(&#39;histogram_data_skimage.csv&#39;, hist_data, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Scikit-Image\u5e93\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u548c\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u5e76\u4fdd\u5b58\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u751f\u6210\u56fe\u50cf\u5e76\u4fdd\u5b58\u3001\u4f7f\u7528OpenCV\u5e93\u8ba1\u7b97\u548c\u4fdd\u5b58\u76f4\u65b9\u56fe\u6570\u636e\u3001\u4ee5\u53ca\u5c06\u76f4\u65b9\u56fe\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5176\u4ed6\u76f8\u5173\u6280\u672f\u548c\u5de5\u5177\uff0c\u5982PIL\u548cScikit-Image\u5e93\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u7070\u5ea6\u76f4\u65b9\u56fe\u7684\u751f\u6210\u548c\u4fdd\u5b58\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u7070\u5ea6\u76f4\u65b9\u56fe\uff1f<\/strong><br \/>\u8981\u751f\u6210\u7070\u5ea6\u76f4\u65b9\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684Matplotlib\u548cOpenCV\u5e93\u3002\u9996\u5148\uff0c\u4f7f\u7528OpenCV\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002\u63a5\u7740\uff0c\u4f7f\u7528Matplotlib\u7684<code>hist()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5e76\u7ed8\u5236\u7070\u5ea6\u76f4\u65b9\u56fe\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\nimport matplotlib.pyplot as plt\n\n# \u8bfb\u53d6\u56fe\u50cf\nimage = cv2.imread(&#39;image.jpg&#39;, cv2.IMREAD_GRAYSCALE)\n# \u8ba1\u7b97\u76f4\u65b9\u56fe\nhistogram, bins = np.histogram(image.flatten(), 256, [0, 256])\n# \u7ed8\u5236\u76f4\u65b9\u56fe\nplt.figure(figsize=(10, 5))\nplt.title(&#39;Gray Histogram&#39;)\nplt.xlabel(&#39;Pixel Value&#39;)\nplt.ylabel(&#39;Frequency&#39;)\nplt.xlim([0, 256])\nplt.plot(histogram)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u4fdd\u5b58\u751f\u6210\u7684\u7070\u5ea6\u76f4\u65b9\u56fe\u9700\u8981\u54ea\u4e9b\u6b65\u9aa4\uff1f<\/strong><br \/>\u5728\u751f\u6210\u7070\u5ea6\u76f4\u65b9\u56fe\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>savefig()<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u8c03\u7528<code>show()<\/code>\u51fd\u6570\u4e4b\u524d\u6267\u884c\u4fdd\u5b58\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u5c06\u76f4\u65b9\u56fe\u56fe\u50cf\u6b63\u786e\u4fdd\u5b58\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">plt.savefig(&#39;gray_histogram.png&#39;)\n<\/code><\/pre>\n<p><strong>\u6709\u4ec0\u4e48\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u66f4\u597d\u5730\u5206\u6790\u7070\u5ea6\u76f4\u65b9\u56fe\uff1f<\/strong><br \/>\u9664\u4e86\u57fa\u672c\u7684\u7070\u5ea6\u76f4\u65b9\u56fe\u7ed8\u5236\uff0c\u8bb8\u591a\u56fe\u50cf\u5904\u7406\u5de5\u5177\u548c\u5e93\u90fd\u80fd\u63d0\u4f9b\u66f4\u9ad8\u7ea7\u7684\u5206\u6790\u529f\u80fd\u3002\u4f8b\u5982\uff0cOpenCV\u5177\u6709\u591a\u79cd\u56fe\u50cf\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8c03\u6574\u76f4\u65b9\u56fe\u7684\u5bf9\u6bd4\u5ea6\u548c\u4eae\u5ea6\u3002\u6b64\u5916\uff0c\u4f7f\u7528SciPy\u5e93\u4e2d\u7684\u56fe\u50cf\u5904\u7406\u6a21\u5757\uff0c\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u56fe\u50cf\u5206\u6790\u548c\u5904\u7406\uff0c\u83b7\u53d6\u66f4\u6df1\u5165\u7684\u76f4\u65b9\u56fe\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7070\u5ea6\u76f4\u65b9\u56fe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4fdd\u5b58\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u751f\u6210\u56fe\u50cf\u5e76\u4fdd\u5b58\u3001\u4f7f\u7528OpenC [&hellip;]","protected":false},"author":3,"featured_media":1179453,"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\/1179445"}],"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=1179445"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179445\/revisions"}],"predecessor-version":[{"id":1179455,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179445\/revisions\/1179455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179453"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}