{"id":1170834,"date":"2025-01-15T16:27:34","date_gmt":"2025-01-15T08:27:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170834.html"},"modified":"2025-01-15T16:27:37","modified_gmt":"2025-01-15T08:27:37","slug":"python-%e5%a6%82%e4%bd%95%e7%94%bb%e7%9f%a9%e5%bd%a2%e6%a1%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170834.html","title":{"rendered":"python \u5982\u4f55\u753b\u77e9\u5f62\u6846"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073342\/595b6be2-6ca4-4199-9ca4-043f20c9e897.webp\" alt=\"python \u5982\u4f55\u753b\u77e9\u5f62\u6846\" \/><\/p>\n<p><p> <strong>Python\u7ed8\u5236\u77e9\u5f62\u6846\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001matplotlib\u5e93\u7b49\u3002<\/strong> \u63a8\u8350\u4f7f\u7528OpenCV\u5e93\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u64cd\u4f5c\u7b80\u5355\u3002\u4f7f\u7528OpenCV\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570cv2.rectangle()\u6765\u7ed8\u5236\u77e9\u5f62\u6846\uff0c\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u56fe\u5f62\u7ed8\u5236\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7ed8\u5236\u77e9\u5f62\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528OpenCV\u7ed8\u5236\u77e9\u5f62\u6846<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u4f7f\u7528OpenCV\u7ed8\u5236\u77e9\u5f62\u6846\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5OpenCV\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5OpenCV\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u77e9\u5f62\u6846<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8cOpenCV\u5e93\u540e\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528OpenCV\u7ed8\u5236\u77e9\u5f62\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((500, 500, 3), dtype=&quot;uint8&quot;)<\/p>\n<p>image.fill(255)<\/p>\n<h2><strong>\u5b9a\u4e49\u77e9\u5f62\u6846\u7684\u8d77\u59cb\u70b9\u548c\u7ed3\u675f\u70b9<\/strong><\/h2>\n<p>start_point = (50, 50)<\/p>\n<p>end_point = (300, 300)<\/p>\n<h2><strong>\u5b9a\u4e49\u77e9\u5f62\u6846\u7684\u989c\u8272 (B, G, R)<\/strong><\/h2>\n<p>color = (255, 0, 0)  # \u84dd\u8272<\/p>\n<h2><strong>\u5b9a\u4e49\u77e9\u5f62\u6846\u7684\u539a\u5ea6<\/strong><\/h2>\n<p>thickness = 2<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u5f62\u6846<\/strong><\/h2>\n<p>cv2.rectangle(image, start_point, end_point, color, thickness)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&quot;Image with Rectangle&quot;, image)<\/p>\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><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf\uff0c\u5e76\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u4e86\u4e00\u4e2a\u84dd\u8272\u7684\u77e9\u5f62\u6846\u3002<code>cv2.rectangle()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u5305\u62ec\u56fe\u50cf\u3001\u77e9\u5f62\u6846\u7684\u8d77\u59cb\u70b9\u548c\u7ed3\u675f\u70b9\u3001\u989c\u8272\u4ee5\u53ca\u539a\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528matplotlib\u7ed8\u5236\u77e9\u5f62\u6846<\/h3>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684Python\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5305\u62ec\u77e9\u5f62\u6846\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528matplotlib\u7ed8\u5236\u77e9\u5f62\u6846\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u77e9\u5f62\u6846<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8cmatplotlib\u5e93\u540e\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528matplotlib\u7ed8\u5236\u77e9\u5f62\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.patches as patches<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aFigure\u548cAxes<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u5b9a\u4e49\u77e9\u5f62\u6846\u7684\u8d77\u59cb\u70b9\u548c\u5bbd\u9ad8<\/strong><\/h2>\n<p>start_point = (50, 50)<\/p>\n<p>width = 250<\/p>\n<p>height = 250<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u5f62\u6846<\/strong><\/h2>\n<p>rect = patches.Rectangle(start_point, width, height, linewidth=2, edgecolor=&#39;b&#39;, facecolor=&#39;none&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u77e9\u5f62\u6846\u5230Axes<\/strong><\/h2>\n<p>ax.add_patch(rect)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim([0, 500])<\/p>\n<p>ax.set_ylim([0, 500])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>patches.Rectangle<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u77e9\u5f62\u6846\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230Axes\u4e2d\u3002<code>rect<\/code>\u7684\u53c2\u6570\u5305\u62ec\u8d77\u59cb\u70b9\u3001\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u8fb9\u6846\u5bbd\u5ea6\u3001\u8fb9\u6846\u989c\u8272\u4ee5\u53ca\u586b\u5145\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001OpenCV\u4e0ematplotlib\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><h4>1\u3001\u529f\u80fd\u548c\u7528\u9014<\/h4>\n<\/p>\n<p><p><strong>OpenCV\uff1a<\/strong> OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5982\u56fe\u50cf\u8fc7\u6ee4\u3001\u7279\u5f81\u68c0\u6d4b\u3001\u5bf9\u8c61\u8bc6\u522b\u7b49\u3002\u5b83\u66f4\u9002\u5408\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>matplotlib\uff1a<\/strong> matplotlib\u662f\u4e00\u4e2a\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002\u5b83\u66f4\u9002\u5408\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4efb\u52a1\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u50cf\u7ed8\u5236\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6613\u7528\u6027<\/h4>\n<\/p>\n<p><p><strong>OpenCV\uff1a<\/strong> OpenCV\u7684API\u8bbe\u8ba1\u7b80\u6d01\uff0c\u4f7f\u7528\u65b9\u4fbf\u3002\u5bf9\u4e8e\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0cOpenCV\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u5404\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>matplotlib\uff1a<\/strong> matplotlib\u7684API\u4e5f\u5f88\u7b80\u6d01\uff0c\u4f7f\u7528\u65b9\u4fbf\u3002\u5bf9\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4efb\u52a1\uff0cmatplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u51fd\u6570\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6027\u80fd<\/h4>\n<\/p>\n<p><p><strong>OpenCV\uff1a<\/strong> OpenCV\u7684\u6027\u80fd\u975e\u5e38\u9ad8\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u56fe\u50cf\u548c\u590d\u6742\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u65f6\u3002OpenCV\u91c7\u7528\u4e86\u591a\u79cd\u4f18\u5316\u6280\u672f\uff0c\u5982\u591a\u7ebf\u7a0b\u548cSIMD\u6307\u4ee4\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p><strong>matplotlib\uff1a<\/strong> matplotlib\u7684\u6027\u80fd\u76f8\u5bf9\u8f83\u4f4e\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u548c\u590d\u6742\u56fe\u5f62\u65f6\u3002\u7531\u4e8ematplotlib\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4efb\u52a1\uff0c\u5b83\u7684\u6027\u80fd\u4f18\u5316\u4e3b\u8981\u96c6\u4e2d\u5728\u7ed8\u56fe\u4e0a\uff0c\u800c\u4e0d\u662f\u56fe\u50cf\u5904\u7406\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u66f4\u591aOpenCV\u7ed8\u5236\u5f62\u72b6\u7684\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u77e9\u5f62\u6846\uff0cOpenCV\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u7ed8\u5236\u5f62\u72b6\u7684\u51fd\u6570\uff0c\u5982\u5706\u5f62\u3001\u591a\u8fb9\u5f62\u3001\u76f4\u7ebf\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7ed8\u5236\u5f62\u72b6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u7ed8\u5236\u5706\u5f62<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((500, 500, 3), dtype=&quot;uint8&quot;)<\/p>\n<p>image.fill(255)<\/p>\n<h2><strong>\u5b9a\u4e49\u5706\u5f62\u7684\u4e2d\u5fc3\u70b9\u548c\u534a\u5f84<\/strong><\/h2>\n<p>center = (250, 250)<\/p>\n<p>radius = 100<\/p>\n<h2><strong>\u5b9a\u4e49\u5706\u5f62\u7684\u989c\u8272 (B, G, R)<\/strong><\/h2>\n<p>color = (0, 255, 0)  # \u7eff\u8272<\/p>\n<h2><strong>\u5b9a\u4e49\u5706\u5f62\u7684\u539a\u5ea6<\/strong><\/h2>\n<p>thickness = 2<\/p>\n<h2><strong>\u7ed8\u5236\u5706\u5f62<\/strong><\/h2>\n<p>cv2.circle(image, center, radius, color, thickness)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&quot;Image with Circle&quot;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u591a\u8fb9\u5f62<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((500, 500, 3), dtype=&quot;uint8&quot;)<\/p>\n<p>image.fill(255)<\/p>\n<h2><strong>\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u9876\u70b9<\/strong><\/h2>\n<p>points = np.array([[100, 100], [200, 50], [300, 100], [250, 200], [150, 200]], np.int32)<\/p>\n<p>points = points.reshape((-1, 1, 2))<\/p>\n<h2><strong>\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u989c\u8272 (B, G, R)<\/strong><\/h2>\n<p>color = (0, 0, 255)  # \u7ea2\u8272<\/p>\n<h2><strong>\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u539a\u5ea6<\/strong><\/h2>\n<p>thickness = 2<\/p>\n<h2><strong>\u7ed8\u5236\u591a\u8fb9\u5f62<\/strong><\/h2>\n<p>cv2.polylines(image, [points], isClosed=True, color=color, thickness=thickness)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&quot;Image with Polygon&quot;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u76f4\u7ebf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((500, 500, 3), dtype=&quot;uint8&quot;)<\/p>\n<p>image.fill(255)<\/p>\n<h2><strong>\u5b9a\u4e49\u76f4\u7ebf\u7684\u8d77\u59cb\u70b9\u548c\u7ed3\u675f\u70b9<\/strong><\/h2>\n<p>start_point = (50, 50)<\/p>\n<p>end_point = (450, 450)<\/p>\n<h2><strong>\u5b9a\u4e49\u76f4\u7ebf\u7684\u989c\u8272 (B, G, R)<\/strong><\/h2>\n<p>color = (255, 0, 255)  # \u7d2b\u8272<\/p>\n<h2><strong>\u5b9a\u4e49\u76f4\u7ebf\u7684\u539a\u5ea6<\/strong><\/h2>\n<p>thickness = 2<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u7ebf<\/strong><\/h2>\n<p>cv2.line(image, start_point, end_point, color, thickness)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&quot;Image with Line&quot;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u7ed8\u5236\u77e9\u5f62\u6846\u662f\u56fe\u50cf\u5904\u7406\u548c\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528OpenCV\u5e93\u548cmatplotlib\u5e93\u3002<\/strong> OpenCV\u5e93\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u800cmatplotlib\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4efb\u52a1\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u7ed8\u5236\u77e9\u5f62\u6846\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u7ed8\u5236\u77e9\u5f62\u6846\u3002\u5982\u679c\u4f60\u5bf9\u56fe\u50cf\u5904\u7406\u548c\u6570\u636e\u53ef\u89c6\u5316\u6709\u66f4\u6df1\u5165\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b66\u4e60OpenCV\u548cmatplotlib\u7684\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7ed8\u5236\u77e9\u5f62\u6846\uff1f<\/strong><br \/>Python\u4e2d\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u5305\u62ecMatplotlib\u3001Pygame\u548cOpenCV\u7b49\u3002Matplotlib\u9002\u5408\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0cPygame\u5219\u9002\u5408\u6e38\u620f\u5f00\u53d1\uff0c\u800cOpenCV\u5219\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u77e9\u5f62\u6846\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Matplotlib\u4e2d\u7ed8\u5236\u77e9\u5f62\u6846\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.patches.Rectangle<\/code>\u6765\u7ed8\u5236\u77e9\u5f62\u6846\u3002\u9700\u8981\u5b9a\u4e49\u77e9\u5f62\u7684\u8d77\u59cb\u5750\u6807\u3001\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230Axes\u5bf9\u8c61\u4e2d\u3002\u5177\u4f53\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\nfig, ax = plt.subplots()\nrect = patches.Rectangle((x, y), width, height, linewidth=1, edgecolor=&#39;r&#39;, facecolor=&#39;none&#39;)\nax.add_patch(rect)\nplt.xlim(0, 10)\nplt.ylim(0, 10)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Pygame\u4e2d\uff0c\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u586b\u5145\u7684\u77e9\u5f62\u6846\uff1f<\/strong><br \/>\u4f7f\u7528Pygame\u7ed8\u5236\u77e9\u5f62\u6846\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>pygame.draw.rect()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u76ee\u6807\u8868\u9762\u3001\u989c\u8272\u3001\u77e9\u5f62\u7684\u5750\u6807\u53ca\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pygame\npygame.init()\nscreen = pygame.display.set_mode((400, 300))\npygame.draw.rect(screen, (255, 0, 0), (50, 50, 100, 50))\npygame.display.flip()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7ea2\u8272\u77e9\u5f62\u6846\u4f1a\u88ab\u7ed8\u5236\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ed8\u5236\u77e9\u5f62\u6846\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001matplotlib\u5e93\u7b49\u3002 \u63a8\u8350\u4f7f\u7528Ope [&hellip;]","protected":false},"author":3,"featured_media":1170841,"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\/1170834"}],"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=1170834"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170834\/revisions"}],"predecessor-version":[{"id":1170845,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170834\/revisions\/1170845"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170841"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}