目录
设置空白画布 画线 画矩形 画圆 画椭圆 画多边形 添加文字
import numpy as np
import cv2
img = np.zeros((512,512,3), np.uint8)
本质是创建了一个512x512x3的0矩阵,用于存储512x512的3通道图像,数据类型为uint8。
常见函数参数:
cv2.line(img,pt1,pt2,color,thinkness,lineType,shift)
import numpy as np
import cv2
img = np.zeros((512, 512, 3), dtype=np.uint8) # 设置窗口尺寸和数据类型
cv2.line(img, (200, 100), (300, 200), (255, 0, 0), 1)
cv2.line(img, (200, 200), (300, 300), (0, 255, 0), 5, 4)
cv2.line(img, (200, 300), (300, 400), (0, 0, 255), 10, cv2.LINE_AA)
WindowsName = 'example'
cv2.namedWindow(WindowsName)
cv2.imshow(WindowsName, img)
cv2.waitKey(0)
cv2.destroyWindow(WindowsName)
cv2.rectangle(img,pt1,pt2,color,thinkness,lineType,shift)
import numpy as np
import cv2
img = np.zeros((512, 512, 3), dtype=np.uint8) # 设置窗口尺寸和数据类型
cv2.rectangle(img, (350, 255), (511, 511), (0, 255, 0), 1)
cv2.rectangle(img, (350, 255), (511, 511), (0, 0, 255), -1, shift=1)
WindowsName = 'example'
cv2.namedWindow(WindowsName)
cv2.imshow(WindowsName, img)
cv2.waitKey(0)
cv2.destroyWindow(WindowsName)
cv2.circle(img,center,radius,color,thinkness,lineType,shift)
import numpy as np
import cv2
img = np.zeros((512, 512, 3), dtype=np.uint8)
cv2.circle(img, (255, 255), 63, (0, 0, 255), -1)
cv2.circle(img, (255, 255), 63, (255, 0, 0), 1, shift=1)
WindowsName = 'example'
cv2.namedWindow(WindowsName)
cv2.imshow(WindowsName, img)
cv2.waitKey(0)
cv2.destroyWindow(WindowsName)
cv2.ellipse(img,center,axes,angle,startAngle,endAngle,color,thinkness,lineType,shift)
import numpy as np
import cv2
img = np.zeros((512, 512, 3), dtype=np.uint8) # 设置窗口尺寸和数据类型
cv2.ellipse(img, (160, 256), (100, 50), 0, 0, 240, (0, 255, 0), -1)
cv2.ellipse(img, (400, 256), (100, 50), 60, 0, 240, (0, 255, 0), -1)
WindowsName = 'example'
cv2.namedWindow(WindowsName)
cv2.imshow(WindowsName, img)
cv2.waitKey(0)
cv2.destroyWindow(WindowsName)
cv2.polylines(img,pts,isClosed,color,thinkness,lineType,shift)
cv2.fillPoly(img,pts,color,thinkness,lineType,shift,offset)
import numpy as np
import cv2
img = np.zeros((512, 512, 3), dtype=np.uint8) # 设置窗口尺寸和数据类型
a = np.array([[[10, 10], [100, 10], [100, 100], [10, 100]]], dtype=np.int32)
b = np.array([[[150, 100], [250, 230], [200, 200], [150, 220]]], dtype=np.int32)
c = np.array([[[300, 300], [400, 430], [350, 400], [300, 420]]], dtype=np.int32)
cv2.fillPoly(img, a, (255, 0, 0))
cv2.polylines(img, b, 0, (0, 255, 0)) # 0或False表示不封闭
cv2.polylines(img, c, 1, (0, 255, 0)) # True表示封闭
WindowsName = 'example'
cv2.namedWindow(WindowsName)
cv2.imshow(WindowsName, img)
cv2.waitKey(0)
cv2.destroyWindow(WindowsName
cv2.putText(img, text, org, fontFace, fontScale, color, thinkness, lineType, bottomLeftOrigin)
CV_FONT_HERSHEY_SIMPLEX - 正常大小无衬线字体。
CV_FONT_HERSHEY_PLAIN - 小号无衬线字体。
CV_FONT_HERSHEY_DUPLEX - 正常大小无衬线字体比 CV_FONT_HERSHEY_SIMPLEX 更复杂)
CV_FONT_HERSHEY_COMPLEX - 正常大小有衬线字体。
CV_FONT_HERSHEY_TRIPLEX - 正常大小有衬线字体 ( 比 CV_FONT_HERSHEY_COMPLEX更复杂)
CV_FONT_HERSHEY_COMPLEX_SMALL - CV_FONT_HERSHEY_COMPLEX 的小译
CV_FONT_HERSHEY_SCRIPT_SIMPLEX - 手写风格字体。
CV_FONT_HERSHEY_SCRIPT_COMPLEX - 比 CV_FONT_HERSHEY_SCRIPT_SIMPLEX 更复杂。
参数能够由一个值和可选择的 CV_FONT_ITALIC 字体标记合成。就是斜体字。
import numpy as np
import cv2
img = np.zeros((512, 512, 3), dtype=np.uint8) # 设置窗口尺寸和数据类型
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OpenCV', (10, 300), font, 4, (0, 255, 0), 2, cv2.LINE_AA, 0)
cv2.putText(img, 'OpenCV', (10, 300), font, 4, (255, 0, 0), 2, cv2.LINE_AA, 1)
WindowsName = 'example'
cv2.namedWindow(WindowsName)
cv2.imshow(WindowsName, img)
cv2.waitKey(0)
cv2.destroyWindow(WindowsName)