{"id":1121382,"date":"2025-01-08T19:08:51","date_gmt":"2025-01-08T11:08:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121382.html"},"modified":"2025-01-08T19:08:53","modified_gmt":"2025-01-08T11:08:53","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e6%9f%90%e4%b8%80%e5%9d%97%e7%9a%84%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1121382.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u67d0\u4e00\u5757\u7684\u56fe\u50cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083648\/233dbb5c-7a00-441f-b580-03e3d87bb1ab.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u67d0\u4e00\u5757\u7684\u56fe\u50cf\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u63d0\u53d6\u67d0\u4e00\u5757\u56fe\u50cf\u7684\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\uff1a\u52a0\u8f7d\u56fe\u50cf\u3001\u6307\u5b9a\u611f\u5174\u8da3\u533a\u57df(ROI)\u3001\u63d0\u53d6\u5e76\u4fdd\u5b58\u8be5\u533a\u57df\u3002\u5e38\u7528\u7684\u5de5\u5177\u6709OpenCV\u548cPillow\u3002<\/strong> \u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528OpenCV\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5e76\u4e14\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u9886\u57df\u975e\u5e38\u53d7\u6b22\u8fce\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u52a0\u8f7d\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528OpenCV\u7684<code>cv2.imread()<\/code>\u51fd\u6570\u52a0\u8f7d\u56fe\u50cf\u3002OpenCV\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u5982JPEG\u3001PNG\u3001BMP\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6307\u5b9a\u611f\u5174\u8da3\u533a\u57df\uff08ROI\uff09<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u52a0\u8f7d\u540e\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9a\u8981\u63d0\u53d6\u7684\u56fe\u50cf\u533a\u57df\u3002\u611f\u5174\u8da3\u533a\u57df\uff08ROI, Region of Interest\uff09\u901a\u5e38\u901a\u8fc7\u56fe\u50cf\u5750\u6807\u6765\u6307\u5b9a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6307\u5b9aROI\u5750\u6807\uff1a\u5de6\u4e0a\u89d2\u5750\u6807(x1, y1) \u548c \u53f3\u4e0b\u89d2\u5750\u6807(x2, y2)<\/p>\n<p>x1, y1 = 100, 100<\/p>\n<p>x2, y2 = 200, 200<\/p>\n<h2><strong>\u63d0\u53d6ROI<\/strong><\/h2>\n<p>roi = image[y1:y2, x1:x2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4fdd\u5b58\u63d0\u53d6\u7684\u56fe\u50cf\u5757<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u5b8cROI\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.imwrite()<\/code>\u51fd\u6570\u4fdd\u5b58\u56fe\u50cf\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58ROI<\/p>\n<p>cv2.imwrite(&#39;roi_image.jpg&#39;, roi)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c55\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u9a8c\u8bc1\u63d0\u53d6\u6548\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684<code>cv2.imshow()<\/code>\u51fd\u6570\u5c55\u793a\u539f\u56fe\u548c\u63d0\u53d6\u7684\u56fe\u50cf\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u539f\u56fe\u548cROI<\/p>\n<p>cv2.imshow(&#39;Original Image&#39;, image)<\/p>\n<p>cv2.imshow(&#39;ROI&#39;, roi)<\/p>\n<h2><strong>\u7b49\u5f85\u6309\u952e\u4e8b\u4ef6\uff0c\u7136\u540e\u5173\u95ed\u6240\u6709\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pillow\u63d0\u53d6\u56fe\u50cf\u5757<\/h3>\n<\/p>\n<p><p>\u867d\u7136OpenCV\u662f\u56fe\u50cf\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u4f46Pillow\uff08PIL\uff09\u4e5f\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pillow\u8fdb\u884c\u7c7b\u4f3c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u6307\u5b9aROI\u5750\u6807\uff1a\u5de6\u4e0a\u89d2\u5750\u6807(x1, y1) \u548c \u53f3\u4e0b\u89d2\u5750\u6807(x2, y2)<\/strong><\/h2>\n<p>x1, y1 = 100, 100<\/p>\n<p>x2, y2 = 200, 200<\/p>\n<h2><strong>\u63d0\u53d6ROI<\/strong><\/h2>\n<p>roi = image.crop((x1, y1, x2, y2))<\/p>\n<h2><strong>\u4fdd\u5b58ROI<\/strong><\/h2>\n<p>roi.save(&#39;roi_image_pillow.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793aROI<\/strong><\/h2>\n<p>roi.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528OpenCV\u6307\u5b9aROI<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0cROI\u7684\u6307\u5b9a\u548c\u63d0\u53d6\u662f\u4e00\u4e2a\u5e38\u89c1\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9f20\u6807\u4e8b\u4ef6\u4ea4\u4e92\u6765\u52a8\u6001\u6307\u5b9aROI\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u9f20\u6807\u56de\u8c03\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u9f20\u6807\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u9f20\u6807\u5728\u56fe\u50cf\u7a97\u53e3\u4e0a\u70b9\u51fb\u65f6\u8bb0\u5f55\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<\/strong><\/h2>\n<p>ref_point = []<\/p>\n<p>cropping = False<\/p>\n<p>def click_and_crop(event, x, y, flags, param):<\/p>\n<p>    global ref_point, cropping<\/p>\n<p>    if event == cv2.EVENT_LBUTTONDOWN:<\/p>\n<p>        ref_point = [(x, y)]<\/p>\n<p>        cropping = True<\/p>\n<p>    elif event == cv2.EVENT_LBUTTONUP:<\/p>\n<p>        ref_point.append((x, y))<\/p>\n<p>        cropping = False<\/p>\n<p>        cv2.rectangle(image, ref_point[0], ref_point[1], (0, 255, 0), 2)<\/p>\n<p>        cv2.imshow(&quot;image&quot;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u52a0\u8f7d\u56fe\u50cf\u5e76\u8bbe\u7f6e\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u52a0\u8f7d\u56fe\u50cf\u540e\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u5c06\u9f20\u6807\u56de\u8c03\u51fd\u6570\u4e0e\u7a97\u53e3\u5173\u8054\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<p>clone = image.copy()<\/p>\n<p>cv2.namedWindow(&quot;image&quot;)<\/p>\n<p>cv2.setMouseCallback(&quot;image&quot;, click_and_crop)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b9e\u73b0\u52a8\u6001ROI\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u5faa\u73af\u4e2d\u5904\u7406\u9f20\u6807\u4e8b\u4ef6\uff0c\u5e76\u5728\u7528\u6237\u6307\u5b9aROI\u540e\u8fdb\u884c\u56fe\u50cf\u63d0\u53d6\u548c\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    cv2.imshow(&quot;image&quot;, image)<\/p>\n<p>    key = cv2.waitKey(1) &amp; 0xFF<\/p>\n<p>    if key == ord(&quot;r&quot;):<\/p>\n<p>        image = clone.copy()<\/p>\n<p>    elif key == ord(&quot;c&quot;):<\/p>\n<p>        break<\/p>\n<p>if len(ref_point) == 2:<\/p>\n<p>    roi = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:ref_point[1][0]]<\/p>\n<p>    cv2.imshow(&quot;ROI&quot;, roi)<\/p>\n<p>    cv2.imwrite(&quot;roi_image_dynamic.jpg&quot;, roi)<\/p>\n<p>    cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u56fe\u50cf\u5757\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u64cd\u4f5c\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<strong>OpenCV<\/strong>\u548c<strong>Pillow<\/strong>\u662f\u4e24\u79cd\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5404\u6709\u4f18\u52a3\u3002OpenCV\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u800cPillow\u4f7f\u7528\u7b80\u5355\uff0c\u9002\u7528\u4e8e\u57fa\u672c\u7684\u56fe\u50cf\u64cd\u4f5c\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6\u56fe\u50cf\u5757\uff0c\u6db5\u76d6\u4e86\u4ece\u52a0\u8f7d\u56fe\u50cf\u3001\u6307\u5b9aROI\u5230\u4fdd\u5b58\u548c\u5c55\u793a\u56fe\u50cf\u7684\u5404\u4e2a\u73af\u8282\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u5904\u7406\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u533a\u57df\uff1f<\/strong><br \/>\u4f7f\u7528Python\u63d0\u53d6\u7279\u5b9a\u533a\u57df\u7684\u56fe\u50cf\u901a\u5e38\u4f9d\u8d56\u4e8e\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982OpenCV\u548cPillow\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u52a0\u8f7d\u56fe\u50cf\u6587\u4ef6\uff0c\u6307\u5b9a\u8981\u63d0\u53d6\u7684\u533a\u57df\u7684\u5750\u6807\uff0c\u7136\u540e\u5c06\u8be5\u533a\u57df\u4fdd\u5b58\u4e3a\u65b0\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u8bfb\u53d6\u56fe\u50cf\u3001\u5b9a\u4e49\u611f\u5174\u8da3\u533a\u57df\uff08ROI\uff09\u7684\u5750\u6807\uff0c\u4ee5\u53ca\u4f7f\u7528\u88c1\u526a\u529f\u80fd\u63d0\u53d6\u5e76\u4fdd\u5b58\u8be5\u533a\u57df\u3002<\/p>\n<p><strong>\u63d0\u53d6\u56fe\u50cf\u533a\u57df\u65f6\u5e38\u89c1\u7684\u5750\u6807\u7cfb\u7edf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u5750\u6807\u7cfb\u7edf\u901a\u5e38\u4ee5\u5de6\u4e0a\u89d2\u4e3a\u539f\u70b9\uff0cX\u8f74\u5411\u53f3\u5ef6\u4f38\uff0cY\u8f74\u5411\u4e0b\u5ef6\u4f38\u3002\u5750\u6807\u503c\u662f\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\u7684\u3002\u4f8b\u5982\uff0c\u5750\u6807\uff08100, 200\uff09\u8868\u793a\u56fe\u50cf\u4e2d\u4ece\u5de6\u8fb9\u7f18\u5411\u53f3\u79fb\u52a8100\u4e2a\u50cf\u7d20\uff0c\u4ece\u4e0a\u8fb9\u7f18\u5411\u4e0b\u79fb\u52a8200\u4e2a\u50cf\u7d20\u7684\u4f4d\u7f6e\u3002\u63d0\u53d6\u533a\u57df\u65f6\uff0c\u9700\u8981\u5b9a\u4e49\u5de6\u4e0a\u89d2\u548c\u53f3\u4e0b\u89d2\u7684\u5750\u6807\uff0c\u4ee5\u51c6\u786e\u88c1\u526a\u51fa\u6240\u9700\u90e8\u5206\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684Python\u5e93\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u6d41\u884c\u7684\u5e93\u53ef\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecOpenCV\u3001Pillow\u548cscikit-image\u3002OpenCV\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff1bPillow\u5219\u662f\u4e00\u4e2a\u7b80\u6d01\u6613\u7528\u7684\u5e93\uff0c\u9002\u5408\u5feb\u901f\u5904\u7406\u548c\u64cd\u4f5c\u56fe\u50cf\uff1bscikit-image\u5219\u4e13\u6ce8\u4e8e\u63d0\u4f9b\u56fe\u50cf\u5904\u7406\u7684\u7b97\u6cd5\u548c\u5de5\u5177\uff0c\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u56fe\u50cf\u5206\u6790\u4efb\u52a1\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u8fdb\u884c\u56fe\u50cf\u63d0\u53d6\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u63d0\u53d6\u67d0\u4e00\u5757\u56fe\u50cf\u7684\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\uff1a\u52a0\u8f7d\u56fe\u50cf\u3001\u6307\u5b9a\u611f\u5174\u8da3\u533a\u57df(ROI)\u3001\u63d0\u53d6\u5e76\u4fdd\u5b58\u8be5\u533a\u57df\u3002\u5e38\u7528\u7684\u5de5\u5177 [&hellip;]","protected":false},"author":3,"featured_media":1121388,"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\/1121382"}],"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=1121382"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121382\/revisions"}],"predecessor-version":[{"id":1121390,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121382\/revisions\/1121390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121388"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}