OpenCV-Python学习(14)—— OpenCV 绘制箭头线(cv.arrowedLine)

1. 学习目标

  1. 学会使用 cv.arrowedLine 带箭头的直线;
  2. 绘制箭头的直线的注意事项。

2. 绘制箭头直线 cv.arrowedLine 函数说明

2.1 cv.arrowedLine() 函数使用
cv.arrowedLine(img, pt1, pt2, color[, thickness=1, line_type=8, shift=0, tipLength=0.1]) → img
2.2 参数说明
参数说明
img表示输入图像,允许单通道灰度图像或多通道彩色图像。
pt1表示线段第一个点的坐标,(x1, y1)。
pt2表示线段第二个点的坐标,(x2, y2)。
color表示绘制直线的颜色,(b,g,r) 格式的元组,或者表示灰度值的标量。
thickness表示绘制直线的粗细,默认值 1px。
lineType表示绘制直线的线性,默认为 LINE_8。
shift表示点坐标的小数位数,默认为 0。
tipLength表示箭头部分长度与线段长度的比例,默认为 0.1
2.3 lineType 值说明
描述
cv.FILLED表示内部填充(实心图形)。
cv.LINE_4表示 4 邻接线型。
cv.LINE_8表示 8 邻接线型。
cv.LINE_AA表示抗锯齿线型,图像更平滑。

3. 实例

3.1 坐标轴实例代码
import cv2 as cv
import numpy as np

def create_csys():
  # 创建一个500*500的白色背景图片
  img = np.ones((500,500,3), dtype=np.uint8)*255
  h,w,c = img.shape
  # 设置坐标轴颜色
  color = (0,0,0)
  # 坐标轴x的起始坐标
  piontx1 = (10,int(h/2))
  pointx2 = (w - 10, int(h/2))
  # x轴绘制
  cv.arrowedLine(img, piontx1, pointx2, color)
  # 坐标轴y的起始坐标
  pionty1 = (int(w/2),h - 10)
  pointy2 = (int(w/2), 10)
  # y轴绘制
  cv.arrowedLine(img, pionty1, pointy2, color)
  cv.imshow("csys img", img)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  create_csys()
3.2 实例运行结果

输入图片说明

3.3 修改箭头大小实例代码
import cv2 as cv
import numpy as np

def create_csys():
  # 创建一个500*500的白色背景图片
  img = np.ones((500,500,3), dtype=np.uint8)*255
  h,w,c = img.shape
  # 设置坐标轴颜色
  color = (0,0,0)
  # 坐标轴箭头大小比例
  tipLength = 0.02
  # 坐标轴x的起始坐标
  piontx1 = (10,int(h/2))
  pointx2 = (w - 10, int(h/2))
  # x轴绘制
  cv.arrowedLine(img, piontx1, pointx2, color, tipLength=tipLength)
  # 坐标轴y的起始坐标
  pionty1 = (int(w/2),h - 10)
  pointy2 = (int(w/2), 10)
  # y轴绘制
  cv.arrowedLine(img, pionty1, pointy2, color, tipLength=tipLength)
  cv.imshow("csys img", img)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  create_csys()
3.4 运行结果

输入图片说明

3.5 其他实例
import cv2 as cv
import numpy as np

def create_demo():
  # 创建一个500*500的白色背景图片
  img = np.ones((300,300,3), dtype=np.uint8)*255
  
  h,w,c = img.shape
  # 创建越界箭头直线
  cv.arrowedLine(img, (10,50), (w+10,50), (255,0,0), tipLength=0.05)
  # 非越界箭头
  cv.arrowedLine(img, (10,100), (w,100), (0,255,0), tipLength=0.08)
  # 双向箭头
  point1 = (10,150)
  point2 = (w - 10,150)
  cv.arrowedLine(img, point1, point2, (0,0,255))
  cv.arrowedLine(img, point2, point1, (0,0,255))

  cv.imshow("csys img", img)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  create_demo()
3.6 实例运行结果

输入图片说明

4. 注意

  1. 点坐标的格式是 (x,y) 而不是 (y,x);
  2. 坐标点必须是int类型;
  3. 双向箭头的绘制就是两条交换起点和终点的箭头直线;
  4. color的值是(b,g,r);
  5. tipLength表示箭头部分长度与线段长度的比例。
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rattenking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值