天天看點

7、OpenCV 畫線段 —— cv.line()0、格式1、功能2、輸入3、輸出4、示例5、備注

目錄

  • 0、格式
  • 1、功能
  • 2、輸入
  • 3、輸出
  • 4、示例
  • 5、備注

0、格式

  • line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])

1、功能

  • 在圖像

    img

    上的兩點

    pt1

    ,

    pt2

    之間畫一條線段

2、輸入

  • img

    :要在上面畫線段的圖像
  • pt1

    :線段的第 1 個點
  • pt2

    :線段的第 2 個點
  • color

    :線段的顔色
  • thickness

    :線段的粗細
  • lineType

    :線段的類型
  • shift

    :坐标精确到小數點後第幾位

3、輸出

  • img

    :畫完線段的輸入圖像

4、示例

  • 代碼
# 導入 OpenCV
import cv2 as cv

# 讀取圖像
imgBgr = cv.imread(r'/home/work/0/OpenCV/0/img/Ta152.jpg')
print('imgBgr.shape:', imgBgr.shape)        # (768, 1024, 3)

# 擷取圖像角點坐标
ptLeftUp = (0, 0)
ptRightUp = (imgBgr.shape[1], 0)
ptRightDown = (imgBgr.shape[1], imgBgr.shape[0])        # (h, w) -> (x, y)
ptLeftDown = (0, imgBgr.shape[1])

# 畫線段
imgRet = cv.line(imgBgr, ptLeftUp, ptRightDown, (0, 255, 0), 3)

# 比較輸入圖像和傳回圖像的記憶體位址
print('id(imgBgr) == id(imgRet):', id(imgBgr) == id(imgRet))        # True

# 顯示圖像
cv.imshow('imgBgr', imgBgr)
cv.imshow('imgRet', imgRet)
idKey = cv.waitKey(0)
if idKey == '27':       # 27 為 ESC 鍵對應的 ASCII 碼
    cv.destroyAllWindows()
           
  • 效果
    7、OpenCV 畫線段 —— cv.line()0、格式1、功能2、輸入3、輸出4、示例5、備注

5、備注

  • pt1

    pt2

    的中對應的坐标順序為

    (x1, y1)

    (x2, y2)

  • 超出圖像範圍的線段會被截斷
  • 傳回圖像和輸入圖像是同一張圖