﻿{"id":1331,"date":"2020-07-16T19:17:20","date_gmt":"2020-07-16T11:17:20","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=1331"},"modified":"2020-07-16T20:02:21","modified_gmt":"2020-07-16T12:02:21","slug":"python-opencv-image-blending","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/python-opencv-image-blending.html","title":{"rendered":"Python OpenCV \u5b9e\u73b0\u56fe\u50cf\u878d\u5408"},"content":{"rendered":"<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bbaPython OpenCV Image Blending\uff08\u56fe\u50cf\u878d\u5408\uff09\uff0c\u5982\u679c\u60a8\u5bf9\u4f7f\u7528Python\u7f16\u7a0b\u8bed\u8a00\u8fdb\u884cGUI\u5f00\u53d1\u611f\u5174\u8da3\uff0c\u8bf7\u67e5\u770b\u8fd9\u4e2a\uff08<a href=\"https:\/\/www.linuxmi.com\/python\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.linuxmi.com\/python<\/a>\uff09\u94fe\u63a5\u3002<\/p>\n<p>\u6839\u636e<a href=\"https:\/\/opencv-python-tutroals.readthedocs.io\/en\/latest\/py_tutorials\/py_core\/py_image_arithmetics\/py_image_arithmetics.html\" target=\"_blank\" rel=\"noopener noreferrer\">OpenCV\u6587\u6863<\/a>\uff0c \u56fe\u50cf\u878d\u5408\u4e5f\u662f\u56fe\u50cf\u53e0\u52a0\uff0c\u4f46\u662f\u4e3a\u56fe\u50cf\u8d4b\u4e88\u4e86\u4e0d\u540c\u7684\u6743\u91cd\uff0c\u4ece\u800c\u4f7f\u56fe\u50cf\u5177\u6709\u6df7\u5408\u6216\u900f\u660e\u7684\u611f\u89c9\u3002\u6839\u636e\u4ee5\u4e0b\u7b49\u5f0f\u53e0\u52a0\u56fe\u50cf\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1332\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/OpenCV-7.png\" alt=\"\" width=\"227\" height=\"18\" \/><\/p>\n<p>\u901a\u8fc7\u6539\u53d8\u00a0 <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1334\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/a.png\" alt=\"\" width=\"11\" height=\"8\" \/> \u4ece <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1333\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/01.png\" alt=\"\" width=\"46\" height=\"13\" \/>\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u56fe\u50cf\u5230\u53e6\u4e00\u4e2a\u56fe\u50cf\u4e4b\u95f4\u6267\u884c\u4e00\u4e2a\u5f88\u9177\u7684\u8f6c\u6362\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4f7f\u7528\u4e86\u4e24\u4e2a\u56fe\u50cf\uff0c\u5c06\u5b83\u4eec\u878d\u5408\u5728\u4e00\u8d77\u3002\u7b2c\u4e00\u5e45\u56fe\u50cf\u7684\u6743\u91cd\u4e3a0.7\uff0c\u7b2c\u4e8c\u5e45\u56fe\u50cf\u7684\u6743\u91cd\u4e3a0.3\u3002 cv2.addWeighted() \u5728\u56fe\u50cf\u4e0a\u5e94\u7528\u4ee5\u4e0b\u7b49\u5f0f\u3002\u56fe\u50cf\u6df7\u5408cv.addWeighted()\u662f\u4e00\u79cd\u56fe\u50cf\u52a0\u6743\u76f8\u52a0\u7684\u64cd\u4f5c\uff0c\u4e24\u5e45\u56fe\u50cf\u7684\u6743\u91cd\u53ef\u4ee5\u4e0d\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u4e00\u6837\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u56fe\u50cf\u878d\u5408\u7684\u5b8c\u6574\u793a\u4f8b<\/p>\n<div>\n<pre style=\"font-size: 1em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 1.2em 0px; line-height: 1.2em;\"><code style=\"font-size: 0.85em; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; background: #292824; white-space: pre; overflow-x: auto; color: #a6a28c; margin: 0px 0.15em; display: block; border-radius: 3px; -moz-text-size-adjust: none; border: #cccccc 1px solid; padding: 0.5em;\"><span style=\"color: #b854d4;\"><span style=\"background-color: #292824;\">import<\/span><\/span> cv2\r\n\r\nimg1 = cv2.imread(<span style=\"color: #60ac39;\"><span style=\"background-color: #292824;\">\"linuxmi.com.jpg\"<\/span><\/span>)\r\nimg2 = cv2.imread(<span style=\"color: #60ac39;\"><span style=\"background-color: #292824;\">\"linuxmi.jpg\"<\/span><\/span>)\r\n\r\ndist = cv2.addWeighted(img1, <span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0.75<\/span><\/span>, img2, <span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0.25<\/span><\/span>,<span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0<\/span><\/span>)\r\n\r\ncv2.imshow(<span style=\"color: #60ac39;\"><span style=\"background-color: #292824;\">\"www.linuxmi.com\"<\/span><\/span>, dist)\r\ncv2.waitKey(<span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0<\/span><\/span>)\r\ncv2.destroyAllWindows()\r\n<\/code><\/pre>\n<\/div>\n<p>\u6ce8\u610f\uff1a\u8fd9\u4e9b\u4ee3\u7801\u662f\u7528\u6765\u8bfb\u53d6\u56fe\u50cf\u7684\uff0c\u786e\u4fdd\u56fe\u50cf\u7684\u5927\u5c0f\u548c\u7c7b\u578b\u76f8\u540c\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u5728\u5de5\u4f5c\u76ee\u5f55\u4e2d\u6dfb\u52a0\u56fe\u50cf\u3002<\/p>\n<p>img1 = cv2.imread(&#8220;linuxmi.com.jpg&#8221;)<br \/>\nimg2 = cv2.imread(&#8220;linuxmi.jpg&#8221;)<\/p>\n<p>\u7528\u6b64\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728OpenCV\u4e2d\u5e94\u7528\u56fe\u50cf\u878d\u5408<\/p>\n<p>dist = cv2.addWeighted(img1, 0.75, img2, 0.25,0)<\/p>\n<p>\u8fd0\u884c\u5b8c\u6574\u7684\u4ee3\u7801\uff0c\u8fd9\u5c06\u662f\u7ed3\u679c<\/p>\n<p><a href=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335 size-large\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending-1024x576.png\" alt=\"\" width=\"731\" height=\"411\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending-1024x576.png 1024w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending-300x169.png 300w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending-768x432.png 768w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending-1536x864.png 1536w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending-430x242.png 430w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/image-blending.png 1920w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/a><\/p>\n<p><strong>\u7b2c2\u79cd\u65b9\u5f0f\uff1a\u901a\u8fc7\u4f4d\u8fd0\u7b97\u5b9e\u73b0\u56fe\u50cf\u7684\u53e0\u52a0<\/strong><\/p>\n<p>\u6211\u4eec\u5982\u4f55\u5c06\u8fd9\u4e2alogo\u52a0\u5230\u53f3\u8fb9\u56fe\u7247\u91cc\u5462\uff1f<\/p>\n<p>\u5982\u679c\u4f7f\u7528cv.add()\u90a3\u4e48\u989c\u8272\u80af\u5b9a\u52a0\u6df1 \u5982\u679c\u4f7f\u7528cv.addWeight()\u53c8\u4f1a\u51fa\u73b0\u900f\u660e\uff0c \u90a3\u600e\u4e48\u529e\u5462\uff1f<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4f4d\u8fd0\u7b97\uff0c\u7136\u540e\u8fd0\u7528\u63a9\u7801\u7684\u6027\u8d28\u5c31\u53ef\u4ee5\u5f97\u5230\u4e86\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u9996\u5148\u5c06logo\u7684\u989c\u8272\u7a7a\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u8f6c\u6362\u6210\u7070\u5ea6\u56fe\u50cf\uff0c\u7136\u540e\u4f7f\u7528\u9600\u503c\u51fd\u6570\uff0c\u5c06\u6211\u4eec\u7684logo\u63d0\u53d6\u51fa\u6765\uff0c\u7136\u540e\u5c06\u5176\u4ed6\u90e8\u5206\u53d8\u4e3a\u9ed1\u8272\uff0c\u5373\u50cf\u7d20\u503c\u4e3a0\u3002<\/p>\n<p>\u5b8c\u6574\u4ee3\u7801<\/p>\n<div>\n<pre style=\"font-size: 1em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 1.2em 0px; line-height: 1.2em;\"><code style=\"font-size: 0.85em; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; background: #292824; white-space: pre; overflow-x: auto; color: #a6a28c; margin: 0px 0.15em; display: block; border-radius: 3px; -moz-text-size-adjust: none; border: #cccccc 1px solid; padding: 0.5em;\"><span style=\"color: #b854d4;\"><span style=\"background-color: #292824;\">import<\/span><\/span> cv2 <span style=\"color: #b854d4;\"><span style=\"background-color: #292824;\">as<\/span><\/span> cv\r\n<span style=\"color: #b854d4;\"><span style=\"background-color: #292824;\">import<\/span><\/span> numpy <span style=\"color: #b854d4;\"><span style=\"background-color: #292824;\">as<\/span><\/span> np\r\n\r\n<span style=\"color: #b854d4;\"><span style=\"background-color: #292824;\">if<\/span><\/span>  __name__ == <span style=\"color: #60ac39;\"><span style=\"background-color: #292824;\">\"__main__\"<\/span><\/span>:\r\n    img = cv.imread(<span style=\"color: #60ac39;\"><span style=\"background-color: #292824;\">\"linuxmi.com.png\"<\/span><\/span>) <span style=\"color: #999580;\"><span style=\"background-color: #292824;\">#LOGO<\/span><\/span>\r\n    img1 = cv.imread(<span style=\"color: #60ac39;\"><span style=\"background-color: #292824;\">\"linuxmi.jpg\"<\/span><\/span>) <span style=\"color: #999580;\"><span style=\"background-color: #292824;\">#MESSI<\/span><\/span>\r\n    <span style=\"color: #999580;\"><span style=\"background-color: #292824;\"># look(img)<\/span><\/span>\r\n    rows, cols, channels = img.shape\r\n    roi = img1[<span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0<\/span><\/span>:rows, <span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0<\/span><\/span>:cols] <span style=\"color: #999580;\"><span style=\"background-color: #292824;\">#\u83b7\u5f97messi\u7684ROI<\/span><\/span>\r\n    img2gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)<span style=\"color: #999580;\"><span style=\"background-color: #292824;\"># \u989c\u8272\u7a7a\u95f4\u7684\u8f6c\u6362<\/span><\/span>\r\n    ret, mask = cv.threshold(img2gray, <span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">20<\/span><\/span>, <span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">255<\/span><\/span>, cv.THRESH_BINARY)<span style=\"color: #999580;\"><span style=\"background-color: #292824;\"># \u63a9\u7801 \u9ed1\u8272<\/span><\/span>\r\n    mask_inv = cv.bitwise_not(mask)<span style=\"color: #999580;\"><span style=\"background-color: #292824;\"># \u63a9\u7801\u53d6\u53cd \u767d\u8272<\/span><\/span>\r\n    <span style=\"color: #999580;\"><span style=\"background-color: #292824;\"># #\u53d6mask\u4e2d\u50cf\u7d20\u4e0d\u4e3a\u76840\u7684\u503c\uff0c\u5176\u4f59\u4e3a0<\/span><\/span>\r\n    img1_bg = cv.bitwise_and(img, img, mask=mask)\r\n    img2_fg = cv.bitwise_and(roi, roi, mask=mask_inv)\r\n    dst = cv.add(img1_bg, img2_fg)\r\n    img1[<span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0<\/span><\/span>:rows, <span style=\"color: #b65611;\"><span style=\"background-color: #292824;\">0<\/span><\/span>:cols] = dst\r\n    cv.imshow(<span style=\"color: #60ac39;\"><span style=\"background-color: #292824;\">\"linuxmi.com\"<\/span><\/span>, img1)\r\n    cv.waitKey()\r\n    cv.destroyAllWindows()\r\n<\/code><\/pre>\n<\/div>\n<p>\u6548\u679c\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><a href=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1338 size-large\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python-1024x576.png\" alt=\"\" width=\"731\" height=\"411\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python-1024x576.png 1024w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python-300x169.png 300w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python-768x432.png 768w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python-1536x864.png 1536w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python-430x242.png 430w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/07\/opencv-python.png 1920w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bbaPython OpenCV Image Blending\uff08\u56fe\u50cf\u878d\u5408\uff09\uff0c\u5982\u679c\u60a8\u5bf9\u4f7f &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[102],"class_list":["post-1331","post","type-post","status-publish","format-standard","hentry","category-python","tag-opencv"],"aioseo_notices":[],"views":1131,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/1331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=1331"}],"version-history":[{"count":3,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/1331\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/1331\/revisions\/1339"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=1331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=1331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=1331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}