opencv (一) 学习通过OpenCV图形界面及基础

opencv 学习通过OpenCV图形界面基础

用的函数有

cv.line(), cv.circle(),cv.rectangle(), cv.ellipse(),cv.putText()

常用参数

  • img : 想要绘制图形的图片
  • color: 图形的颜色, BGR
  • thickness:厚度
  • lineType: 线的类型, 8-connected、anti-aliased 等

绘制线段

import numpy as np
import cv2 as cv
# 创建一个黑色的图片
img = np.zeros((512,512,3), np.uint8)
# 绘制一个蓝色5px对角线
cv.line(img, (0, 0), (511, 511), (255, 0, 0), 5)

绘制矩形

cv.rectangle(img,(384, 0), (510, 128), (0,255,0), 3)

绘制圆

cv.circle(img,(447, 63), 63, (0, 0, 255), -1)

绘制椭圆

cv.ellipse(img, (256,256), (100, 50), 0, 0, 180, 255, -1)

绘制多边形

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))

Adding Text to Images:

font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV', (10, 500), font, 4, (255, 255, 255), 2, cv.LINE_AA)

opencv监听鼠标事件

import numpy as np
import cv2 as cv

events = [i for i in dir(cv) if 'EVENT' in i]
print(events) # 遍历所有鼠标事件

drawing = False
mode = True
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
    if event == cv.EVENT_LBUTTONDBLCLK:
        if drawing:
            if mode:
                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
        if mode:
            cv.rectangle(img, (ix, iy), (x, y), (0, 255, 0), -1)
        else:
            cv.circle(img, (x, y), 5, (0, 0, 255), -1)


img = np.zeros((512, 512, 3), np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image', draw_circle)

while 1:
    cv.imshow('image', img)
    k = cv.waitKey(1) & 0xFF
    if k == ord('m'):#监听键盘m键
        mode = not mode
    elif k == 27:#监听esc
        break
cv.destroyAllWindows()

opencv 绑定视图进度条

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)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('B', 'image', 0, 255, nothing)

switch = '0 : 0FF \n1 : ON'
cv.createTrackbar(switch, 'image', 0, 1, nothing)

while 1:
    cv.imshow('image', img)
    k = cv.waitKey(1) & 0xFF
    if k == 27:
        break
    r = cv.getTrackbarPos('R', 'image')
    g = cv.getTrackbarPos('G', 'image')
    b = cv.getTrackbarPos('B', 'image')
    s = cv.getTrackbarPos(switch, 'image')

    if s == 0:
        img[:] = 0
    else:
        img[:] = [b, g, r]

cv.destroyAllWindows()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值