Opencv 绘图

绘制形状

黑色背景板

import numpy as np
import cv2 as cv
img = np.zeros((512,512,3),np.uint8)

在这里插入图片描述

斜线

cv.line(img,(0,0),(512,512),(255,0,0),5)
cv.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
#pt1:第一个点的坐标
#pt2:第二个点的坐标
#color:颜色
#thickness:粗细
#linType:线段类型
#shift:精确位数

在这里插入图片描述

矩形

cv.rectangle(img,(384,0),(510,128),(0,255),-1)
#cv2.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
#同上

在这里插入图片描述

圆形

cv.circle(img,(477,63),63,(0,0,255),-1)
#圆心、半径、颜色、粗细(-1为实心)

在这里插入图片描述

椭圆

cv.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
#ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
#img:输入
#center:圆心
#axes:轴长(长周半径,短轴半径)
#angle:椭圆沿水平方向逆时针旋转的角度
#startAngle:沿长轴顺时针方向开始显示的角度
#endAngle:沿长轴顺时针结束显示的角度
#其他同上

在这里插入图片描述

不规则图形

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))
#polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])
#img:输入
#pts:包含多边形上点的数组
#isClosed:标志,决定所绘制的多边形是否闭合。若为 True ,则画若干个闭合多边形;若为 False ,则画一条连接所有点的折线

在这里插入图片描述

文字

font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
#    cv2.putText(src, text, place, Font, Font_Size, Font_Color, Font_Overstriking)
#src:输入
#test:文本
#place:坐标
#Font:字体
#Font_Size:大小
#Font_Color:颜色
#Font_Overstriking:粗细

在这里插入图片描述

用鼠标绘制

import numpy as np
import cv2 as cv
drawing = False # 如果按下鼠标,则为真
mode = True #设定的一个标志位,用来判断画笔形状的切换,True为矩形,False为圆形

ix,iy = -1,-1
# 鼠标回调函数
def draw_circle(event,x,y,flags,param):
    global ix,iy,drawing,mode
    if event == cv.EVENT_LBUTTONDOWN:#双击鼠标左键
        drawing = True
        ix,iy = x,y
    
    elif event == cv.EVENT_MBUTTONDBLCLK:#双击鼠标滚轮,将mode置负
        mode = False
    elif event == cv.EVENT_MBUTTONDOWN:#单击滚轮置正
        mode = True
    elif event == cv.EVENT_MOUSEMOVE:#拖动鼠标
        if drawing == True:               
            if mode == True:
                cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
            else:
                cv.circle(img,(x,y),5,(0,0,255),-1)
                
    elif event == cv.EVENT_LBUTTONUP:
        drawing = False

img = np.zeros((512,512,3), np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
#三个参数:窗口名称,响应函数,鼠标响应处理函数的ID,识别号(这里我忽略了)
while(1):
    cv.imshow('image',img)      
    if cv.waitKey(20) & 0xFF == 27:
        break
cv.destroyAllWindows()


在这里插入图片描述

调色板

import numpy as np
import cv2 as cv
def nothing (x):
    pass
img = np.zeros((300,512,3), np.uint8)
cv.namedWindow('image')

cv.createTrackbar('R','image',0,255,nothing)
#创造一个滑动条 6个参数分别为:滑动条名称,窗口名,滑块最小值,最大值
#onChange:用来接收回调函数函数名的,默认值为0;
#userdata:这个参数是用户传给回调函数的数据,用来处理轨迹条事件,默认值为0。
cv.createTrackbar('G','image',0,255,nothing)
cv.createTrackbar('B','image',0,255,nothing)
switch = '0 /1'
print(switch)
cv.createTrackbar(switch,'image',0,1,nothing)
while(1):
    cv.imshow('image',img)
    k = cv.waitKey(1)
    if k == 27:
        break
    r = cv.getTrackbarPos('R','image')
    g = cv.getTrackbarPos('G','image')
    b = cv.getTrackbarPos('B','image')
    s = cv.getTrackbarPos(switch,'image')
#此函数的作用:配合createTrackbar使用,获取当前滑块的位置。
#函数原型: getTrackbarPos( trackbarname, winname)
#trackbarname:轨迹条的名字。
#winname:轨迹条所在窗口的名字。
    if s == 0:
        img[:] = 0
    else :
        img[:] = [b,g,r]
cv.destroyAllWindows()        
img[:]=[1,2,3]

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NAND_LU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值