跳到主要内容

图像添加线段

图像添加线段1、实现原理2、实现效果3、实现代码

1、实现原理

使用cv2.line() 函数在图像上绘制线短。

2、实现效果

x cd ~/opencv xxxxxxxxxx python3 11.image_draw_line.py

注意:选中图片按q可以退出程序!

image-20250106182621895

3、实现代码

​ x import cv2 def draw_line(input_path, output_path, start_point, end_point, color, thickness): image = cv2.imread(input_path) if image is None: print("Error: Unable to open image file.") return cv2.line(image, start_point, end_point, color, thickness) if cv2.imwrite(output_path, image): print(f"Image saved to {output_path}") cv2.imshow('Image Preview', cv2.imread(output_path)) cv2.waitKey(0) cv2.destroyAllWindows() else: print("Error: Unable to save image file.") draw_line('/home/jetson/opencv/images/yahboom_logo.png', \ '/home/jetson/opencv/images/yahboom_logo_line.png', \ (50, 150), (700, 150), (0, 0, 255), 5)