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

这篇具有很好参考价值的文章主要介绍了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使用率为

改成八路视频

运行结果如下图

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

算法对摄像头每帧图片的处理时间为65ms左右,实时性也非常好,cpu的使用率为

cpu(八核)

memery(16G)

speed(平均)

摄像头延时

1路

126.9%

1.9%

正常文章来源地址https://www.toymoban.com/news/detail-405569.html

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

正常

到了这里,关于python opencv多路视频畸变矫正与显示的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 相机的畸变矫正与opencv代码说明

    图像算法中会经常用到摄像机的畸变校正,有必要总结分析OpenCV中畸变校正方法,其中包括普通针孔相机模型和鱼眼相机模型fisheye两种畸变校正方法。普通相机模型畸变校正函数针对OpenCV中的cv::initUndistortRectifyMap(),鱼眼相机模型畸变校正函数对应OpenCV中的cv::fisheye::initUndi

    2024年02月14日
    浏览(28)
  • opencv对相机进行畸变矫正,及从矫正后的图像坐标反求原来的对应坐标

    目前有个项目,需要用到热成像相机。但是这个热成像相机它的畸变比较厉害,因此需要用标定板进行标定,从而消除镜头畸变。 同时需要实现用户用鼠标点击校正后的画面后,显示用户点击位置的像素所代表的温度。 另外热成像sdk中还有个功能:选定一个rect,可以返回这

    2024年02月16日
    浏览(35)
  • OpenCvSharp (C# OpenCV) 二维码畸变矫正--基于透视变换(附源码)

        本文主要介绍如何使用OpenCvSharp中的透视变换来实现二维码的畸变矫正。     由于CSDN文章中贴二维码会导致显示失败,大家可以直接点下面链接查看图片:     C# OpenCV实现二维码畸变矫正--基于透视变换 (详细步骤 + 代码)      讲解实现步骤之前先看下效果(左边是原图

    2024年02月15日
    浏览(28)
  • OpenCV开发笔记(七十七):相机标定(二):通过棋盘标定计算相机内参矩阵矫正畸变摄像头图像

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/136616551 各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究 红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、

    2024年03月13日
    浏览(37)
  • 畸变矫正-深度学习相关论文学习

    目录 DocTr: Document Image Transformer for Geometric Unwarping and Illumination Correction SimFIR: A Simple Framework for Fisheye Image Rectification with Self-supervised Representation Learning Model-Free Distortion Rectification Framework Bridged by Distortion Distribution Map A Deep Ordinal Distortion Estimation Approach for Distortion Rectification Mul

    2024年02月01日
    浏览(34)
  • 无人机红外相机的畸变矫正

    在项目开展过程中,发现大疆M30T的红外相机存在比较明显的畸变问题,因此需要对红外图像进行畸变矫正。在资料检索过程中,发现对红外无人机影像矫正的资料较少,对此,我从相机的成像原理角度出发,探索出一种效果尚可的解决思路,遂以本文记录如下。 目前采用的

    2024年02月04日
    浏览(40)
  • 相机的内外参数标定和畸变矫正原理和代码

    相机的成像过程实质上是坐标系转换。首先空间中的点坐标由世界坐标系转换到相机坐标系,然后将其投影到成像平面(图像物理坐标系),最后再将成像平面上的数据转换到图像像素坐标系。但是由于透镜制造精度及组装工艺的差别会引入畸变,导致原始图像的失真。镜头

    2024年04月16日
    浏览(26)
  • 六.图像处理与光学之镜头畸变矫正LDC

    海思35xx芯片专门有一个模块GDC(Geometry Distortion Correction)来对畸变图片进行校正。它里面其实有两个应用case,一个是fisheye校正,另外一个是LDC镜头畸变校正。 畸变主要存在与镜头FOV大的产生ÿ

    2024年02月11日
    浏览(30)
  • Python|OpenCV-读取视频,显示视频并保存视频(3)

    前言 本文是该专栏的第3篇,后面将持续分享OpenCV计算机视觉的干货知识,记得关注。 在使用OpenCV处理视频的时候,不论是摄像头画面还是视频文件,通常情况下都要使用VideoCapture类来进行每一帧图像的处理。对于OpenCV而言,只要使用视频文件作为参数,它就可以打开视频文

    2024年02月11日
    浏览(32)
  • Python OpenCV 图像矫正的原理与实现

    目录hw1下的图像是一些胶片的照片,请将其进行度量矫正。 推荐流程:采用 Canny算子 ,检测边缘点;采用 Hough直线检测 ,根据边缘点检测胶片边缘对应的4条直线;4条直线在图像平面中的交点为胶片图像的4个顶点。根据4个顶点与真实世界中胶片的位置(假设胶片图像长宽比

    2024年02月16日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包