python opencv多路视频畸变矫正与显示

用于测试的计算机配置如下:

计算机为八核Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz

注意:文中所说的cpu使用率是指该算法占用的cpu使用率

测试用的视频规格为1920*1080

做一路视频的去除畸变

cpu的使用率为126.9%

多路视频去除畸变显示

三路视频去除畸变显示代码如下:

import cv2

import numpy as np

from numpy.core.fromnumeric import reshape

import queue

import multiprocessing

import time

q = queue.Queue()

#savedir="camera_data/"

def ClearBackGround(img):

    gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #灰度图

    height, width = gray_img.shape #获取图片宽高

    (_, blur_img) = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY) #二值化 固定阈值127

    

    #去除黑色背景,seedPoint代表初始种子,进行四次,即对四个角都做一次,可去除最外围的黑边

    blur_img = cv2.floodFill(blur_img,mask=None,seedPoint=(0,0),newVal=(255,255,255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(0,height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, 0), newVal=(255, 255, 255))[1]

    #blur_img = cv2.cvtColor(blur_img, cv2.COLOR_GRAY2RGB)

    w1,h1=blur_img.shape[1],blur_img.shape[0]

    w2,h2=img.shape[1],img.shape[0]

    w=w2-w1

    h=h2-h1

    w_left=int(w/2)

    h_b=int(h/3)

    re_img=img[h_b:h1-5*h_b,w_left:w1+w_left]

    cv2.imshow("clear_background", re_img)

    return re_img

def remove_the_blackborder(image):

    #image = cv2.imread(image)      #读取图片

    img = cv2.medianBlur(image, 5) #中值滤波,去除黑色边际中可能含有的噪声干扰

    b = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY) #调整裁剪效果

    binary_image = b[1]            #二值图--具有三通道

    binary_image = cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)

    # print(binary_image.shape)     #改为单通道

    edges_y, edges_x = np.where(binary_image==255) ##h, w

    bottom = min(edges_y)

    top = max(edges_y)

    #height = top - bottom

    left = min(edges_x)

    right = max(edges_x)

    height = top -bottom

    width = right -left

    res_image = image[bottom:bottom+height, left:left+width]

    #plt.figure()

    #plt.subplot(1,2,1)

    #plt.imshow(image)

    #plt.subplot(1,2,2)

    #plt.imshow(res_image)

    #plt.savefig(os.path.join("res_combine.jpg"))

    #plt.show()

    return res_image

def video_demo1(interval):

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./camera_data/1.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo2(interval):

    savedir="camera_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013634000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo3(interval):

    savedir="camera_data/history_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013649000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

if __name__ == "__main__":

    p1 = multiprocessing.Process(target = video_demo1, args = (2,))

    p2 = multiprocessing.Process(target = video_demo2, args = (3,))

    p3 = multiprocessing.Process(target = video_demo3, args = (4,))

    p1.start()

    p2.start()

    p3.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))

    for p in multiprocessing.active_children():

        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))

    print("END!!!!!!!!!!!!!!!!!")

'''video_demo()

cap2 = cv2.VideoCapture("./camera_data/outpy3.avi.mp4")

ret2, frame2 = cap2.read()

frame2 = remove_the_blackborder(frame2)

fps, w, h = 30, frame2.shape[1], frame2.shape[0]

out2 = cv2.VideoWriter('./camera_data/outpy_qhb3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (w, h))

while True:

    ret2, frame2 = cap2.read()

    frame2 = remove_the_blackborder(frame2)

    cv2.imshow('capture', frame2)

    key= cv2.waitKey(1)

    if key & 0xff == ord('q'):

            break

    out2.write(frame2)

cv2.destroyAllWindows()'''

没有卡顿

cpu的使用率为442.9%

增加到四路

代码如下:

import cv2

import numpy as np

from numpy.core.fromnumeric import reshape

import queue

import multiprocessing

import time

q = queue.Queue()

#savedir="camera_data/"

def ClearBackGround(img):

    gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #灰度图

    height, width = gray_img.shape #获取图片宽高

    (_, blur_img) = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY) #二值化 固定阈值127

    

    #去除黑色背景,seedPoint代表初始种子,进行四次,即对四个角都做一次,可去除最外围的黑边

    blur_img = cv2.floodFill(blur_img,mask=None,seedPoint=(0,0),newVal=(255,255,255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(0,height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, 0), newVal=(255, 255, 255))[1]

    #blur_img = cv2.cvtColor(blur_img, cv2.COLOR_GRAY2RGB)

    w1,h1=blur_img.shape[1],blur_img.shape[0]

    w2,h2=img.shape[1],img.shape[0]

    w=w2-w1

    h=h2-h1

    w_left=int(w/2)

    h_b=int(h/3)

    re_img=img[h_b:h1-5*h_b,w_left:w1+w_left]

    cv2.imshow("clear_background", re_img)

    return re_img

def remove_the_blackborder(image):

    #image = cv2.imread(image)      #读取图片

    img = cv2.medianBlur(image, 5) #中值滤波,去除黑色边际中可能含有的噪声干扰

    b = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY) #调整裁剪效果

    binary_image = b[1]            #二值图--具有三通道

    binary_image = cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)

    # print(binary_image.shape)     #改为单通道

    edges_y, edges_x = np.where(binary_image==255) ##h, w

    bottom = min(edges_y)

    top = max(edges_y)

    #height = top - bottom

    left = min(edges_x)

    right = max(edges_x)

    height = top -bottom

    width = right -left

    res_image = image[bottom:bottom+height, left:left+width]

    #plt.figure()

    #plt.subplot(1,2,1)

    #plt.imshow(image)

    #plt.subplot(1,2,2)

    #plt.imshow(res_image)

    #plt.savefig(os.path.join("res_combine.jpg"))

    #plt.show()

    return res_image

def video_demo1(interval):

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo2(interval):

    savedir="camera_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013634000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo3(interval):

    savedir="camera_data/history_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013649000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo4(interval):

    savedir="camera_data/xididasha_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013697000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

if __name__ == "__main__":

    p1 = multiprocessing.Process(target = video_demo1, args = (2,))

    p2 = multiprocessing.Process(target = video_demo2, args = (3,))

    p3 = multiprocessing.Process(target = video_demo3, args = (4,))

    p4 = multiprocessing.Process(target = video_demo4, args = (5,))

    p1.start()

    p2.start()

    p3.start()

    p4.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))

    for p in multiprocessing.active_children():

        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))

    print("END!!!!!!!!!!!!!!!!!")

'''video_demo()

cap2 = cv2.VideoCapture("./camera_data/outpy3.avi.mp4")

ret2, frame2 = cap2.read()

frame2 = remove_the_blackborder(frame2)

fps, w, h = 30, frame2.shape[1], frame2.shape[0]

out2 = cv2.VideoWriter('./camera_data/outpy_qhb3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (w, h))

while True:

    ret2, frame2 = cap2.read()

    frame2 = remove_the_blackborder(frame2)

    cv2.imshow('capture', frame2)

    key= cv2.waitKey(1)

    if key & 0xff == ord('q'):

            break

    out2.write(frame2)

cv2.destroyAllWindows()'''

运行结果:

视频有些许卡顿

cpu的使用率为594.7%

增加到6路视频

代码如下:

import cv2

import numpy as np

from numpy.core.fromnumeric import reshape

import queue

import multiprocessing

import time

q = queue.Queue()

#savedir="camera_data/"

def ClearBackGround(img):

    gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #灰度图

    height, width = gray_img.shape #获取图片宽高

    (_, blur_img) = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY) #二值化 固定阈值127

    

    #去除黑色背景,seedPoint代表初始种子,进行四次,即对四个角都做一次,可去除最外围的黑边

    blur_img = cv2.floodFill(blur_img,mask=None,seedPoint=(0,0),newVal=(255,255,255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(0,height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, 0), newVal=(255, 255, 255))[1]

    #blur_img = cv2.cvtColor(blur_img, cv2.COLOR_GRAY2RGB)

    w1,h1=blur_img.shape[1],blur_img.shape[0]

    w2,h2=img.shape[1],img.shape[0]

    w=w2-w1

    h=h2-h1

    w_left=int(w/2)

    h_b=int(h/3)

    re_img=img[h_b:h1-5*h_b,w_left:w1+w_left]

    cv2.imshow("clear_background", re_img)

    return re_img

def remove_the_blackborder(image):

    #image = cv2.imread(image)      #读取图片

    img = cv2.medianBlur(image, 5) #中值滤波,去除黑色边际中可能含有的噪声干扰

    b = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY) #调整裁剪效果

    binary_image = b[1]            #二值图--具有三通道

    binary_image = cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)

    # print(binary_image.shape)     #改为单通道

    edges_y, edges_x = np.where(binary_image==255) ##h, w

    bottom = min(edges_y)

    top = max(edges_y)

    #height = top - bottom

    left = min(edges_x)

    right = max(edges_x)

    height = top -bottom

    width = right -left

    res_image = image[bottom:bottom+height, left:left+width]

    #plt.figure()

    #plt.subplot(1,2,1)

    #plt.imshow(image)

    #plt.subplot(1,2,2)

    #plt.imshow(res_image)

    #plt.savefig(os.path.join("res_combine.jpg"))

    #plt.show()

    return res_image

def video_demo1(interval):

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo2(interval):

    savedir="camera_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013634000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo3(interval):

    savedir="camera_data/history_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013649000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo4(interval):

    savedir="camera_data/xididasha_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013697000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo5(interval):

    savedir="camera_data/960*540/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013728000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo6(interval):

    savedir="camera_data/640*360/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013785000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

if __name__ == "__main__":

    p1 = multiprocessing.Process(target = video_demo1, args = (2,))

    p2 = multiprocessing.Process(target = video_demo2, args = (3,))

    p3 = multiprocessing.Process(target = video_demo3, args = (4,))

    p4 = multiprocessing.Process(target = video_demo4, args = (5,))

    p5 = multiprocessing.Process(target = video_demo5, args = (6,))

    p6 = multiprocessing.Process(target = video_demo6, args = (7,))

    p1.start()

    p2.start()

    p3.start()

    p4.start()

    p5.start()

    p6.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))

    for p in multiprocessing.active_children():

        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))

    print("END!!!!!!!!!!!!!!!!!")

'''video_demo()

cap2 = cv2.VideoCapture("./camera_data/outpy3.avi.mp4")

ret2, frame2 = cap2.read()

frame2 = remove_the_blackborder(frame2)

fps, w, h = 30, frame2.shape[1], frame2.shape[0]

out2 = cv2.VideoWriter('./camera_data/outpy_qhb3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (w, h))

while True:

    ret2, frame2 = cap2.read()

    frame2 = remove_the_blackborder(frame2)

    cv2.imshow('capture', frame2)

    key= cv2.waitKey(1)

    if key & 0xff == ord('q'):

            break

    out2.write(frame2)

cv2.destroyAllWindows()'''

运行结果如下

视频延迟比较高

cpu使用率为563.2%

添加代码统计耗时

每帧图片处理耗时大概在450ms左右,显示起来非常卡顿,延时非常严重

发现视频是在去除畸变cv2.undistort这一步耗时较多

于是更换为remap进行尝试

代码如下:

import cv2

import numpy as np

from numpy.core.fromnumeric import reshape

import queue

import multiprocessing

import time

q = queue.Queue()

#savedir="camera_data/"

def ClearBackGround(img):

    gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #灰度图

    height, width = gray_img.shape #获取图片宽高

    (_, blur_img) = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY) #二值化 固定阈值127

    

    #去除黑色背景,seedPoint代表初始种子,进行四次,即对四个角都做一次,可去除最外围的黑边

    blur_img = cv2.floodFill(blur_img,mask=None,seedPoint=(0,0),newVal=(255,255,255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(0,height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, 0), newVal=(255, 255, 255))[1]

    #blur_img = cv2.cvtColor(blur_img, cv2.COLOR_GRAY2RGB)

    w1,h1=blur_img.shape[1],blur_img.shape[0]

    w2,h2=img.shape[1],img.shape[0]

    w=w2-w1

    h=h2-h1

    w_left=int(w/2)

    h_b=int(h/3)

    re_img=img[h_b:h1-5*h_b,w_left:w1+w_left]

    cv2.imshow("clear_background", re_img)

    return re_img

def remove_the_blackborder(image):

    #image = cv2.imread(image)      #读取图片

    img = cv2.medianBlur(image, 5) #中值滤波,去除黑色边际中可能含有的噪声干扰

    b = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY) #调整裁剪效果

    binary_image = b[1]            #二值图--具有三通道

    binary_image = cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)

    # print(binary_image.shape)     #改为单通道

    edges_y, edges_x = np.where(binary_image==255) ##h, w

    bottom = min(edges_y)

    top = max(edges_y)

    #height = top - bottom

    left = min(edges_x)

    right = max(edges_x)

    height = top -bottom

    width = right -left

    res_image = image[bottom:bottom+height, left:left+width]

    #plt.figure()

    #plt.subplot(1,2,1)

    #plt.imshow(image)

    #plt.subplot(1,2,2)

    #plt.imshow(res_image)

    #plt.savefig(os.path.join("res_combine.jpg"))

    #plt.show()

    return res_image

def video_demo1(interval):

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        #w,h = frame.shape[1],frame.shape[0]

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        q.put(undst)

        undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo2(interval):

    savedir="camera_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013634000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        #w,h = frame.shape[1],frame.shape[0]

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo3(interval):

    savedir="camera_data/history_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013649000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo4(interval):

    savedir="camera_data/xididasha_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013697000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo5(interval):

    savedir="camera_data/960*540/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013728000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo6(interval):

    savedir="camera_data/640*360/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013785000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

if __name__ == "__main__":

    p1 = multiprocessing.Process(target = video_demo1, args = (2,))

    p2 = multiprocessing.Process(target = video_demo2, args = (3,))

    p3 = multiprocessing.Process(target = video_demo3, args = (4,))

    p4 = multiprocessing.Process(target = video_demo4, args = (5,))

    p5 = multiprocessing.Process(target = video_demo5, args = (6,))

    p6 = multiprocessing.Process(target = video_demo6, args = (7,))

    p1.start()

    p2.start()

    p3.start()

    p4.start()

    p5.start()

    p6.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))

    for p in multiprocessing.active_children():

        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))

    print("END!!!!!!!!!!!!!!!!!")

'''video_demo()

cap2 = cv2.VideoCapture("./camera_data/outpy3.avi.mp4")

ret2, frame2 = cap2.read()

frame2 = remove_the_blackborder(frame2)

fps, w, h = 30, frame2.shape[1], frame2.shape[0]

out2 = cv2.VideoWriter('./camera_data/outpy_qhb3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (w, h))

while True:

    ret2, frame2 = cap2.read()

    frame2 = remove_the_blackborder(frame2)

    cv2.imshow('capture', frame2)

    key= cv2.waitKey(1)

    if key & 0xff == ord('q'):

            break

    out2.write(frame2)

cv2.destroyAllWindows()'''

结果显示如下:

运行速度提高了很多,cpu的使用率为716.2%

添加代码统计每帧图片耗时

每帧图片运行耗时基本在140ms左右,对比之前耗时减少了很多

虽然耗时减少了,但延时很严重

对处理的摄像头图片进行缩放处理(缩小1.5倍)

运行结果如下:

算法对摄像头(video1)每帧图片处理耗时为80ms左右,cpu使用率为715.6%,对每帧图片处理延时依然严重

于是继续对代码进行修改,在缩图的基础上尝试使用多线程

代码如下:

import cv2

import numpy as np

from numpy.core.fromnumeric import reshape

import queue

import multiprocessing

import time

import threading

q = queue.Queue()

#savedir="camera_data/"

def ClearBackGround(img):

    gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #灰度图

    height, width = gray_img.shape #获取图片宽高

    (_, blur_img) = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY) #二值化 固定阈值127

    

    #去除黑色背景,seedPoint代表初始种子,进行四次,即对四个角都做一次,可去除最外围的黑边

    blur_img = cv2.floodFill(blur_img,mask=None,seedPoint=(0,0),newVal=(255,255,255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(0,height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, 0), newVal=(255, 255, 255))[1]

    #blur_img = cv2.cvtColor(blur_img, cv2.COLOR_GRAY2RGB)

    w1,h1=blur_img.shape[1],blur_img.shape[0]

    w2,h2=img.shape[1],img.shape[0]

    w=w2-w1

    h=h2-h1

    w_left=int(w/2)

    h_b=int(h/3)

    re_img=img[h_b:h1-5*h_b,w_left:w1+w_left]

    cv2.imshow("clear_background", re_img)

    return re_img

def remove_the_blackborder(image):

    #image = cv2.imread(image)      #读取图片

    img = cv2.medianBlur(image, 5) #中值滤波,去除黑色边际中可能含有的噪声干扰

    b = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY) #调整裁剪效果

    binary_image = b[1]            #二值图--具有三通道

    binary_image = cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)

    # print(binary_image.shape)     #改为单通道

    edges_y, edges_x = np.where(binary_image==255) ##h, w

    bottom = min(edges_y)

    top = max(edges_y)

    #height = top - bottom

    left = min(edges_x)

    right = max(edges_x)

    height = top -bottom

    width = right -left

    res_image = image[bottom:bottom+height, left:left+width]

    #plt.figure()

    #plt.subplot(1,2,1)

    #plt.imshow(image)

    #plt.subplot(1,2,2)

    #plt.imshow(res_image)

    #plt.savefig(os.path.join("res_combine.jpg"))

    #plt.show()

    return res_image

def video_demo1(interval):

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap.set(cv2.CAP_PROP_FPS, 8)

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        start_time = time.time()

        cap.set(cv2.CAP_PROP_FPS, 7)

        ret, frame = cap.read()

        mid_time = time.time()

        print('解码耗时:',mid_time-start_time)

        if not ret:

            continue

        #w,h = frame.shape[1],frame.shape[0]

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        q.put(undst)

        undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        end_time = time.time()

        print('video1 FPS:',cap.get(5))

        print('video1 耗时:',end_time-start_time)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo1_encode():

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap.set(cv2.CAP_PROP_FPS, 8)

    ret, frame = cap.read()

    #fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    q.put(frame)

    print('video1 FPS:',cap.get(5))

    while ret:

        start_time = time.time()

        ret, frame = cap.read()

        q.put(frame)

        end_time = time.time()

        print('摄像头解码耗时:',end_time-start_time)

def video_demo1_undist():

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    frame=q.get()

    fps, w, h = 30, int(frame.shape[1]/1.5), int(frame.shape[0]/1.5)

    while 1:

        start_time = time.time()

        if q.empty() !=True:

            frame=q.get()

            mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

            undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

            cv2.imshow('capture', undst)

            key= cv2.waitKey(1)

            end_time = time.time()

            #print('video1 FPS:',cap.get(5))

            print('video1 耗时:',end_time-start_time)

            if key & 0xff == ord('q'):

                break

    cap.release()

def video_demo1_run(interval):

     d1=threading.Thread(target=video_demo1_encode)

     d2=threading.Thread(target=video_demo1_undist)

     d1.start()

     d2.start()

def video_demo2(interval):

    savedir="camera_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013634000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        start_time = time.time()

        ret, frame = cap.read()

        #w,h = frame.shape[1],frame.shape[0]

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        end_time = time.time()

        #print('video2 FPS:',cap.get(5))

        print('video2 耗时:',end_time-start_time)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo3(interval):

    savedir="camera_data/history_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013649000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo4(interval):

    savedir="camera_data/xididasha_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap=cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013697000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo5(interval):

    savedir="camera_data/960*540/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013728000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo6(interval):

    savedir="camera_data/640*360/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013785000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

if __name__ == "__main__":

    p1 = multiprocessing.Process(target = video_demo1_run, args = (2,))

    p2 = multiprocessing.Process(target = video_demo2, args = (3,))

    p3 = multiprocessing.Process(target = video_demo3, args = (4,))

    p4 = multiprocessing.Process(target = video_demo4, args = (5,))

    p5 = multiprocessing.Process(target = video_demo5, args = (6,))

    p6 = multiprocessing.Process(target = video_demo6, args = (7,))

    p1.start()

    p2.start()

    p3.start()

    p4.start()

    p5.start()

    p6.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))

    for p in multiprocessing.active_children():

        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))

    print("END!!!!!!!!!!!!!!!!!")

'''video_demo()

cap2 = cv2.VideoCapture("./camera_data/outpy3.avi.mp4")

ret2, frame2 = cap2.read()

frame2 = remove_the_blackborder(frame2)

fps, w, h = 30, frame2.shape[1], frame2.shape[0]

out2 = cv2.VideoWriter('./camera_data/outpy_qhb3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (w, h))

while True:

    ret2, frame2 = cap2.read()

    frame2 = remove_the_blackborder(frame2)

    cv2.imshow('capture', frame2)

    key= cv2.waitKey(1)

    if key & 0xff == ord('q'):

            break

    out2.write(frame2)

cv2.destroyAllWindows()'''

运行结果如下:

对摄像头每帧的图片处理耗时在75ms左右,改善不明显,但延时得到了极大的改善,现在是实时显示

进行八路视频的畸变矫正显示

代码如下:

import cv2

import numpy as np

from numpy.core.fromnumeric import reshape

import queue

import multiprocessing

import time

import threading

q = queue.Queue()

#savedir="camera_data/"

def ClearBackGround(img):

    gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #灰度图

    height, width = gray_img.shape #获取图片宽高

    (_, blur_img) = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY) #二值化 固定阈值127

    

    #去除黑色背景,seedPoint代表初始种子,进行四次,即对四个角都做一次,可去除最外围的黑边

    blur_img = cv2.floodFill(blur_img,mask=None,seedPoint=(0,0),newVal=(255,255,255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(0,height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, 0), newVal=(255, 255, 255))[1]

    #blur_img = cv2.cvtColor(blur_img, cv2.COLOR_GRAY2RGB)

    w1,h1=blur_img.shape[1],blur_img.shape[0]

    w2,h2=img.shape[1],img.shape[0]

    w=w2-w1

    h=h2-h1

    w_left=int(w/2)

    h_b=int(h/3)

    re_img=img[h_b:h1-5*h_b,w_left:w1+w_left]

    cv2.imshow("clear_background", re_img)

    return re_img

def remove_the_blackborder(image):

    #image = cv2.imread(image)      #读取图片

    img = cv2.medianBlur(image, 5) #中值滤波,去除黑色边际中可能含有的噪声干扰

    b = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY) #调整裁剪效果

    binary_image = b[1]            #二值图--具有三通道

    binary_image = cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)

    # print(binary_image.shape)     #改为单通道

    edges_y, edges_x = np.where(binary_image==255) ##h, w

    bottom = min(edges_y)

    top = max(edges_y)

    #height = top - bottom

    left = min(edges_x)

    right = max(edges_x)

    height = top -bottom

    width = right -left

    res_image = image[bottom:bottom+height, left:left+width]

    #plt.figure()

    #plt.subplot(1,2,1)

    #plt.imshow(image)

    #plt.subplot(1,2,2)

    #plt.imshow(res_image)

    #plt.savefig(os.path.join("res_combine.jpg"))

    #plt.show()

    return res_image

def video_demo1(interval):

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap.set(cv2.CAP_PROP_FPS, 8)

    ret, frame = cap.read()

    fps, w, h = 30, int(frame.shape[1]/1.5), int(frame.shape[0]/1.5)

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        start_time = time.time()

        cap.set(cv2.CAP_PROP_FPS, 7)

        ret, frame = cap.read()

        mid_time = time.time()

        print('摄像头解码耗时:',mid_time-start_time)

        if not ret:

            continue

        #w,h = frame.shape[1],frame.shape[0]

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        q.put(undst)

        undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        end_time = time.time()

        print('video1 FPS:',cap.get(5))

        print('video1 处理耗时:',end_time-start_time)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo1_encode():

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap.set(cv2.CAP_PROP_FPS, 8)

    ret, frame = cap.read()

    #fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    q.put(frame)

    print('video1 FPS:',cap.get(5))

    while ret:

        start_time = time.time()

        ret, frame = cap.read()

        q.put(frame)

        end_time = time.time()

        print('摄像头解码耗时:',end_time-start_time)

def video_demo1_undist():

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    frame=q.get()

    fps, w, h = 30, int(frame.shape[1]/1.5), int(frame.shape[0]/1.5)

    while 1:

        start_time = time.time()

        if q.empty() !=True:

            frame=q.get()

            mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

            undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

            cv2.imshow('capture', undst)

            key= cv2.waitKey(1)

            end_time = time.time()

            #print('video1 FPS:',cap.get(5))

            print('video1 耗时:',end_time-start_time)

            if key & 0xff == ord('q'):

                break

    cap.release()

def video_demo1_run(interval):

     d1=threading.Thread(target=video_demo1_encode)

     d2=threading.Thread(target=video_demo1_undist)

     d1.start()

     d2.start()

def video_demo2(interval):

    savedir="camera_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013634000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        start_time = time.time()

        ret, frame = cap.read()

        #w,h = frame.shape[1],frame.shape[0]

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        end_time = time.time()

        #print('video2 FPS:',cap.get(5))

        print('video2 耗时:',end_time-start_time)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo3(interval):

    savedir="camera_data/history_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013649000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo4(interval):

    savedir="camera_data/xididasha_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013697000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo5(interval):

    savedir="camera_data/960*540/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013728000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo6(interval):

    savedir="camera_data/640*360/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013785000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo7(interval):

    savedir="camera_data/xididasha_data2/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013842000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo8(interval):

    savedir="camera_data/xididasha_data3/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013838000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    while 1:

        ret, frame = cap.read()

        mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

if __name__ == "__main__":

    #p1 = multiprocessing.Process(target = video_demo1, args = (2,))

    p1 = multiprocessing.Process(target = video_demo1_run, args = (2,))

    p2 = multiprocessing.Process(target = video_demo2, args = (3,))

    p3 = multiprocessing.Process(target = video_demo3, args = (4,))

    p4 = multiprocessing.Process(target = video_demo4, args = (5,))

    p5 = multiprocessing.Process(target = video_demo5, args = (6,))

    p6 = multiprocessing.Process(target = video_demo6, args = (7,))

    p7 = multiprocessing.Process(target = video_demo7, args = (8,))

    p8 = multiprocessing.Process(target = video_demo8, args = (9,))

    p1.start()

    p2.start()

    p3.start()

    p4.start()

    p5.start()

    p6.start()

    p7.start()

    p8.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))

    for p in multiprocessing.active_children():

        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))

    print("END!!!!!!!!!!!!!!!!!")

'''video_demo()

cap2 = cv2.VideoCapture("./camera_data/outpy3.avi.mp4")

ret2, frame2 = cap2.read()

frame2 = remove_the_blackborder(frame2)

fps, w, h = 30, frame2.shape[1], frame2.shape[0]

out2 = cv2.VideoWriter('./camera_data/outpy_qhb3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (w, h))

while True:

    ret2, frame2 = cap2.read()

    frame2 = remove_the_blackborder(frame2)

    cv2.imshow('capture', frame2)

    key= cv2.waitKey(1)

    if key & 0xff == ord('q'):

            break

    out2.write(frame2)

cv2.destroyAllWindows()'''

结果显示如下:

算法对摄像头(video1)每帧图片处理耗时在100ms左右,延时较为严重,同时电脑也存在卡顿现象

继续检查代码,发现

mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

这段代码可以放到循环外面提高运行效率

修改后的代码如下:

import cv2

import numpy as np

from numpy.core.fromnumeric import reshape

import queue

import multiprocessing

import time

import threading

q = queue.Queue()

#savedir="camera_data/"

def ClearBackGround(img):

    gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #灰度图

    height, width = gray_img.shape #获取图片宽高

    (_, blur_img) = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY) #二值化 固定阈值127

    

    #去除黑色背景,seedPoint代表初始种子,进行四次,即对四个角都做一次,可去除最外围的黑边

    blur_img = cv2.floodFill(blur_img,mask=None,seedPoint=(0,0),newVal=(255,255,255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(0,height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, height-1), newVal=(255, 255, 255))[1]

    blur_img = cv2.floodFill(blur_img, mask=None, seedPoint=(width-1, 0), newVal=(255, 255, 255))[1]

    #blur_img = cv2.cvtColor(blur_img, cv2.COLOR_GRAY2RGB)

    w1,h1=blur_img.shape[1],blur_img.shape[0]

    w2,h2=img.shape[1],img.shape[0]

    w=w2-w1

    h=h2-h1

    w_left=int(w/2)

    h_b=int(h/3)

    re_img=img[h_b:h1-5*h_b,w_left:w1+w_left]

    cv2.imshow("clear_background", re_img)

    return re_img

def remove_the_blackborder(image):

    #image = cv2.imread(image)      #读取图片

    img = cv2.medianBlur(image, 5) #中值滤波,去除黑色边际中可能含有的噪声干扰

    b = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY) #调整裁剪效果

    binary_image = b[1]            #二值图--具有三通道

    binary_image = cv2.cvtColor(binary_image,cv2.COLOR_BGR2GRAY)

    # print(binary_image.shape)     #改为单通道

    edges_y, edges_x = np.where(binary_image==255) ##h, w

    bottom = min(edges_y)

    top = max(edges_y)

    #height = top - bottom

    left = min(edges_x)

    right = max(edges_x)

    height = top -bottom

    width = right -left

    res_image = image[bottom:bottom+height, left:left+width]

    #plt.figure()

    #plt.subplot(1,2,1)

    #plt.imshow(image)

    #plt.subplot(1,2,2)

    #plt.imshow(res_image)

    #plt.savefig(os.path.join("res_combine.jpg"))

    #plt.show()

    return res_image

def video_demo1(interval):

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap.set(cv2.CAP_PROP_FPS, 8)

    ret, frame = cap.read()

    fps, w, h = 30, int(frame.shape[1]/1.5), int(frame.shape[0]/1.5)

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        start_time = time.time()

        cap.set(cv2.CAP_PROP_FPS, 7)

        ret, frame = cap.read()

        mid_time = time.time()

        print('摄像头解码耗时:',mid_time-start_time)

        if not ret:

            continue

        #w,h = frame.shape[1],frame.shape[0]

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        q.put(undst)

        undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        end_time = time.time()

        print('video1 FPS:',cap.get(5))

        print('video1 处理耗时:',end_time-start_time)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo1_encode():

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap.set(cv2.CAP_PROP_FPS, 8)

    ret, frame = cap.read()

    #fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    q.put(frame)

    print('video1 FPS:',cap.get(5))

    while ret:

        start_time = time.time()

        ret, frame = cap.read()

        q.put(frame)

        end_time = time.time()

        print('摄像头解码耗时:',end_time-start_time)

def video_demo1_undist():

    savedir="lzj_config/"

    newcam_mtx = np.load(savedir+'lzj_video_newcameramtx.npy')

    cam_mtx = np.load(savedir+'lzj_video_mtx.npy')

    distCoeffs = np.load(savedir+'lzj_dist.npy')

    frame=q.get()

    fps, w, h = 30, int(frame.shape[1]/1.5), int(frame.shape[0]/1.5)

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        start_time = time.time()

        if q.empty() !=True:

            frame=q.get()

            print('video W:',w)

            print('   video H:',h)

            #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

            undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

            cv2.imshow('capture', undst)

            key= cv2.waitKey(1)

            end_time = time.time()

            #print('video1 FPS:',cap.get(5))

            print('video1 耗时:',end_time-start_time)

            if key & 0xff == ord('q'):

                break

    cap.release()

def video_demo1_run(interval):

     d1=threading.Thread(target=video_demo1_encode)

     d2=threading.Thread(target=video_demo1_undist)

     d1.start()

     d2.start()

def video_demo2(interval):

    savedir="camera_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013634000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        start_time = time.time()

        ret, frame = cap.read()

        #w,h = frame.shape[1],frame.shape[0]

        print('video2 W:',w)

        print('   video2 H:',h)

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        end_time = time.time()

        #print('video2 FPS:',cap.get(5))

        print('video2 耗时:',end_time-start_time)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo3(interval):

    savedir="camera_data/history_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013649000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        ret, frame = cap.read()

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo4(interval):

    savedir="camera_data/xididasha_data/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013697000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        ret, frame = cap.read()

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo5(interval):

    savedir="camera_data/960*540/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013728000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        ret, frame = cap.read()

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo6(interval):

    savedir="camera_data/640*360/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013785000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        ret, frame = cap.read()

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo7(interval):

    savedir="camera_data/xididasha_data2/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013842000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        ret, frame = cap.read()

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

def video_demo8(interval):

    savedir="camera_data/xididasha_data3/"

    newcam_mtx = np.load(savedir+'newcam_mtx.npy')

    cam_mtx = np.load(savedir+'cam_mtx.npy')

    distCoeffs = np.load(savedir+'dist.npy')

    #cap = cv2.VideoCapture("rtsp://admin:admin123@192.168.1.39:554/stream1&channel=1")

    cap = cv2.VideoCapture("./video/2021-12-30/00000013838000000.mp4")

    ret, frame = cap.read()

    fps, w, h = 30, frame.shape[1], frame.shape[0]

    # ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=5000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000', cv2.CAP_GSTREAMER, fps, (w, h))

    #ab = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=10000 speed-preset=superfast ! h264parse ! flvmux ! rtmpsink location=rtmp://127.0.0.1:1935/rec/live', cv2.CAP_GSTREAMER, fps, (w, h))

    #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20, (w, h))

    mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

    while 1:

        ret, frame = cap.read()

        #mapx,mapy = cv2.initUndistortRectifyMap(cam_mtx,distCoeffs,None,newcam_mtx,(w,h),5)

        undst = cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR)

        #undst = cv2.undistort(frame, cam_mtx, distCoeffs, None, newcam_mtx)

        #undst=ClearBackGround(undst)

        #undst = remove_the_blackborder(undst)

        #w,h = undst.shape[1],undst.shape[0]

        #out = cv2.VideoWriter('./camera_data/outpy3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

        #q.put(undst)

        #undst = q.get()

        cv2.imshow('capture', undst)

        key= cv2.waitKey(1)

        if key & 0xff == ord('q'):

            break

        #out.write(undst)

        #ab.write(undst)

    cap.release()

if __name__ == "__main__":

    #p1 = multiprocessing.Process(target = video_demo1, args = (2,))

    p1 = multiprocessing.Process(target = video_demo1_run, args = (2,))

    p2 = multiprocessing.Process(target = video_demo2, args = (3,))

    p3 = multiprocessing.Process(target = video_demo3, args = (4,))

    p4 = multiprocessing.Process(target = video_demo4, args = (5,))

    p5 = multiprocessing.Process(target = video_demo5, args = (6,))

    p6 = multiprocessing.Process(target = video_demo6, args = (7,))

    #p7 = multiprocessing.Process(target = video_demo7, args = (8,))

    #p8 = multiprocessing.Process(target = video_demo8, args = (9,))

    p1.start()

    p2.start()

    p3.start()

    p4.start()

    p5.start()

    p6.start()

    #p7.start()

    #p8.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))

    for p in multiprocessing.active_children():

        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))

    print("END!!!!!!!!!!!!!!!!!")

'''video_demo()

cap2 = cv2.VideoCapture("./camera_data/outpy3.avi.mp4")

ret2, frame2 = cap2.read()

frame2 = remove_the_blackborder(frame2)

fps, w, h = 30, frame2.shape[1], frame2.shape[0]

out2 = cv2.VideoWriter('./camera_data/outpy_qhb3.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (w, h))

while True:

    ret2, frame2 = cap2.read()

    frame2 = remove_the_blackborder(frame2)

    cv2.imshow('capture', frame2)

    key= cv2.waitKey(1)

    if key & 0xff == ord('q'):

            break

    out2.write(frame2)

cv2.destroyAllWindows()'''

6路视频运行结果如下:

算法对摄像头每帧图片处理时间为45ms左右,改进后速度提高很多,cpu使用率为

改成八路视频

运行结果如下图

算法对摄像头每帧图片的处理时间为65ms左右,实时性也非常好,cpu的使用率为

cpu(八核)

memery(16G)

speed(平均)

摄像头延时

1路

126.9%

1.9%

正常

3路

442.9%

3.1%

正常

4路

594.7%

3.3%

严重

6路(undistort)

675.5%

7.4%

450ms

严重

6路(remap)

716.2%

8.3%

140ms

严重

6路(remap+缩图)

715.6%

8.3%

80ms

严重

6路(remap++缩图+多线程)

701.9%

9.7%

75ms

正常

6路(remap++缩图+多线程+代码改进)

706.7%

7.8%

45ms

正常

8路(remap++缩图+多线程)

727.1%

16.1%

100ms

较为严重,电脑卡顿严重(存在死机风险)

8路(remap++缩图+多线程+代码改进)

775.3%

11.4%

65ms

正常

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

搬砖者(视觉算法工程师)

绝对物超所值的干货

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

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

打赏作者

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

抵扣说明:

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

余额充值