opencv-python-學習筆記二(利用opencv繪圖)


常見參數:

color:顏色(255,255,0),BGR表示

thickness:線寬px,-1代表填充

linetype:圓邊界類型。cv.LINE_4,cv.LINE_8,cv.LINE_AA,AA代表抗鋸齒線

shift:圖形縮小倍數

常見函數:

cv.arrowedLine()  箭頭線

cv.arrowedLine(img, pt1, pt2, color[, thickness[, line_type[, shift[, tipLength]]]])

pt1:箭頭起點(x,y)

pt2:箭頭終點(x,y)

tipLength:

import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512, 512, 3), np.uint8)
cv.arrowedLine(img, (0, 0), (255, 255), (255, 255, 255))
# cv.WINDOW_AUTOSIZE  cv.WINDOW_NORMAL
cv.namedWindow('circle', cv.WINDOW_NORMAL)
# 顯示圖像
cv.imshow('circle', img)
cv.waitKey(0)
cv.destroyAllWindows()

 

cv.line() 直線

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

pt1:起點(x,y)

pt2:終點(x,y)

import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512, 512, 3), np.uint8)

cv.line(img, (255, 255), (274, 274), (240, 240, 240), 10) # cv.WINDOW_AUTOSIZE cv.WINDOW_NORMAL cv.namedWindow('circle', cv.WINDOW_NORMAL) # 顯示圖像 cv.imshow('circle', img) cv.waitKey(0) cv.destroyAllWindows()

 

常見函數:

cv.circle()  圓

cv.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

img:

center:圓心(x,y)

radius:半徑 int

import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512, 512, 3), np.uint8)
cv.circle(img, (447, 63), 63, (255, 255, 255), -1)
# cv.WINDOW_AUTOSIZE  cv.WINDOW_NORMAL
cv.namedWindow('circle', cv.WINDOW_NORMAL)
# 顯示圖像
cv.imshow('circle', img)
cv.waitKey(0)
cv.destroyAllWindows()

 

矩形

常見函數

cv.rectangle

cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])

pt1:對角,左上點

pt2:對角,右下點

import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512, 512, 3), np.uint8)
cv.rectangle(img, ((10, 10), (40, 40)), (255, 255, 255))
# cv.WINDOW_AUTOSIZE  cv.WINDOW_NORMAL
cv.namedWindow('circle')
# 顯示圖像
cv.imshow('circle', img)
cv.waitKey(0)
cv.destroyAllWindows()

 

其它

常見函數

cv.drawMarker() 在指定點出標記

cv.drawMarker(img, position, color[, markerType[, markerSize[, thickness[, line_type]]]])

position:點位置 (x,y)

markerType:標記類型

import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512, 512, 3), np.uint8)
cv.drawMarker(img, (260, 260), (255, 255, 255)
# cv.WINDOW_AUTOSIZE cv.WINDOW_NORMAL
cv.namedWindow('circle')
# 顯示圖像
cv.imshow('circle', img)
cv.waitKey(0)
cv.destroyAllWindows()

 

 

cv.putText() 繪制文本字符串

cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

org:位置

text:字符串內容

fontFace:

  

 cv.FONT_HERSHEY_SIMPLEX

normal size sans-serif font

cv.FONT_HERSHEY_PLAIN

small size sans-serif font

cv.FONT_HERSHEY_DUPLEX

normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)

cv.FONT_HERSHEY_COMPLEX

normal size serif font

cv.FONT_HERSHEY_TRIPLEX

normal size serif font (more complex than FONT_HERSHEY_COMPLEX)

 cv.FONT_HERSHEY_COMPLEX_SMALL

smaller version of FONT_HERSHEY_COMPLEX

cv.FONT_HERSHEY_SCRIPT_SIMPLEX

hand-writing style font

cv.FONT_HERSHEY_SCRIPT_COMPLEX

more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX

 cv.FONT_ITALIC

flag for italic font

fontScale:字體縮放倍數

bottomLeftOrigin:true:。false:反轉。

import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512, 512, 3), np.uint8)
# cv.circle(img, (447, 63), 63, (255, 255, 255), -1)
# cv.arrowedLine(img, (0, 0), (255, 255), (255, 255, 255))
# cv.drawMarker(img, (260, 260), (255, 255, 255)    )
# cv.line(img, (255, 255), (274, 274), (240, 240, 240), 10)
cv.putText(    img, "opencv", (200, 200), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255))

# cv.WINDOW_AUTOSIZE  cv.WINDOW_NORMAL
cv.namedWindow('circle')
# 顯示圖像
cv.imshow('circle', img)
cv.waitKey(0)
cv.destroyAllWindows()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM