{"id":1179101,"date":"2025-01-15T18:19:07","date_gmt":"2025-01-15T10:19:07","guid":{"rendered":""},"modified":"2025-01-15T18:19:09","modified_gmt":"2025-01-15T10:19:09","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%9b%be%e5%83%8f%e4%b8%8a%e5%88%92%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179101.html","title":{"rendered":"python\u5982\u4f55\u5728\u56fe\u50cf\u4e0a\u5212\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113623\/8fdd8f12-55ac-4430-85e5-318dd0e83eb0.webp\" alt=\"python\u5982\u4f55\u5728\u56fe\u50cf\u4e0a\u5212\u7ebf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u3001Pillow\u548cMatplotlib\u7b49\u5e93\u5728\u56fe\u50cf\u4e0a\u5212\u7ebf\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u65b9\u6cd5\u6765\u5904\u7406\u56fe\u50cf\u5e76\u7ed8\u5236\u7ebf\u6761\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>OpenCV<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u3002<strong>Pillow<\/strong>\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u56fe\u50cf\u7684Python\u5e93\uff0c\u5b83\u7ee7\u627f\u4e86PIL\uff08Python Imaging Library\uff09\u7684\u529f\u80fd\u3002<strong>Matplotlib<\/strong>\u4e3b\u8981\u7528\u4e8e\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u7ed8\u5236\u7ebf\u6761\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u4e2a\u5e93\u5728\u56fe\u50cf\u4e0a\u5212\u7ebf\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001OPENCV<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8f6f\u4ef6\u5e93\uff0c\u5b83\u5177\u6709\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528OpenCV\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5OpenCV<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86OpenCV\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u7ebf\u6761<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>cv2.line<\/code>\u51fd\u6570\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u3002\u8be5\u51fd\u6570\u7684\u53c2\u6570\u5305\u62ec\u56fe\u50cf\u3001\u8d77\u70b9\u5750\u6807\u3001\u7ec8\u70b9\u5750\u6807\u3001\u7ebf\u6761\u989c\u8272\u548c\u7ebf\u6761\u539a\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761<\/p>\n<p>start_point = (50, 50)<\/p>\n<p>end_point = (200, 200)<\/p>\n<p>color = (255, 0, 0)  # \u989c\u8272\u4e3a\u84dd\u8272\uff08BGR\u683c\u5f0f\uff09<\/p>\n<p>thickness = 2<\/p>\n<p>image_with_line = cv2.line(image, start_point, end_point, color, thickness)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>cv2.imshow<\/code>\u663e\u793a\u56fe\u50cf\uff0c\u4f7f\u7528<code>cv2.imwrite<\/code>\u4fdd\u5b58\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u56fe\u50cf<\/p>\n<p>cv2.imshow(&#39;Image with Line&#39;, image_with_line)<\/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<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;output_image.jpg&#39;, image_with_line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001PILLOW<\/h3>\n<\/p>\n<p><p>Pillow\u662f\u4e00\u4e2a\u53cb\u597d\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u7ee7\u627f\u81eaPIL\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u7ed8\u5236\u7ebf\u6761\u548c\u5904\u7406\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pillow<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path_to_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u7ebf\u6761<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>ImageDraw.Draw<\/code>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>line<\/code>\u65b9\u6cd5\u7ed8\u5236\u7ebf\u6761\u3002\u8be5\u65b9\u6cd5\u7684\u53c2\u6570\u5305\u62ec\u8d77\u70b9\u548c\u7ec8\u70b9\u5750\u6807\u5217\u8868\u3001\u7ebf\u6761\u989c\u8272\u548c\u7ebf\u6761\u5bbd\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/p>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>start_point = (50, 50)<\/p>\n<p>end_point = (200, 200)<\/p>\n<p>color = (255, 0, 0)  # \u989c\u8272\u4e3a\u7ea2\u8272\uff08RGB\u683c\u5f0f\uff09<\/p>\n<p>width = 2<\/p>\n<p>draw.line([start_point, end_point], fill=color, width=width)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>show<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u50cf\uff0c\u4f7f\u7528<code>save<\/code>\u65b9\u6cd5\u4fdd\u5b58\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u56fe\u50cf<\/p>\n<p>image.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001MATPLOTLIB<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u56fe\u8868\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u7ebf\u6761<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>plot<\/code>\u51fd\u6570\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u3002\u8be5\u51fd\u6570\u7684\u53c2\u6570\u5305\u62ec\u8d77\u70b9\u548c\u7ec8\u70b9\u5750\u6807\u3001\u7ebf\u6761\u989c\u8272\u548c\u7ebf\u6761\u5bbd\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u56fe\u50cf<\/p>\n<p>plt.imshow(image)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>start_point = [50, 50]<\/p>\n<p>end_point = [200, 200]<\/p>\n<p>plt.plot([start_point[0], end_point[0]], [start_point[1], end_point[1]], color=&#39;red&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf\u5e76\u7ed8\u5236\u7684\u7ebf\u6761<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4fdd\u5b58\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>savefig<\/code>\u65b9\u6cd5\u4fdd\u5b58\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>plt.savefig(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u4f7f\u7528OpenCV\u3001Pillow\u548cMatplotlib\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u5404\u6709\u4f18\u7f3a\u70b9\u3002<\/strong>OpenCV\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002Pillow\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u56fe\u50cf\u4efb\u52a1\u3002Matplotlib\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u54ea\u79cd\u5e93\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002<\/strong>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u4efb\u52a1\uff0cOpenCV\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u53ea\u9700\u8981\u7b80\u5355\u5730\u7ed8\u5236\u7ebf\u6761\u548c\u5904\u7406\u56fe\u50cf\uff0cPillow\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u9700\u8981\u7ed8\u5236\u56fe\u8868\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0cMatplotlib\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u8fd9\u4e09\u4e2a\u5e93\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u7684\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528OpenCV<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>start_point = (50, 50)<\/p>\n<p>end_point = (200, 200)<\/p>\n<p>color = (255, 0, 0)  # \u989c\u8272\u4e3a\u84dd\u8272\uff08BGR\u683c\u5f0f\uff09<\/p>\n<p>thickness = 2<\/p>\n<p>image_with_line = cv2.line(image, start_point, end_point, color, thickness)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image with Line&#39;, image_with_line)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;output_image.jpg&#39;, image_with_line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Pillow<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>start_point = (50, 50)<\/p>\n<p>end_point = (200, 200)<\/p>\n<p>color = (255, 0, 0)  # \u989c\u8272\u4e3a\u7ea2\u8272\uff08RGB\u683c\u5f0f\uff09<\/p>\n<p>width = 2<\/p>\n<p>draw.line([start_point, end_point], fill=color, width=width)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528Matplotlib<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>start_point = [50, 50]<\/p>\n<p>end_point = [200, 200]<\/p>\n<p>plt.plot([start_point[0], end_point[0]], [start_point[1], end_point[1]], color=&#39;red&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf\u5e76\u7ed8\u5236\u7684\u7ebf\u6761<\/strong><\/h2>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u4ee3\u7801\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u4f7f\u7528OpenCV\u3001Pillow\u548cMatplotlib\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u7684\u65b9\u6cd5\u3002\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u9002\u5408\u7684\u5e93\u6765\u5904\u7406\u56fe\u50cf\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528OpenCV\u5e93\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\uff1f<\/strong><br \/>\u4f7f\u7528OpenCV\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86OpenCV\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install opencv-python<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u7136\u540e\uff0c\u8bfb\u53d6\u56fe\u50cf\uff0c\u4f7f\u7528<code>cv2.line()<\/code>\u51fd\u6570\u6307\u5b9a\u8d77\u59cb\u548c\u7ed3\u675f\u5750\u6807\u3001\u989c\u8272\u548c\u7ebf\u6761\u7c97\u7ec6\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u8bfb\u53d6\u56fe\u50cf\nimage = cv2.imread(&#39;image.jpg&#39;)\n\n# \u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u7ebf\u6761\nstart_point = (50, 50)\nend_point = (200, 200)\ncolor = (255, 0, 0)  # BGR\u683c\u5f0f\nthickness = 2\ncv2.line(image, start_point, end_point, color, thickness)\n\n# \u663e\u793a\u56fe\u50cf\ncv2.imshow(&#39;Image with Line&#39;, image)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p><strong>\u5728\u7ed8\u5236\u7ebf\u6761\u65f6\u5982\u4f55\u9009\u62e9\u989c\u8272\u548c\u7ebf\u6761\u7c97\u7ec6\uff1f<\/strong><br \/>\u5728OpenCV\u4e2d\uff0c\u989c\u8272\u662f\u4ee5BGR\u683c\u5f0f\u8868\u793a\u7684\u3002\u4f8b\u5982\uff0c\u7ea2\u8272\u4e3a(0, 0, 255)\uff0c\u7eff\u8272\u4e3a(0, 255, 0)\uff0c\u84dd\u8272\u4e3a(255, 0, 0)\u3002\u7ebf\u6761\u7684\u7c97\u7ec6\u53ef\u4ee5\u901a\u8fc7<code>thickness<\/code>\u53c2\u6570\u8fdb\u884c\u8bbe\u7f6e\uff0c\u6570\u503c\u8d8a\u5927\u7ebf\u6761\u8d8a\u7c97\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8c03\u6574\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<p><strong>\u7ed8\u5236\u591a\u6761\u7ebf\u6761\u65f6\u6709\u4ec0\u4e48\u5efa\u8bae\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u591a\u6761\u7ebf\u6761\uff0c\u5efa\u8bae\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u5904\u7406\u3002\u53ef\u4ee5\u5c06\u8d77\u59cb\u548c\u7ed3\u675f\u70b9\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\u8fdb\u884c\u7ed8\u5236\u3002\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">lines = [((50, 50), (200, 200)), ((100, 100), (300, 300))]\nfor line in lines:\n    cv2.line(image, line[0], line[1], (255, 0, 0), 2)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u51cf\u5c11\u91cd\u590d\u4ee3\u7801\uff0c\u8fd8\u80fd\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u8c03\u6574\u9700\u8981\u7ed8\u5236\u7684\u7ebf\u6761\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u3001Pillow\u548cMatplotlib\u7b49\u5e93\u5728\u56fe\u50cf\u4e0a\u5212\u7ebf\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1179106,"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\/1179101"}],"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=1179101"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179101\/revisions"}],"predecessor-version":[{"id":1179109,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179101\/revisions\/1179109"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179106"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}