cv2-drawline


title: cv2 drawline
date: 2022-07-02 21:23:50
tags: opencv

cv2 drawline


import numpy as np
import cv2 as cv

def draw_line():
    '''
    画直线
    cv.line(img, pt1, pt2, color, thickness)

    Parameters:
        img	Image.
        pt1	First point of the line segment.
        pt2	Second point of the line segment.
        color	Line color.
        thickness	Line thickness.
        lineType	Type of the line. See LineTypes.
        shift	Number of fractional bits in the point coordinates.
    '''
    #创建一个黑色的图像
    img = np.zeros((512, 512, 3), np.uint8)

    #画一条5px宽的对角线
    cv.line(img, (0, 0), (511, 511), (255, 0, 0), 5)

    #显示图片
    cv.imshow('img', img)

    key = cv.waitKey(0)
    if key == ord('q'):
        cv.destroyAllWindows()


def draw_rectangle():

    '''
    画矩形
    cv.rectangle(img, pt1, pt2, color, thickness)

    Parameters
        img	Image.
        pt1	Vertex of the rectangle.
        pt2	Vertex of the rectangle opposite to pt1 .
        color	Rectangle color or brightness (grayscale image).
        thickness	Thickness of lines that make up the rectangle. Negative values, like FILLED, mean that the function has to draw a filled rectangle.
        lineType	Type of the line. See LineTypes
        shift	Number of fractional bits in the point coordinates.
    
    '''

    img = np.zeros((512, 512, 3), np.uint8)
    cv.rectangle(img, (384, 0), (510, 128), (0, 256, 0), 3)
    
    cv.imshow('img', img)
    key = cv.waitKey(0)
    if key == ord('q'):
        cv.destroyAllWindows()

def draw_circle():
    '''
    
    '''
    img = np.zeros((512, 512), np.uint8)
    cv.circle(img,(447,63), 63, (0,0,255), -1)
    # cv.circle(img, (200, 63), 63, (0, 255, 0), 3)
    cv.imshow('img', img)
    key = cv.waitKey(0)
    if key == ord('q'):
        cv.destroyAllWindows()

def draw_polylines():
    img = np.zeros((512, 512, 3), np.uint8)
    pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
    pts = pts.reshape((-1, 1, 2))
    cv.polylines(img, [pts], True, (0, 255, 255))

    cv.imshow('img', img)
    key = cv.waitKey(0)
    if key == ord('q'):
        cv.destroyAllWindows()
    print(pts.shape)

你可能感兴趣的:(python,计算机视觉,opencv,python)