OpenCV: Drawing graphics using python-cv2

1. Draw a line segment

cv2.line(img, pt1, pt2, color, thickness, lineType):

-img: 目标图像
-pt1: 起点
-pt2: 终点
-color: 绘制的颜色
-thickness: 线段宽度
-lineType: 线段类型
import cv2
import numpy as np
newImageInfo = (200,200,3)
dat = np.zeros(newImageInfo, np.uint8)
# 绘制线段 1.目标图像 2.begin 3.end 4.color
cv2.line(dat, (0,0), (200, 200), (0,,255))
cv2.line(dat, (50, 100), (150, 100), (0,255,255), 20)
cv2.imshow('dat', dat)
cv2.waitKey(0)

Insert picture description here

Insert picture description here

2. Draw rectangles, circles and ellipses

cv2.rectangle(img, pt1, pt2, color,thickness,lineType)

cv2.circle(img, center, radius, color,thickness,lineType

cv2.ellipse(mg, center, axes, angle, startAngle, endAngle, color,thickness,lineType)

cv2.ellipse?
import cv2
import numpy as np
newImageInfo = (200,200,3)
dat = np.zeros(newImageInfo, np.uint8)
# -1表示填充
cv2.rectangle(dat, (50,50), (150, 150), (0, 255, 0), -1) 
# 2.center 3 半径 4 color 
cv2.circle(dat, (100,100), (25), (0,0,255),2)
# 1.图像 2 圆心 3 轴 4 角度 5 起始角度 6 终止角度 7 颜色 8宽度
cv2.ellipse(dat,(100,100), (50,25),0,0,360,(255,0,0),2)
cv2.imshow('dat', dat)
cv2.waitKey(0)

Insert picture description here

3. Draw any polygon

cv2.polylines(img, pts, isClosed, color,thickness,lineType)

import cv2
import numpy as np
newImageInfo = (200,200,3)
dat = np.zeros(newImageInfo, np.uint8)

points = np.array([[25,25], [50, 25], [25, 75], [150, 175], [100, 75]], np.int32)
points = points.reshape((-1, 1, 2))
cv2.polylines(dat, [points], True, (0,255,255))
cv2.imshow('dat', dat)
cv2.waitKey(0)

Insert picture description here

4. Text picture drawing

Draw text

cv2.putText(img, text, org, fontFace, fontScale, color,thickness,lineType)

import cv2
import numpy as np
img = cv2.imread('img.jpg', 1)
font = cv2.FONT_HERSHEY_SIMPLEX

#2 text 3 坐标起点 4 字体类型 5 字体大小 6 颜色 7粗细 8 linetype
cv2.putText(img,"this is flower",(100, 160), font, 1, (200,100,255),2,cv2.LINE_AA)
cv2.imshow('img', img)
cv2.waitKey(0)

Insert picture description here

Draw pictures

import cv2
import numpy as np
img = cv2.imread('img.jpg', 1)
height = int(img.shape[0] * 0.3)
width = int(img.shape[1] * 0.3)
imgResize = cv2.resize(img, (width, height))
for i in range(height):
    for j in range(width):
        img[i + 125, j + 125] = imgResize[i, j]
cv2.imshow('img', img)
cv2.waitKey(0)

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43328040/article/details/109094429