Python进阶——OpenCV之GUI


有感于人工智能发展,现在开始学习Opencv关于计算机视觉的知识,又不想捣鼓C++代码,因此决定用Python来搞,此篇开始按照官网的教程开始学习,记录自己的学习历程,写一点笔记,方便以后查阅。
官方的教程:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html
GUI部分主要包括图像处理、视频处理、画图工具、GUI工具4个部分。

图像处理(Getting Started with Images)

学习使用cv2.imread(), cv2.imshow() , cv2.imwrite()这三个函数,以及Matplotlib库显示图像。

读取图像

cv2.imread(filename, flags=None)

import cv2
img = cv2.imread(r'/home/luke/图片/2018-08-09 11-58-52.jpg',cv2.IMREAD_COLOR)
# filename参数u用于定位图片,可以是当前路径或绝对路径,注意windows平台,字符串前加 r
# 第二项参数的默认值是:cv2.IMREAD_COLOR,读取色彩图像,还可以是
# cv2.IMREAD_GRAYSCALE : 读取灰度图像
# cv2.IMREAD_UNCHANGED : 读取图像包含 alpha channel

显示图像

cv2.imshow() 函数将图像显示到窗口,窗口大小自动适应图像大小。

cv2.imshow('image',img)
#参数1:窗口标题名称
#参数2:已读取的显示图像
cv2.waitKey(0)
# cv2.waitKey()函数等待任意按键, 
# 参数是等待超时时间,时间单位为ms;参数为0时,等待直到任意键按下,
# 返回值是按下的键值
cv2.destroyAllWindows()
# 销毁所有打开的窗口

保存图像

img = cv2.imread('/home/luke/图片/2018-07-19 19-47-33屏幕截图.png',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
#k = cv2.waitKey(0) & 0xFF
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv2.imwrite('/home/luke/图片/test.png',img)
    cv2.destroyAllWindows()

使用Matplotlib

使用Matplotlib库显示图像、缩放图像,保存图像,关于这个库的说明,需要另写文章。以下是简单示例

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('messi5.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()
# 注意:OpenCV图像存储格式为BGR模式,但是 Matplotlib 使用RGB模式;因此要想正确使用 Matplotlib库必须先进行转换。
# convert BGR image to RGB image
# cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# img2 = img[:,:,::-1]

视频处理(Getting Started with Videos)

主要学习函数:

  • cv2.VideoCapture(),
  • cv2.VideoWriter()

读取摄像头播放视频

要播放摄像头视频,需要先捕捉摄像头视频流,创造一个VideoCapture对象。

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture(/dev/video0)
#cap.read() returns a bool (True/False). If frame is read correctly, 
#it will be True. So you can check end of the video by checking this return value
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
  • cv2.VideoCapture()用于创建VideoCapture对象,参数可以使video设备序号,从0开始,也可以是设备名称/dev/video0cap 对象代表的摄像头节点可能还未被初始化,直接读取可能会报错可以使用cap.isOpened()函数判断,返回值为True,表示已打开;否则用cap.open()函数打开。
  • cap.get(propId)函数可以获取视频相关参数
  • cap.set(propId, vaule),设置视频相关参数
  • propId取值范围是0-21
  • CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
  • CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
  • CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
  • CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
  • CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
  • CV_CAP_PROP_FPS Frame rate.
  • CV_CAP_PROP_FOURCC 4-character code of codec.
  • CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
  • CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
  • CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
  • CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
  • CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
  • CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
  • CV_CAP_PROP_HUE Hue of the image (only for cameras).
  • CV_CAP_PROP_GAIN Gain of the image (only for cameras).
  • CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
  • CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
  • CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)

播放视频文件

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')
while(cap.isOpened()):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

注意:Make sure proper versions of ffmpeg or gstreamer is installed. Sometimes, it is a headache to work with Video Capture mostly due to wrong installation of ffmpeg/gstreamer.

保存视频文件

以下代码一遍播放一遍保存视频文件

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)
        # write the flipped frame,垂直方向翻转
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

cv2.VideoWriter()参数说明:

  • 参数1:存储的视频文件名称
  • 参数2:视频文件格式FourCC, 4-byte 的编码格式,In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
    In Windows: DIVX (More to be tested and added)
  • 参数3:视频文件的帧率大小,每秒多少帧
  • 参数4:元组格式(640, 320),表示视频文件画面的大小

OpenCV画图函数

基本函数

  • cv2.line(),
  • cv2.circle() ,
  • cv2.rectangle(),
  • cv2.ellipse(),
  • cv2.putText()

基本参数

  • img : 被作图的图像载体
  • color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness : Thickness of the line or circle etc. If -1 is passed for closed figures like circles, it will fill the shape. default thickness = 1
  • lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves.

画线

首先创建全黑图像,然后给出起点与终点座标,线的颜色、厚度
以下函数画一条从左上角到右下角的蓝色线

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a diagonal blue line with thickness of 5 px
img = cv2.line(img,(0,0),(511,511),(255,0,0),5)

画矩形

声明矩形的左上角座标、右下角座标

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

画圆

声明圆心点座标、半径、颜色

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

画椭圆

椭圆参数:

  • 圆心座标 (x,y)
  • a、b长短轴长度 (major axis length, minor axis length)
  • 作图方向,逆时针,起点角度与终点角度 0 到 360 g画出完整椭圆
img = cv2.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))
img = cv2.polylines(img,[pts],True,(0,255,255))

注意:If third argument is False, you will get a polylines joining all the points, not a closed shape.
注意:cv2.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is more better and faster way to draw a group of lines than calling cv2.line() for each line.

添加文字

向图像添加文字需要声明以下参数

  • 文字内容
  • 文字起点座标,起始位置为左下角
  • 字体类型
  • 字体大小
  • 其它参数:color, thickness, lineType, lineType = cv2.LINE_AA 推荐使用.
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

以上步骤最终图像

在这里插入图片描述

鼠标作为画图刷

学习函数:cv2.setMouseCallback()

简单示例

  • 1、首先创建鼠标事件回调函数,每一个鼠标事件给出一个座标(x,y),根据此座标与对应的鼠标EVENT,灰回调函数可以实现任何事情
  • 2、鼠标事件EVENT:['EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP']
  • 3、以下代码,设置鼠标双击左键事件,以此事件的座标点换一个圆
import cv2
import numpy as np

# mouse callback function
def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),100,(255,0,0),-1)

# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

高级示例

鼠标回调函数绑定多个事件,既可以画圆也可以画矩形

import cv2
import numpy as np

drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1

# mouse callback function
def draw_circle(event,x,y,flags,param):
    global ix,iy,drawing,mode

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix,iy = x,y

    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            if mode == True:
                cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
            else:
                cv2.circle(img,(x,y),5,(0,0,255),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        if mode == True:
            cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
        else:
            cv2.circle(img,(x,y),5,(0,0,255),-1)

主循环检测“m”键,是否按下,用以切换画圆还是画矩形
鼠标左键按下使能画图,并拖动开始画图

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

while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('m'):
        mode = not mode
    elif k == 27:
        break

cv2.destroyAllWindows()

移动条调色板

主要学习函数

  • cv2.createTrackbar()
  • cv2.getTrackbarPos(),参数1,trackbar名称,参数2,附着窗口名称,参数3,默认值,参数4,最大值,参数5,回调函数,移动条value变化时出发执行,此示例中,只需trackbar的value值,因此回调函数什么也不做
  • 此示例有4个移动条,分别用来选择R、G、B;以及On/Off选择
  • trackbar的value取值只有0与1时,可以作为switch开关,此示例中用来决定是否显色调色的后的颜色,value为0 时,调色板为黑色
import cv2
import numpy as np

def nothing(x):
    pass

# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)

# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)

while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

    # get current positions of four trackbars
    r = cv2.getTrackbarPos('R','image')
    g = cv2.getTrackbarPos('G','image')
    b = cv2.getTrackbarPos('B','image')
    s = cv2.getTrackbarPos(switch,'image')

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

cv2.destroyAllWindows()

好了,本片就这么多,继续学习!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章