{"id":1092180,"date":"2025-01-08T14:18:46","date_gmt":"2025-01-08T06:18:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1092180.html"},"modified":"2025-01-08T14:18:48","modified_gmt":"2025-01-08T06:18:48","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%87%ba%e5%9b%be%e4%b8%ad%e7%9f%a9%e5%bd%a2%e5%8c%ba%e5%9f%9f%e7%9a%84%e5%9d%90%e6%a0%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1092180.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u51fa\u56fe\u4e2d\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205336\/df97dad1-82dc-4354-aef9-f7d85c7da71c.webp\" alt=\"python\u5982\u4f55\u8bfb\u51fa\u56fe\u4e2d\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bfb\u53d6\u56fe\u50cf\u4e2d\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\uff0c\u4e3b\u8981\u901a\u8fc7\u56fe\u50cf\u5904\u7406\u5e93\u548c\u8ba1\u7b97\u51e0\u4f55\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u56fe\u50cf\u9884\u5904\u7406\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u8f6e\u5ed3\u68c0\u6d4b\u548c\u5750\u6807\u63d0\u53d6\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u56fe\u50cf\u5e76\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\u6b65\u9aa4\uff0c\u5982\u7070\u5ea6\u5316\u548c\u4e8c\u503c\u5316\u3002\u7136\u540e\u4f7f\u7528\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff08\u5982Canny\u7b97\u6cd5\uff09\u6765\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u8f6e\u5ed3\u68c0\u6d4b\u6765\u627e\u5230\u56fe\u50cf\u4e2d\u7684\u77e9\u5f62\u533a\u57df\uff0c\u6700\u540e\u63d0\u53d6\u8fd9\u4e9b\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u52a0\u8f7d\u56fe\u50cf\u548c\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5904\u7406\u56fe\u50cf\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u52a0\u8f7d\u56fe\u50cf\u5e76\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\u6b65\u9aa4\u3002\u9884\u5904\u7406\u901a\u5e38\u5305\u62ec\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u548c\u8fdb\u884c\u4e8c\u503c\u5316\u5904\u7406\u3002\u8fd9\u4e9b\u6b65\u9aa4\u6709\u52a9\u4e8e\u63d0\u9ad8\u8fb9\u7f18\u68c0\u6d4b\u548c\u8f6e\u5ed3\u68c0\u6d4b\u7684\u6548\u679c\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;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u8fdb\u884c\u4e8c\u503c\u5316\u5904\u7406<\/strong><\/h2>\n<p>_, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u8fb9\u7f18\u68c0\u6d4b\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u6b65\u9aa4\uff0c\u7528\u4e8e\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002Canny\u7b97\u6cd5\u662f\u4e00\u79cd\u591a\u7ea7\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff0c\u80fd\u591f\u6709\u6548\u68c0\u6d4b\u51fa\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5<\/p>\n<p>edges = cv2.Canny(binary, 50, 150)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8f6e\u5ed3\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u5728\u68c0\u6d4b\u5230\u56fe\u50cf\u7684\u8fb9\u7f18\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8f6e\u5ed3\u68c0\u6d4b\u7b97\u6cd5\u6765\u627e\u5230\u56fe\u50cf\u4e2d\u7684\u77e9\u5f62\u533a\u57df\u3002OpenCV\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>findContours<\/code>\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8f6e\u5ed3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8f6e\u5ed3<\/p>\n<p>contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CH<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>N_APPROX_SIMPLE)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u63d0\u53d6\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807<\/h3>\n<\/p>\n<p><p>\u5728\u68c0\u6d4b\u5230\u56fe\u50cf\u4e2d\u7684\u8f6e\u5ed3\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u51e0\u4f55\u7b97\u6cd5\u6765\u63d0\u53d6\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528OpenCV\u63d0\u4f9b\u7684<code>boundingRect<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6bcf\u4e2a\u8f6e\u5ed3\u7684\u5916\u63a5\u77e9\u5f62\uff0c\u5e76\u63d0\u53d6\u5176\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u6240\u6709\u68c0\u6d4b\u5230\u7684\u8f6e\u5ed3<\/p>\n<p>for contour in contours:<\/p>\n<p>    # \u8ba1\u7b97\u8f6e\u5ed3\u7684\u5916\u63a5\u77e9\u5f62<\/p>\n<p>    x, y, w, h = cv2.boundingRect(contour)<\/p>\n<p>    # \u7ed8\u5236\u77e9\u5f62\u533a\u57df<\/p>\n<p>    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)<\/p>\n<p>    # \u6253\u5370\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807<\/p>\n<p>    print(f&quot;Rectangle: x={x}, y={y}, w={w}, h={h}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u663e\u793a\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u793a\u5904\u7406\u540e\u7684\u56fe\u50cf\uff0c\u4ee5\u4fbf\u76f4\u89c2\u5730\u67e5\u770b\u68c0\u6d4b\u5230\u7684\u77e9\u5f62\u533a\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u5904\u7406\u540e\u7684\u56fe\u50cf<\/p>\n<p>cv2.imshow(&#39;Rectangles&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528OpenCV\u5e93\u6765\u8bfb\u53d6\u56fe\u50cf\u4e2d\u7684\u77e9\u5f62\u533a\u57df\u5e76\u63d0\u53d6\u5176\u5750\u6807\u3002<strong>\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u56fe\u50cf\u9884\u5904\u7406\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u8f6e\u5ed3\u68c0\u6d4b\u548c\u5750\u6807\u63d0\u53d6\u3002<\/strong> \u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u77e9\u5f62\u533a\u57df\uff0c\u5e76\u63d0\u53d6\u5176\u5750\u6807\u7528\u4e8e\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0cOpenCV\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u51fd\u6570\u548c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u5206\u6790\u56fe\u50cf\u6570\u636e\uff0c\u4ee5\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u56fe\u50cf\u4e2d\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u6765\u8bfb\u53d6\u56fe\u50cf\u5e76\u901a\u8fc7\u7528\u6237\u4ea4\u4e92\u6216\u7b97\u6cd5\u83b7\u53d6\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5OpenCV\u5e93\uff0c\u7136\u540e\u52a0\u8f7d\u56fe\u50cf\u5e76\u4f7f\u7528\u9f20\u6807\u56de\u8c03\u51fd\u6570\u6765\u9009\u62e9\u77e9\u5f62\u533a\u57df\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u5b9a\u4e49\u9f20\u6807\u56de\u8c03\u51fd\u6570\ndef draw_rectangle(event, x, y, flags, param):\n    global x_start, y_start, drawing\n    if event == cv2.EVENT_LBUTTONDOWN:\n        drawing = True\n        x_start, y_start = x, y\n    elif event == cv2.EVENT_MOUSEMOVE:\n        if drawing:\n            cv2.rectangle(image, (x_start, y_start), (x, y), (0, 255, 0), 2)\n    elif event == cv2.EVENT_LBUTTONUP:\n        drawing = False\n        cv2.rectangle(image, (x_start, y_start), (x, y), (0, 255, 0), 2)\n        print(f&quot;\u77e9\u5f62\u5750\u6807: ({x_start}, {y_start}), ({x}, {y})&quot;)\n\n# \u52a0\u8f7d\u56fe\u50cf\nimage = cv2.imread(&#39;image.jpg&#39;)\ncv2.namedWindow(&#39;Image&#39;)\ncv2.setMouseCallback(&#39;Image&#39;, draw_rectangle)\n\ndrawing = False\nx_start, y_start = -1, -1\n\nwhile True:\n    cv2.imshow(&#39;Image&#39;, image)\n    if cv2.waitKey(1) &amp; 0xFF == 27:  # \u6309Esc\u952e\u9000\u51fa\n        break\n\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u56fe\u50cf\u4ee5\u63d0\u53d6\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u6280\u672f\uff0c\u5982\u8fb9\u7f18\u68c0\u6d4b\u548c\u8f6e\u5ed3\u67e5\u627e\uff0c\u6765\u81ea\u52a8\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u77e9\u5f62\u533a\u57df\u3002\u901a\u8fc7OpenCV\u7684<code>cv2.findContours<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u627e\u5230\u56fe\u50cf\u4e2d\u7684\u6240\u6709\u8f6e\u5ed3\uff0c\u5e76\u901a\u8fc7\u7b5b\u9009\u6761\u4ef6\u83b7\u5f97\u77e9\u5f62\u7684\u5750\u6807\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u8bfb\u53d6\u5e76\u8f6c\u6362\u56fe\u50cf\u4e3a\u7070\u5ea6\nimage = cv2.imread(&#39;image.jpg&#39;)\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n# \u5e94\u7528\u8fb9\u7f18\u68c0\u6d4b\nedges = cv2.Canny(gray, 50, 150)\n\n# \u67e5\u627e\u8f6e\u5ed3\ncontours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n\nfor contour in contours:\n    x, y, w, h = cv2.boundingRect(contour)\n    if w &gt; 50 and h &gt; 50:  # \u8bbe\u5b9a\u6700\u5c0f\u77e9\u5f62\u5c3a\u5bf8\n        print(f&quot;\u77e9\u5f62\u5750\u6807: ({x}, {y}), \u5bbd\u5ea6: {w}, \u9ad8\u5ea6: {h}&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4fdd\u5b58\u77e9\u5f62\u5750\u6807\u4fe1\u606f\uff1f<\/strong><br \/>\u83b7\u53d6\u5230\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\u540e\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5c06\u8fd9\u4e9b\u4fe1\u606f\u5b58\u50a8\u5230\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u6587\u4ef6\u64cd\u4f5c\uff0c\u6216\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u5750\u6807\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868\u6765\u5b58\u50a8\u77e9\u5f62\u5750\u6807\nrectangles = [((x1, y1), (x2, y2)), ((x3, y3), (x4, y4))]\n\n# \u8f6c\u6362\u4e3aDataFrame\u5e76\u4fdd\u5b58\ndf = pd.DataFrame(rectangles, columns=[&#39;Start&#39;, &#39;End&#39;])\ndf.to_csv(&#39;rectangles.csv&#39;, index=False)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u83b7\u53d6\u548c\u5904\u7406\u56fe\u50cf\u4e2d\u7684\u77e9\u5f62\u533a\u57df\u5750\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bfb\u53d6\u56fe\u50cf\u4e2d\u77e9\u5f62\u533a\u57df\u7684\u5750\u6807\uff0c\u4e3b\u8981\u901a\u8fc7\u56fe\u50cf\u5904\u7406\u5e93\u548c\u8ba1\u7b97\u51e0\u4f55\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u56fe\u50cf\u9884\u5904\u7406\u3001\u8fb9 [&hellip;]","protected":false},"author":3,"featured_media":1092193,"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\/1092180"}],"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=1092180"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092180\/revisions"}],"predecessor-version":[{"id":1092197,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092180\/revisions\/1092197"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1092193"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1092180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1092180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1092180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}