{"id":4776,"date":"2022-04-08T00:53:49","date_gmt":"2022-04-07T16:53:49","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=4776"},"modified":"2024-05-05T14:19:16","modified_gmt":"2024-05-05T06:19:16","slug":"opencv-%e9%a2%9c%e8%89%b2%e5%88%86%e5%89%b2","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=4776","title":{"rendered":"opencv-python \u989c\u8272\u5206\u5272"},"content":{"rendered":"\n<p>\u989c\u8272\u5206\u5272\u4e00\u822c\u7528\u4e8e\u5728\u5355\u4e00\u80cc\u666f\u7684\u56fe\u50cf\u4e2d\u63d0\u53d6\u5f69\u8272\u56fe\u50cf\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u968f\u610f\u62cd\u7684\u4e00\u5f20\u56fe\u7247\u7528\u4e8e\u6f14\u793a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"385\" height=\"542\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-109.png\" alt=\"\" class=\"wp-image-4777 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-109.png 385w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-109-213x300.png 213w\" data-sizes=\"(max-width: 385px) 100vw, 385px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 385px; --smush-placeholder-aspect-ratio: 385\/542;\" \/><\/figure>\n\n\n\n<p>\u6253\u5f00\u672c\u5730\u56fe\u7247\uff0cOpenCV \u4e2d\u56fe\u50cf\u901a\u9053\u987a\u5e8f\u4e3a BGR\uff0c\u800c Matplotlib \u4e2d\u4f7f\u7528\u7684\u662f RGB\uff0c\u6240\u4ee5\u8981\u663e\u793a\u56fe\u7247\u8fd9\u91cc\u8981\u8f6c\u6362\u4e00\u4e0b\u3002\u5f53\u7136\u4e0d\u8f6c\u6362\u4e5f\u80fd\u663e\u793a\uff0c\u53ea\u662f\u663e\u793a\u51fa\u6765\u7684\u989c\u8272\u548c\u539f\u56fe\u4e0d\u4e00\u6837\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import cv2  # opencv-python==4.5.5\nimport matplotlib.pyplot as plt\n\nbgr_img = cv2.imread('image.png')\nrgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)\nplt.imshow(rgb_img)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"615\" height=\"593\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-110.png\" alt=\"\" class=\"wp-image-4782 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-110.png 615w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-110-300x289.png 300w\" data-sizes=\"(max-width: 615px) 100vw, 615px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 615px; --smush-placeholder-aspect-ratio: 615\/593;\" \/><\/figure>\n\n\n\n<p>\u63a5\u7740\u9700\u8981\u4f7f\u7528\u6ee4\u6ce2\u5668\u5bf9\u56fe\u50cf\u8fdb\u884c\u6a21\u7cca\u5904\u7406\uff0c\u51cf\u5c11\u56fe\u50cf\u4e2d\u7684\u7ec6\u5fae\u5dee\u5f02\u3002\u9009\u4e0b\u9762\u4e00\u79cd\u5c31\u884c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">blur = cv2.blur(bgr_img, (5,5))  # \u5747\u503c\u6ee4\u6ce2<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">blur = cv2.medianBlur(bgr_img, 5)  # \u4e2d\u503c\u6ee4\u6ce2<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">blur = cv2.GaussianBlur(bgr_img, (5,5), 0)  # \u9ad8\u65af\u6ee4\u6ce2<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">blur = cv2.bilateralFilter(bgr_img, 9, 75, 75)  # \u53cc\u8fb9\u6ee4\u6ce2<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"582\" height=\"800\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-111.png\" alt=\"\" class=\"wp-image-4784 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-111.png 582w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-111-218x300.png 218w\" data-sizes=\"(max-width: 582px) 100vw, 582px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 582px; --smush-placeholder-aspect-ratio: 582\/800;\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"578\" height=\"817\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-112.png\" alt=\"\" class=\"wp-image-4785 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-112.png 578w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-112-212x300.png 212w\" data-sizes=\"(max-width: 578px) 100vw, 578px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 578px; --smush-placeholder-aspect-ratio: 578\/817;\" \/><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u5c06\u6a21\u7cca\u5904\u7406\u540e\u7684\u56fe\u50cf\u7531 BGR \u8f6c\u4e3a HSV\uff08H \u8272\u5ea6 S \u9971\u548c\u5ea6 V \u4eae\u5ea6\uff09\uff0c\u53ef\u4ee5\u66f4\u597d\u7684\u8fdb\u884c\u989c\u8272\u5206\u5272\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"565\" height=\"381\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-113.png\" alt=\"\" class=\"wp-image-4786 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-113.png 565w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-113-300x202.png 300w\" data-sizes=\"(max-width: 565px) 100vw, 565px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 565px; --smush-placeholder-aspect-ratio: 565\/381;\" \/><\/figure>\n\n\n\n<p>\u7136\u540e\u8fdb\u884c\u989c\u8272\u5206\u5272\uff0c\u8ba1\u7b97\u51fa\u63a9\u7801\u56fe\u50cf\u3002\u6839\u636e\u56fe\u50cf\u80cc\u666f\u548c\u8981\u5206\u5272\u7684\u4e1c\u897f\u4e0d\u540c\uff0c\u8fd9\u91cc\u5206\u5272\u7684\u9608\u503c\u8303\u56f4\u4e5f\u4e0d\u540c\uff0c\u9700\u8981\u8fdb\u884c\u4e00\u5b9a\u7684\u5c1d\u8bd5\u3002<\/p>\n\n\n\n<p>\u6ce8\uff1amask \u672c\u8eab\u5e94\u8be5\u662f\u9ed1\u767d\u56fe\u50cf\uff0c\u8fd9\u91cc\u7684\u8272\u5f69\u662f Matplotlib \u663e\u793a\u56fe\u7247\u7684\u56e0\u7d20\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\n\nlow_blue = np.array([105,0,0])\nhigh_blue = np.array([200, 255, 255])\nmask = cv2.inRange(hsv, low_blue, high_blue)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"585\" height=\"487\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-114.png\" alt=\"\" class=\"wp-image-4787 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-114.png 585w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-114-300x250.png 300w\" data-sizes=\"(max-width: 585px) 100vw, 585px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 585px; --smush-placeholder-aspect-ratio: 585\/487;\" \/><\/figure>\n\n\n\n<p>\u6700\u540e\u8fdb\u884c\u4e0e\u8fd0\u7b97\uff0c\u5c06 mask \u4f5c\u4e3a\u80cc\u666f\u56fe\u50cf<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">res = cv2.bitwise_and(bgr_img, bgr_img, mask=mask)\nrgb_img = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)\nplt.imshow(rgb_img)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"588\" height=\"411\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-115.png\" alt=\"\" class=\"wp-image-4788 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-115.png 588w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/04\/image-115-300x210.png 300w\" data-sizes=\"(max-width: 588px) 100vw, 588px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 588px; --smush-placeholder-aspect-ratio: 588\/411;\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u989c\u8272\u5206\u5272\u4e00\u822c\u7528\u4e8e\u5728\u5355\u4e00\u80cc\u666f\u7684\u56fe\u50cf\u4e2d\u63d0\u53d6\u5f69\u8272\u56fe\u50cf\u3002 \u8fd9\u91cc\u6211\u968f\u610f\u62cd\u7684\u4e00\u5f20\u56fe\u7247\u7528\u4e8e\u6f14\u793a \u6253\u5f00\u672c\u5730\u56fe\u7247\uff0cOpenCV  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-4776","post","type-post","status-publish","format-standard","hentry","category-all"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/4776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4776"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/4776\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}