{"id":1809,"date":"2018-09-17T23:27:39","date_gmt":"2018-09-17T14:27:39","guid":{"rendered":"https:\/\/www.dogrow.net\/python\/?p=1809"},"modified":"2025-06-25T16:12:32","modified_gmt":"2025-06-25T07:12:32","slug":"blog99","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/python\/blog99\/","title":{"rendered":"(99) OpenCV #4 : \u30ac\u30f3\u30de\u88dc\u6b63\u3067\u753b\u50cf\u3092\u898b\u3084\u3059\u304f\u8abf\u6574"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>Python\u3067 OpenCV\u306e\u7b2c 4\u56de\u76ee\u3001\u4eca\u56de\u306f OpenCV\u306e Look up table \u3092\u4f7f\u3063\u3066\u753b\u50cf\u306e\u30ac\u30f3\u30de\u88dc\u6b63\uff08gamma correction\uff09\u3092\u3084\u3063\u3066\u307f\u308b\u3002<\/p>\n<h1 class=\"my_h\">2. \u30ac\u30f3\u30de\u88dc\u6b63\u3068\u306f<\/h1>\n<p>\u30ac\u30f3\u30de\u88dc\u6b63\uff08gamma correction\uff09\u3068\u306f\u3001\u4ee5\u4e0b\u306e\u5f0f\u3067\u5165\u529b\u5024\u306b\u5bfe\u3059\u308b\u51fa\u529b\u5024\u3092\u5f97\u308b\u3053\u3068\u3002<br \/>\n\u3053\u308c\u306b\u3088\u308a\u753b\u50cf\u306e\u8f1d\u5ea6\u3092\u6240\u671b\u306e\u72b6\u614b\u306b\u8abf\u6574\u3059\u308b\u3053\u3068\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/001-1.png\" alt=\"\" \/><\/p>\n<p>\u307e\u305a\u306f\u3001\u5165\u529b\u5024\uff08I\uff09\u3068\u51fa\u529b\u5024\uff08O\uff09\u306e\u95a2\u4fc2\u3092\u30b0\u30e9\u30d5\u4e0a\u3067\u898b\u3066\u307f\u308b\u3002<br \/>\n\u6298\u89d2 Python\u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067 <span class=\"my_fc_deeppink\">matplotlib<\/span> \u3067\u30b0\u30e9\u30d5\u3092\u8868\u793a\u3057\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\nary_gamma = np.array(&#x5B;0.1, 0.2, 0.4, 0.67, 1, 1.5, 2.5, 5, 10, 25])\r\n\r\nfor var_gamma in ary_gamma:\r\n    var_px = np.empty(256, np.uint8)\r\n    for i in range(256):\r\n        var_px&#x5B;i] = np.clip(pow(i \/ 255.0, var_gamma) * 255.0, 0, 255)\r\n    plt.plot(var_px, label=str(var_gamma))\r\n\r\nplt.legend()\r\nplt.xlabel(&quot;INPUT&quot;)\r\nplt.ylabel(&quot;OUTPUT&quot;)\r\nplt.show()\r\n<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30b0\u30e9\u30d5\u304c\u8868\u793a\u3055\u308c\u308b\u3002<br \/>\n\u7dda\u3054\u3068\u306b\u8272\u3092\u6307\u5b9a\u305b\u305a\u3068\u3082\u81ea\u52d5\u7684\u306b\u8272\u5206\u3051\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u4fbf\u5229\u3060\u3002<\/p>\n<p>\u03b3 \uff1d 1 \u3067\u3042\u308c\u3070\u3001\u51fa\u529b\u5024\uff1d\u5165\u529b\u5024\u306e\u30ea\u30cb\u30a2\u306a\u95a2\u4fc2\u306b\u306a\u308b\u306e\u3067\u753b\u50cf\u306f\u4f55\u3082\u5909\u308f\u3089\u306a\u3044\u3002<br \/>\n\u03b3 \uff1c 1 \u3067\u3042\u308c\u3070\u3001\u6697\u3044\u90e8\u5206\u306e\u8f1d\u5ea6\u5909\u5316\u304c\u5f37\u8abf\u3055\u308c\u308b\u3002<br \/>\n\u03b3 \uff1e 1 \u3067\u3042\u308c\u3070\u3001\u660e\u308b\u3044\u90e8\u5206\u306e\u8f1d\u5ea6\u5909\u5316\u304c\u5f37\u8abf\u3055\u308c\u308b\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/002-1.png\" alt=\"\" \/><\/p>\n<h1 class=\"my_h\">3. \u3084\u3063\u3066\u307f\u308b<\/h1>\n<h2 class=\"my_h\">(1) \u5fc5\u8981\u6700\u5c0f\u9650\u306e\u6a5f\u80fd<\/h2>\n<p>\u307e\u305a\u306f\u4e00\u5207\u306e\u88c5\u98fe\u3084\u5229\u4fbf\u6027\u306a\u3069\u3092\u8003\u616e\u305b\u305a\u3001\u611a\u76f4\u306b\u30ac\u30f3\u30de\u88dc\u6b63\u6a5f\u80fd\u3060\u3051\u3092\u5b9f\u884c\u3057\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport cv2\r\nimport numpy as np\r\n\r\n# \u30ac\u30f3\u30de\u5024\u3092\u6c7a\u3081\u308b\u3002\r\ngamma = 0.8\r\n\r\n# \u51e6\u7406\u5bfe\u8c61\u306e\u753b\u50cf\u3092\u30ed\u30fc\u30c9\r\nimgS = cv2.imread(&quot;img001.png&quot;)\r\n\r\n# \u30ac\u30f3\u30de\u5024\u3092\u4f7f\u3063\u3066 Look up table\u3092\u4f5c\u6210\r\nlookUpTable = np.empty((1,256), np.uint8)\r\nfor i in range(256):\r\n    lookUpTable&#x5B;0,i] = np.clip(pow(i \/ 255.0, gamma) * 255.0, 0, 255)\r\n\r\n# Look up table\u3092\u4f7f\u3063\u3066\u753b\u50cf\u306e\u8f1d\u5ea6\u5024\u3092\u5909\u66f4\r\nimgA = cv2.LUT(imgS, lookUpTable)\r\n\r\n# \u8868\u793a\u5b9f\u884c\r\ncv2.namedWindow(&#039;image&#039;, cv2.WINDOW_AUTOSIZE)\r\ncv2.imshow(&#039;image&#039;, imgA)\r\ncv2.waitKey()\r\n<\/pre>\n<p>\u30ac\u30f3\u30de\u5024\uff1d0.8\u3067\u5b9f\u884c\u3057\u305f\u3068\u3053\u308d\u3001\u6697\u3044\u90e8\u5206\u304c\u5c11\u3057\u3060\u3051\u898b\u3084\u3059\u304f\u306a\u3063\u305f\u304b\uff1f<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/src_dst.png\" alt=\"\" \/><\/p>\n\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"fluid\"\n     data-ad-layout-key=\"-fb+5w+4e-db+86\"\n     data-ad-client=\"ca-pub-0629873272207301\"\n     data-ad-slot=\"9529643655\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n<h2 class=\"my_h\">(2) \u5c11\u3057\u3060\u3051\u6a5f\u80fd\u62e1\u5f35<\/h2>\n<p>\u4e0a\u8a18(1)\u306e\u30b7\u30f3\u30d7\u30eb\u5b9f\u88c5\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u5bfe\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u4e8c\u3064\u306e\u6a5f\u80fd\u3092\u8ffd\u52a0\u5b9f\u88c5\u3057\u305f\u3002<br \/>\n\u8ffd\u52a0\u6a5f\u80fd#1 \uff1a \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u3001\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3002<br \/>\n\u8ffd\u52a0\u6a5f\u80fd#2 \uff1a \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u3001\u30ac\u30f3\u30de\u5024\u3092\u6307\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n###########################################################\r\n# \u30ac\u30f3\u30de\u88dc\u6b63\u30d7\u30ed\u30b0\u30e9\u30e0\r\nimport argparse\r\nimport cv2\r\nimport numpy as np\r\n\r\ndef main():\r\n    # \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u5f15\u6570\u300c\u03b3\u5024\u300d\u3092\u53d6\u5f97\r\n    parser = argparse.ArgumentParser()\r\n    parser.add_argument(&#039;-g&#039;,&#039;--gamma&#039;, required=True)\r\n    parser.add_argument(&#039;-f&#039;,&#039;--filepath&#039;,  required=True)\r\n    args = parser.parse_args()\r\n    # \u03b3\u88dc\u6b63\u5b9f\u884c\r\n    exec_gamma_correction( args.filepath, float(args.gamma) )\r\n\r\ndef exec_gamma_correction( filepath, gamma ):\r\n    # \u51e6\u7406\u5bfe\u8c61\u306e\u753b\u50cf\u3092\u30ed\u30fc\u30c9\r\n    imgS = cv2.imread(filepath)\r\n\r\n    # \u03b3\u5024\u3092\u4f7f\u3063\u3066 Look up table\u3092\u4f5c\u6210\r\n    lookUpTable = np.empty((1,256), np.uint8)\r\n    for i in range(256):\r\n        lookUpTable&#x5B;0,i] = np.clip(pow(i \/ 255.0, gamma) * 255.0, 0, 255)\r\n\r\n    # Look up table\u3092\u4f7f\u3063\u3066\u753b\u50cf\u306e\u8f1d\u5ea6\u5024\u3092\u5909\u66f4\r\n    imgA = cv2.LUT(imgS, lookUpTable)\r\n\r\n    # PIL\u3067\u8868\u793a\u7528\u753b\u50cf\u3092\u4f5c\u6210\r\n    from PIL import Image, ImageDraw, ImageFont\r\n    imgA_RGB = cv2.cvtColor(imgA, cv2.COLOR_BGR2RGB)\r\n    imgP = Image.fromarray(imgA_RGB)\r\n\r\n    # \u753b\u50cf\u306e\u5de6\u4e0a\u306b\u03b3\u5024\u306e\u8868\u793a\u3092\u57cb\u3081\u8fbc\u3080\r\n    obj_draw = ImageDraw.Draw(imgP)\r\n    obj_font = ImageFont.truetype(&quot;\/usr\/share\/fonts\/ipa\/ipagp.ttf&quot;, 40)\r\n    obj_draw.text((10, 10), &quot;\u03b3 = %.1f&quot; % gamma, fill=(255, 255, 255), font=obj_font)\r\n\r\n    # \u8868\u793a\u5b9f\u884c\r\n    imgP.show()\r\n\r\nif __name__==&quot;__main__&quot;: main()\r\n<\/pre>\n<p>Shell\u304b\u3089\u4ee5\u4e0b\u306e\u4e8c\u3064\u306e\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3059\u308b\u3002<br \/>\n-f : \u5bfe\u8c61\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9<br \/>\n-g : \u30ac\u30f3\u30de\u5024<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 gamma_correct.py -f img001.png -g 0.8\r\n<\/pre>\n<p>\u4ee5\u4e0b\u3001\u3044\u308d\u3044\u308d\u306a\u30ac\u30f3\u30de\u5024\u3067\u5b9f\u884c\u3057\u3066\u307f\u305f\u3002<\/p>\n<p>\u3067\u3082&#8230;<br \/>\niPhone\u306e\u81ea\u52d5\u88dc\u6b63\u6a5f\u80fd\u304c\u7d20\u6674\u3089\u3057\u3044\u306e\u304b\uff1f<br \/>\n\u30ac\u30f3\u30de\u5024\uff1d1 \u304c\u4e00\u756a\u304d\u308c\u3044\u306b\u898b\u3048\u308b\u306a\u3041<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/r04.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/r07.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/r10.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/r11.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/r15.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/r25.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/r50.png\" alt=\"\" \/><\/p>\n<h1 class=\"my_h\">4. \u53c2\u8003<\/h1>\n<p>\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002 m(_ _)m<br \/>\n<a href=\"https:\/\/docs.opencv.org\/master\/d3\/dc1\/tutorial_basic_linear_transform.html\" target=\"_blank\">https:\/\/docs.opencv.org\/master\/d3\/dc1\/tutorial_basic_linear_transform.html<\/a><\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 Python\u3067 OpenCV\u306e\u7b2c 4\u56de\u76ee\u3001\u4eca\u56de\u306f OpenCV\u306e Look up table \u3092\u4f7f\u3063\u3066\u753b\u50cf\u306e\u30ac\u30f3\u30de\u88dc\u6b63\uff08gamma correction\uff09\u3092\u3084\u3063\u3066\u307f\u308b\u3002 2. \u30ac\u30f3\u30de\u88dc\u6b63\u3068\u306f \u30ac\u30f3\u30de\u88dc\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/python\/blog99\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,36],"tags":[],"class_list":["post-1809","post","type-post","status-publish","format-standard","hentry","category-matplotlib","category-opencv"],"views":29416,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/comments?post=1809"}],"version-history":[{"count":18,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1809\/revisions"}],"predecessor-version":[{"id":3933,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1809\/revisions\/3933"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/media?parent=1809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/categories?post=1809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/tags?post=1809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}