[PYTHON] I tried image processing like an event camera

Overview

There is a camera called an event camera. I would like to convert a normal video like this event camera. I wonder if it can be used for something.

Deliverables

Any video was fine, but with a fixed camera, it can be taken beautifully. This time I tried a shot of Tiger Woods from the data of GolfDB

What is an event camera?

Event cameras have a fundamentally different mechanism than ordinary cameras. A typical camera has a dimension of H × W × C from any C of height H, width W, and RGB. If it is a video, it will be (T x H x W x C).

image.png

On the other hand, the event camera only records events. Here, the event is "change in brightness value". In other words, only the "points" whose brightness value has changed by the threshold value or more in the time direction are recorded. It is T × (X, Y, P) to record. P is 1 when the brightness value increases and -1 when the brightness value decreases. The feature is that the place where there is no change is not recorded.

image.png

Event camera features

By the way, this camera that records only events has the following features.

・ Ultra high frame rate ·Low power consumption ・ Asynchronous

While ordinary cameras have around 60 FPS, event cameras will exceed 1000 FPS. With RGB cameras, the higher the FPS, the less light that enters the camera and the darker it becomes, but with event cameras, the amount of incoming light changes or does not change, so you can maintain a high FPS. .. Low power because there is little data to record. Also, with a normal camera, there is data at a constant pace of 1F at 0.03 seconds and 2F at 0.06 seconds, but in the case of an event camera, there is data. There is a change at (100,200) at 0.0012 seconds There is a change at (250, 50) at 0.0581 seconds It will be recorded only at the time when there is a change.

Since it has low power consumption and high time resolution, it can be applied to automatic driving.

Main subject

It's a simple code, but I'll try something like an event camera. I will leave only the frames that have changed in the video.

In reality, the essential data structure of an event camera is to "acquire only the points where there is a difference". You can easily convert to that format using np.where Event cameras are generally a point cloud data format. Therefore, points are often aggregated and visualized in the time direction, so I will try to make only the visualized image from the image.

code

This time, the difference is taken in each channel one image before, and if it is above the threshold value, the pixel is left as an event.

import numpy as np
import cv2

def img2event_image(base_img, img, th = 10, plus = 128, minus = 255):
    img3 = base_img.astype(float) - img.astype(float)

    index1 = img3 > th
    img3[index1] = plus

    index2 = img3 < -1 * th
    img3[index2] = minus

    img3[~(index1) & ~(index2)] = 0 
    return img3.astype(np.uint8)

def video2event_video():
    stack = True
    path = "96.mp4"
    # output_name = "output.mp4"
    output_name = "output_stack.mp4"

    cap = cv2.VideoCapture(path)
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    if stack:
        w *= 2
    fps = cap.get(cv2.CAP_PROP_FPS)
    fps = 30
    out = cv2.VideoWriter(output_name, fourcc, fps, (w, h)) 

    success, image = cap.read()
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    base_image = image[:]
    while success:
        b_img = img2event_image(base_image.copy(), image.copy())
        if stack:
            b_img = cv2.hconcat([image, b_img])

        out.write(b_img)
        base_image = image[:]
        success, image = cap.read()
        # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    cap.release()
    cv2.destroyAllWindows()

reference

Event camera DAVIS346 https://nanoxeed.co.jp/product/eventcamera/

Event camera story https://qiita.com/minomonter/items/6d71029f6c860da60740

Recommended Posts

I tried image processing like an event camera
[Machine learning] I tried to do something like passing an image
I tried to get an image by scraping
I tried simple image processing with Google Colaboratory.
I tried to detect the iris from the camera image
I tried AutoGluon's Image Classification
I tried 100 language processing knock 2020
seam carving (image cropping) I tried
I tried knocking 100 image processes (Q1 ~ Q10)
I tried 100 language processing knock 2020: Chapter 3
I tried 100 language processing knock 2020: Chapter 1
I tried 100 language processing knock 2020: Chapter 2
I tried 100 language processing knock 2020: Chapter 4
I tried asynchronous processing using asyncio
I tried to make an image similarity function with Python + OpenCV
I tried sending an SMS with Twilio
I tried to get the batting results of Hachinai using image processing
Create an image processing viewer with PySimpleGUI
I tried sending an email with python.
I tried playing with the image with Pillow
I tried simple image recognition with Jupyter
I tried natural language processing with transformers.
#I tried something like Vlookup with Python # 2
I made an image classification model and tried to move it on mobile
I tried to extract a line art from an image with Deep Learning
I tried "smoothing" the image with Python + OpenCV
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried "differentiating" the image with Python + OpenCV
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried Python-like loop processing with BigQuery Scripting
I tried to correct the keystone of the image
I tried "binarizing" the image with Python + OpenCV
I tried to detect an object with M2Det!
I tried sending an email with SendGrid + Python
I tried using the image filter of OpenCV
[Image processing] Posterization
I tried PyQ
I tried AutoKeras
I tried papermill
Image processing 100 knocks ①
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I want to convert an image to WebP with lollipop
I read an introductory book on natural language processing
100 image processing knocks !! (021-030) I want to take a break ...
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried Smith standardizing an integer matrix with Numpy
I tried to implement an artificial perceptron with python
I tried to get an AMI using AWS Lambda
I tried to become an Ann Man using OpenCV
I tried to make an OCR application with PySimpleGUI
Python: I tried a liar and an honest tribe
I tried to find an alternating series with tensorflow
I tried to compress the image using machine learning