{"id":368,"date":"2022-09-03T15:41:39","date_gmt":"2022-09-03T07:41:39","guid":{"rendered":"https:\/\/blog.cirno.fun\/?p=368"},"modified":"2022-09-03T15:41:39","modified_gmt":"2022-09-03T07:41:39","slug":"1","status":"publish","type":"post","link":"https:\/\/blog.sssn.tech\/?p=368","title":{"rendered":"1"},"content":{"rendered":"<pre><code class=\"language-python\">!mkdir \/home\/aistudio\/external-libraries\n!pip install beautifulsoup4 -t \/home\/aistudio\/external-libraries<\/code><\/pre>\n<pre><code>mkdir: cannot create directory \u2018\/home\/aistudio\/external-libraries\u2019: File exists\nLooking in indexes: https:\/\/mirror.baidu.com\/pypi\/simple\/\nCollecting beautifulsoup4\n\u001b[?25l  Downloading https:\/\/mirror.baidu.com\/pypi\/packages\/d1\/41\/e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae\/beautifulsoup4-4.9.3-py3-none-any.whl (115kB)\n\u001b[K     |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 122kB 16.5MB\/s eta 0:00:01\n\u001b[?25hCollecting soupsieve>1.2; python_version >= \"3.0\" (from beautifulsoup4)\n  Downloading https:\/\/mirror.baidu.com\/pypi\/packages\/36\/69\/d82d04022f02733bf9a72bc3b96332d360c0c5307096d76f6bb7489f7e57\/soupsieve-2.2.1-py3-none-any.whl\nInstalling collected packages: soupsieve, beautifulsoup4\nSuccessfully installed beautifulsoup4-4.9.3 soupsieve-2.2.1\n\u001b[33mWARNING: Target directory \/home\/aistudio\/external-libraries\/bs4 already exists. Specify --upgrade to force replacement.\u001b[0m\n\u001b[33mWARNING: Target directory \/home\/aistudio\/external-libraries\/soupsieve already exists. Specify --upgrade to force replacement.\u001b[0m\n\u001b[33mWARNING: Target directory \/home\/aistudio\/external-libraries\/soupsieve-2.2.1.dist-info already exists. Specify --upgrade to force replacement.\u001b[0m\n\u001b[33mWARNING: Target directory \/home\/aistudio\/external-libraries\/beautifulsoup4-4.9.3.dist-info already exists. Specify --upgrade to force replacement.\u001b[0m<\/code><\/pre>\n<pre><code class=\"language-python\"># \u540c\u65f6\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801, \u8fd9\u6837\u6bcf\u6b21\u73af\u5883(kernel)\u542f\u52a8\u7684\u65f6\u5019\u53ea\u8981\u8fd0\u884c\u4e0b\u65b9\u4ee3\u7801\u5373\u53ef:\n\nimport sys\nsys.path.append(&#039;\/home\/aistudio\/external-libraries&#039;)<\/code><\/pre>\n<p>\u8bf7\u70b9\u51fb<a href=\"https:\/\/ai.baidu.com\/docs#\/AIStudio_Project_Notebook\/a38e5576\">\u6b64\u5904<\/a>\u67e5\u770b\u672c\u73af\u5883\u57fa\u672c\u7528\u6cd5.  <br \/>\nPlease click <a href=\"https:\/\/ai.baidu.com\/docs#\/AIStudio_Project_Notebook\/a38e5576\">here <\/a> for more detailed instructions. <\/p>\n<p>\u7070\u5ea6\u76f4\u65b9\u56fe\u6982\u62ec\u4e86\u56fe\u50cf\u7684\u7070\u5ea6\u7ea7\u4fe1\u606f\uff0c\u7b80\u5355\u7684\u6765\u8bf4\u5c31\u662f\u6bcf\u4e2a\u7070\u5ea6\u7ea7\u56fe\u50cf\u4e2d\u7684\u50cf\u7d20\u4e2a\u6570\u4ee5\u53ca\u5360\u6709\u7387\uff0c\u521b\u5efa\u76f4\u65b9\u56fe\u65e0\u5916\u4e4e\u4e24\u4e2a\u6b65\u9aa4\uff0c\u7edf\u8ba1\u76f4\u65b9\u56fe\u6570\u636e\uff0c\u518d\u7528\u7ed8\u56fe\u5e93\u7ed8\u5236\u76f4\u65b9\u56fe\u3002<\/p>\n<p>\u7edf\u8ba1\u76f4\u65b9\u56fe\u6570\u636e<br \/>\n\u2003\u9996\u5148\u8981\u7a0d\u5fae\u7406\u89e3\u4e00\u4e9b\u4e0e\u51fd\u6570\u76f8\u5173\u7684\u672f\u8bed\uff0c\u65b9\u4fbf\u7406\u89e3\u5176\u5728python3\u5e93\u4e2d\u7684\u5e94\u7528\u548c\u5904\u7406<br \/>\n\u2003BINS: \u5728\u4e0a\u9762\u7684\u76f4\u65b9\u56fe\u5f53\u4e2d\uff0c\u5982\u679c\u50cf\u7d20\u503c\u662f0\u5230255\uff0c\u5219\u9700\u8981256\u4e2a\u503c\u6765\u663e\u793a\u76f4 \u65b9\u56fe\u3002\u4f46\u662f\uff0c\u5982\u679c\u4e0d\u9700\u8981\u77e5\u9053\u6bcf\u4e2a\u50cf\u7d20\u503c\u7684\u50cf\u7d20\u6570\u76ee\uff0c\u53ea\u60f3\u77e5\u9053\u4e24\u4e2a\u50cf\u7d20\u503c\u4e4b\u95f4\u7684\u50cf\u7d20\u70b9\u6570\u76ee\u600e\u4e48\u529e\uff1f\u4f8b\u5982\uff0c\u60f3\u77e5\u9053\u50cf\u7d20\u503c\u57280\u523015\u4e4b\u95f4\u7684\u50cf\u7d20\u70b9\u6570\u76ee\uff0c\u7136\u540e\u662f16\u523031\u3002\u3002\u3002240\u5230255\u3002\u53ef\u4ee5\u5c06256\u4e2a\u503c\u5206\u621016\u4efd\uff0c\u6bcf\u4efd\u8ba1\u7b97\u7efc\u5408\u3002\u6bcf\u4e2a\u5206\u6210\u7684\u5c0f\u7ec4\u5c31\u662f\u4e00\u4e2aBIN\uff08\u7bb1\uff09\u3002\u5728opencv\u4e2d\u4f7f\u7528histSize\u8868\u793aBINS\u3002<br \/>\n\u2003DIMS: \u6570\u636e\u7684\u53c2\u6570\u6570\u76ee\u3002\u5f53\u524d\u4f8b\u5b50\u5f53\u4e2d\uff0c\u5bf9\u6536\u96c6\u5230\u7684\u6570\u636e\u53ea\u8003\u8651\u7070\u5ea6\u503c\uff0c\u6240\u4ee5\u8be5\u503c\u4e3a1\u3002<br \/>\n\u2003RANGE: \u7070\u5ea6\u503c\u8303\u56f4\uff0c\u901a\u5e38\u662f[0,256]\uff0c\u4e5f\u5c31\u662f\u7070\u5ea6\u6240\u6709\u7684\u53d6\u503c\u8303\u56f4\u3002<br \/>\n\u7edf\u8ba1\u76f4\u65b9\u56fe\u540c\u6837\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528opencv\u7edf\u8ba1\u76f4\u65b9\u56fe\uff0c\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<p>cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])<\/p>\n<pre><code class=\"language-python\">import cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\nimg = cv2.imread(&#039;data\/nezha.jpeg&#039;,1)\n# img_np = np.array(img)\nplt.hist(img.reshape([-1]),256,[0,256]);\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"output_5_0.png\" alt=\"png\" \/><\/p>\n<p>cv2.calcHist([images], [channels], mask, histSize, ranges[, hist[, accumulate ]])<\/p>\n<ul>\n<li>imaes:\u8f93\u5165\u7684\u56fe\u50cf<\/li>\n<li>channels:\u9009\u62e9\u56fe\u50cf\u7684\u901a\u9053<\/li>\n<li>mask:\u63a9\u819c\uff0c\u662f\u4e00\u4e2a\u5927\u5c0f\u548cimage\u4e00\u6837\u7684np\u6570\u7ec4\uff0c\u5176\u4e2d\u628a\u9700\u8981\u5904\u7406\u7684\u90e8\u5206\u6307\u5b9a\u4e3a1\uff0c\u4e0d\u9700\u8981\u5904\u7406\u7684\u90e8\u5206\u6307\u5b9a\u4e3a0\uff0c\u4e00\u822c\u8bbe\u7f6e\u4e3aNone\uff0c\u8868\u793a\u5904\u7406\u6574\u5e45\u56fe\u50cf<\/li>\n<li>histSize:\u4f7f\u7528\u591a\u5c11\u4e2abin(\u67f1\u5b50)\uff0c\u4e00\u822c\u4e3a256<\/li>\n<li>ranges:\u50cf\u7d20\u503c\u7684\u8303\u56f4\uff0c\u4e00\u822c\u4e3a[0,255]\u8868\u793a0~255<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import cv2\n# import numpy as np\nfrom matplotlib import pyplot as plt\nimg = cv2.imread(&#039;data\/nezha.jpeg&#039;,0)\nhistr = cv2.calcHist([img],[0],None,[256],[0,256]) #hist\u662f\u4e00\u4e2ashape\u4e3a(256,1)\u7684\u6570\u7ec4\uff0c\u8868\u793a0-255\u6bcf\u4e2a\u50cf\u7d20\u503c\u5bf9\u5e94\u7684\u50cf\u7d20\u4e2a\u6570\uff0c\u4e0b\u6807\u5373\u4e3a\u76f8\u5e94\u7684\u50cf\u7d20\u503c\nplt.plot(histr,color = &#039;b&#039;)\nplt.xlim([0,256])\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"output_7_0.png\" alt=\"png\" \/><\/p>\n<pre><code class=\"language-python\">import cv2\nfrom matplotlib import pyplot as plt\nimg = cv2.imread(&#039;data\/nezha.jpeg&#039;,1)\ncolor = (&#039;b&#039;,&#039;g&#039;,&#039;r&#039;)\nfor i,col in enumerate(color):\n    histr = cv2.calcHist([img],[i],None,[256],[0,256])\n    #hist\u662f\u4e00\u4e2ashape\u4e3a(256,1)\u7684\u6570\u7ec4\uff0c\u8868\u793a0-255\u6bcf\u4e2a\u50cf\u7d20\u503c\u5bf9\u5e94\u7684\u50cf\u7d20\u4e2a\u6570\uff0c\u4e0b\u6807\u5373\u4e3a\u76f8\u5e94\u7684\u50cf\u7d20\u503c\n    plt.plot(histr,color = col)\n    plt.xlim([0,256])\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"output_8_0.png\" alt=\"png\" \/><\/p>\n<pre><code class=\"language-python\">import sys\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\n\ndef main():\n    img=cv2.imread(&#039;data\/nezha.jpeg&#039;,0)\n    #\u5f97\u5230\u8ba1\u7b97\u7070\u5ea6\u76f4\u65b9\u56fe\u7684\u503c\n    n = np.array(img)\n    xy=xygray(img)\n\n    #\u753b\u51fa\u7070\u5ea6\u76f4\u65b9\u56fe\n    x_range=range(256)\n    plt.plot(x_range,xy,&quot;r&quot;,linewidth=2,c=&#039;black&#039;)\n    #\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\n    y_maxValue=np.max(xy)\n    plt.axis([0,255,0,y_maxValue])\n    #\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\n    plt.xlabel(&#039;gray Level&#039;)\n    plt.ylabel(&quot;number of pixels&quot;)\n    plt.show()\n\ndef xygray(img):\n    #\u5f97\u5230\u9ad8\u548c\u5bbd\n    rows,cols=img.shape\n    print(img.shape)\n    #\u5b58\u50a8\u7070\u5ea6\u76f4\u65b9\u56fe\n    xy=np.zeros([256],np.uint64)\n    for r in range(rows):\n        for c in range(cols):\n            xy[img[r][c]] += 1\n    #\u8fd4\u56de\u4e00\u7ef4ndarry\n    print(xy.sum())\n    return xy\n\nmain()<\/code><\/pre>\n<pre><code>(442, 700)\n309400<\/code><\/pre>\n<p><img decoding=\"async\" src=\"output_9_1.png\" alt=\"png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>!mkdir \/home\/aistudio\/external-libraries [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-368","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=368"}],"version-history":[{"count":0,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/368\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}