python + opencv 第四节 绘图函数 绘制直线 绘制矩形


import numpy as np
import cv2
# 1.画直线 
img = np.zeros((512, 512, 3), np.uint8)
# print(type(img))
# print(img)
cv2.line(img, (100,100), (511, 511), (255, 0, 0), 5, lineType=8, shift=1)
# def line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
# img : 要画的线所在的图像
# pt1 : 直线起点
# pt2 : 直线终点
# color : 线条颜色 BGR
# thickness : 线条宽度
# lineType : 8 -- 8-connected line 8邻连接线
#            4 -- 4-connected line 4邻连接线
#            CV_AA   反锯齿连接线 (采用了高斯滤波)
# shift : 坐标点小数点位数(即坐标点的缩小倍数,0表示不缩小,1表示缩小10背
#          比如现在坐标是 起点(100, 100),终点(511,511) shift = 1 坐标就变成 起点(10.0, 10.0),终点(51.1, 51.1)
cv2.namedWindow('line')
cv2.imshow('line', img)
cv2.waitKey(10000) #显示 10000ms 即10s后消失
cv2.destroyAllWindows()
#关于linetype这个参数,可以用下面这段代码去体验不同值的不同效果
#
# lineType = cv2.LINE_8
# # lineType = cv2.LINE_4
# # lineType = cv2.LINE_AA
#
# img = np.zeros((420, 1000, 4), np.uint8)
# cv2.putText(
#     img=img,
#     text='XerCis',
#     org=(0, 300),
#     fontFace=cv2.FONT_HERSHEY_SIMPLEX,
#     fontScale=10,
#     color=(0, 255, 0),
#     thickness=2,
#     lineType=lineType)
# cv2.imshow('LINE_8', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# 这段代码来源https://blog.csdn.net/lly1122334/article/details/88913758
# 2.画矩形
import cv2
import numpy as np

img = np.zeros((512, 512, 3), np.uint8) # 新建一张画布
# numpy.zeros()函数 :
# def zeros(shape, dtype=None, order='C'):  # real signature unknown; restored from __doc__
#     """
#     zeros(shape, dtype=float, order='C')
#
#         Return a new array of given shape and type, filled with zeros.
#
#         Parameters
#         ----------
#         shape : int or tuple of ints
#             Shape of the new array, e.g., ``(2, 3)`` or ``2``.
#         dtype : data-type, optional
#             The desired data-type for the array, e.g., `numpy.int8`.  Default is
#             `numpy.float64`.
#         order : {'C', 'F'}, optional, default: 'C'
#             Whether to store multi-dimensional data in row-major
#             (C-style) or column-major (Fortran-style) order in
#             memory.
#
#         Returns
#         -------
#         out : ndarray
#             Array of zeros with the given shape, dtype, and order.

cv2.rectangle(img, (100, 100), (200, 200), (0, 255, 0), 3)
# def rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None): # real signature unknown; restored from __doc__
# 参数 pt1 : 左上角顶点
# 参数 pt2 : 右下角顶点
# 其他参数与 cv2.line() 函数一样
cv2.namedWindow('rectangle')
cv2.imshow('rectangle', img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
# 3.画圆
import cv2
import numpy as np
img = np.zeros((512, 512, 3), np.uint8)
cv2.circle(img, (300, 300), 50, (255, 0, 0), 3)
# def circle(img, center, radius, color, thickness=None, lineType=None, shift=None)
# center : 圆心坐标
# radius : 半径
cv2.namedWindow('circle')
cv2.imshow('circle', img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
# 4.画椭圆
import cv2
import numpy as np
img = np.zeros((512, 512, 3), np.uint8)
# def ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness=None, lineType=None, shift=None)
# 参数 center : 椭圆圆心
# 参数 axes : 椭圆长轴和短轴长度
# 参数 angle : 椭圆旋转角度
# 参数 startAngle : 椭圆弧的起始角,单位为度
# 参数 endAngle : 椭圆弧的结束角,以度为单位
cv2.ellipse(img, (256,256), (100,50), 60, 0, 360, (255, 0, 0),  -1) # 这里 thinkness = -1 表示填充
cv2.namedWindow('ellipse')
cv2.imshow('ellipse', img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
# 5.画多边形
import cv2
import numpy as np
img = np.zeros((512, 512, 3), np.uint8)
# def polylines(img, pts, isClosed, color, thickness=None, lineType=None, shift=None)
# 参数 pts : 多边形曲线阵列 就是要绘制的多边形的各个点坐标
# 参数 isClosed : 表示多边形是闭合还是开放的
pts = np.array([[20,20], [20, 50], [40, 70], [80, 70], [100, 20]], np.int32) #创建一个坐标数组
# pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1, 1, 2)) #矩阵变换
cv2.polylines(img, [pts], True, (0, 255 , 0)) #这里注意 pts 要用[]括起来
cv2.namedWindow('polylines')
cv2.imshow('polylines', img)
cv2.waitKey(10000)
cv2.destroyAllWindows()

6.绘制文字的例子,见上文绘制直线的例子里的linetype例子里有

def putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None): # real signature unknown; restored from __doc__
    """
    putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
    .   @brief Draws a text string.
    .   
    .   The function cv::putText renders the specified text string in the image. Symbols that cannot be rendered
    .   using the specified font are replaced by question marks. See #getTextSize for a text rendering code
    .   example.
    .   
    .   @param img Image.
    .   @param text Text string to be drawn.
    .   @param org Bottom-left corner of the text string in the image.
    .   @param fontFace Font type, see #HersheyFonts.
    .   @param fontScale Font scale factor that is multiplied by the font-specific base size.
    .   @param color Text color.
    .   @param thickness Thickness of the lines used to draw a text.
    .   @param lineType Line type. See #LineTypes
    .   @param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise,
    .   it is at the top-left corner.
发布了80 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qiukapi/article/details/104441266