"Far Frame Difference" Image Processing Algorithm Based on Periodic Motion of Camera Pan/Tilt

        The traditional frame difference method performs image processing on two frames before and after or a few frames. Once the camera angle of view rotates, it cannot obtain a good detection effect. The project I participated in is a gimbal equipped with an infrared camera for image capture. In this way The cost can be greatly saved, but the requirements for image processing algorithms are increased.

        This project does not have high real-time and accuracy of detection, so a far frame difference image processing algorithm based on the periodic motion of the camera pan/tilt is proposed. Because the image processing methods of different cores of the project are also different, this article hides the core of this project Processing algorithm, but the algorithm framework is completely retained, you only need to adjust the cycle video parameters, combined with your own core image processing algorithm, it can run smoothly.

        To put it simply, the number of shooting frames of a motion cycle of the gimbal, that is, the number of photos taken is fixed. Take the photos of one cycle and compare them with the photos of the next cycle. Although the time is different, they are different in space. The field of view of the shooting is consistent, and we can process these two pictures.

        Written at the end: When I first came into contact with cv2, it was difficult to install it successfully. It is also mentioned on its official website. There are many solutions on the Internet, and the problem cannot be solved after trying. Guessing has a lot to do with computer configuration, folder path, and downloaded version. Finally, I solved the problem by installing Conda. It is recommended to install directly: Conda + Pycharm, which is easy to manage and use.

        The original intention of writing this article is only for sharing, and I have also consulted the posts of some CSDN bigwigs, and I have learned something from it, so I would like to express my gratitude here!

# 必要的库需要导入,有的同学可能加载会有问题,最后再说
import cv2

# 参数为0就表示第1个摄像头;也可以直接输入需要检测的视频的地址;
camera = cv2.VideoCapture(0)
# 判断视频或摄像头是否打开:
if camera.isOpened():
    print('摄像头已打开')
else:
    print('摄像头未打开')
# 选择了一个形态学核:椭圆形;具体的图像处理算法根据自己的项目改变
es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9))
one_time_frames = 105  # 全局变量:一个云台运动周期的视频总帧数,可以自行设置
all_frames = []  # 全局变量创建一个空数组,用来储存一个周期的所有帧
test_count = 0  # 全局变量:对比次数
i = -1  # 全局变量:计数器
# 一个循环,用于读取图像并进行图像处理:
while True:
    # ret参数代表是否成功读入图像;frame参数代表传入的一帧图像
    ret, frame = camera.read()  # 读入摄像头或者视频的一帧
    if frame is None:  # 假如传入的是一段视频,此代码用来检测是否已经处理完成
        break
    if len(all_frames) == one_time_frames:  # 判断第一个周期的图像是否收集完成
        while True:
            cv2.imshow('2', frame)  # 展示当前周期视频
            cv2.waitKey(1)
            i = i + 1  # 计数器
            # frame 开始和 all_frames【i】(上一周期的帧图像)进行对比:
            # 对帧进行预处理,先转灰度图,再进行高斯滤波。
            # 用高斯滤波进行模糊处理,进行处理的原因:每个输入的视频都会因自然震动、光照变化或者摄像头本身等原因而产生噪声。对噪声进行平滑是为了避免在运动和跟踪时将其检测出来。
            gray_lwpCV = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            gray_lwpCV = cv2.GaussianBlur(gray_lwpCV, (21, 21), 0)
            # 对帧进行预处理,先转灰度图,再进行高斯滤波。
            # 用高斯滤波进行模糊处理,进行处理的原因:每个输入的视频都会因自然震动、光照变化或者摄像头本身等原因而产生噪声。对噪声进行平滑是为了避免在运动和跟踪时将其检测出来。
            gray_lwpCV_i = cv2.cvtColor(all_frames[i], cv2.COLOR_BGR2GRAY)
            all_frames[i] = ''  # 图像取出后进行删除操作,节约内存空间
            gray_lwpCV_i = cv2.GaussianBlur(gray_lwpCV_i, (21, 21), 0)
            diff = cv2.absdiff(gray_lwpCV_i, gray_lwpCV)
            diff = cv2.threshold(diff, 40, 255, cv2.THRESH_BINARY)[1]  # 二值化阈值处理,总之,这句话是把diff图像当中阈值高于40的部分转为255。
            diff = cv2.dilate(diff, es, iterations=2)  # 形态学膨胀操作
            # ......
            # 此处只是进行了简单的处理,根据你的项目在进行丰富
            # ......
            print("正在与上一周期的第:", i, "张图像进行对比:")  # 便于观察当前进程
            cv2.imshow('result', diff)  # 图像处理结果
            cv2.waitKey(1)
            test_count = test_count + 1
            break
    if len(all_frames) != one_time_frames:  # 收集一个周期内的视频图像放入all_frames数组
        cv2.imshow('1', frame)  # 一个窗口,展示一个周期内的视频,便于观察
        cv2.waitKey(1)  # 必要的cv2算子,保证我们能看到,而不是一闪而过
        all_frames.append(frame)  # 添加图像到数组中
        print("正在收集上一周期的第:", len(all_frames), "张图像,请稍后")  # 打印出当前数组的长度,便于观察当前进程
    if test_count == one_time_frames:
        all_frames = []
        test_count = 0
        i = -1
camera.release()  # 释放相机或者视频流
cv2.destroyAllWindows()  # 关闭所有窗口

There may be mistakes in the first post, please correct me.

Like it~

Guess you like

Origin blog.csdn.net/m0_63104578/article/details/126752148