What is the best way for frame construction?

What is the best way to convert events(x,y,polarity,time stamp) that are obtained from event camera via ROS to Frames?

Answers (1)

Converting events from an event camera (also known as a dynamic vision sensor, DVS) to traditional frames is a process called event-based frame reconstruction. Event cameras capture changes in pixel intensity as a stream of events, typically consisting of (x, y, polarity, timestamp) data, where (x, y) represents the location of the event, polarity indicates whether the event represents an increase or decrease in intensity, and timestamp records the time when the event occurred. To convert these events into frames, you can use various methods and algorithms.
import numpy as np
import cv2
import rospy
from dvs_msgs.msg import EventArray
# Initialize the frame and time variables
frame_width, frame_height = 640, 480 # Set your desired frame resolution
frames = np.zeros((frame_height, frame_width), dtype=np.uint8)
current_time = 0
# Define a time window for accumulating events (in microseconds)
time_window = 10000 # 10 ms
# Callback function for processing incoming events
def event_callback(event_array):
global frames, current_time
for event in event_array.events:
x, y, polarity, timestamp = event.x, event.y, event.polarity, event.ts.to_nsec()
# Check if the event is within the time window
if timestamp - current_time > time_window:
# Visualize or process the accumulated frame(s) here
cv2.imshow("Reconstructed Frame", frames)
cv2.waitKey(1)
# Reset the frame and current time
frames = np.zeros((frame_height, frame_width), dtype=np.uint8)
current_time = timestamp
# Update the pixel based on event polarity
frames[y, x] = 255 if polarity else 0
if __name__ == "__main__":
rospy.init_node("event_to_frame_converter")
rospy.Subscriber("/dvs/events", EventArray, event_callback)
rospy.spin()
This code listens to ROS messages containing events and accumulates them into frames within a specified time window. When the time window is exceeded, it displays the accumulated frame(s) and resets for the next accumulation. You'll need to adapt it to your specific event camera and ROS setup

7 Comments

M
M on 4 Oct 2023
Edited: M on 4 Oct 2023
@recent works Thanks but do you have MATLAB code because I imorted the events in MATLAB? Also is there a way other than accumulating the events and can be used for real time applications?
To convert these events into frames, you can use various methods and algorithms.
Do you know if there is a Matlab representation for these methods? and what do you think the best way can be used for real-time?
% Initialize variables
frame_width = 640; % Set your desired frame resolution
frame_height = 480;
frames = zeros(frame_height, frame_width, 'uint8');
% Set a time window for visualization (in microseconds)
time_window = 10000; % 10 ms
% Create a figure for real-time display
figure;
h = imshow(frames);
% Loop through your event data (replace with your actual event data)
for i = 1:numel(events)
event = events(i);
x = event.x;
y = event.y;
polarity = event.polarity;
timestamp = event.timestamp; % Timestamp in microseconds
% Check if the event is within the time window
if timestamp - current_time > time_window
% Display the accumulated frame
set(h, 'CData', frames);
drawnow;
% Reset the frame and current time
frames = zeros(frame_height, frame_width, 'uint8');
current_time = timestamp;
end
% Update the pixel based on event polarity
if polarity
frames(y, x) = 255;
end
end
@recent works Thanks, do you have an idea how to reduce the noise because my camera is noisy (DVD mini plorer) and I couldn't find fast way to reduce the noise, especially for online processing?
M
M on 4 Oct 2023
Edited: M on 4 Oct 2023
@recent works Do you know how to store the images as matrices not as image in your code?
% Initialize variables
frame_width = 640; % Set your desired frame resolution
frame_height = 480;
frames = zeros(frame_height, frame_width, 'uint8');
% Set a time window for visualization (in microseconds)
time_window = 10000; % 10 ms
% Create a cell array to store frames as matrices
frame_cell = cell(1);
% Loop through your event data (replace with your actual event data)
for i = 1:numel(events)
event = events(i);
x = event.x;
y = event.y;
polarity = event.polarity;
timestamp = event.timestamp; % Timestamp in microseconds
% Check if the event is within the time window
if timestamp - current_time > time_window
% Store the accumulated frame as a matrix
frame_cell{end+1} = frames;
% Reset the frame and current time
frames = zeros(frame_height, frame_width, 'uint8');
current_time = timestamp;
end
% Update the pixel based on event polarity
if polarity
frames(y, x) = 255;
end
end
% Now, frame_cell contains frames as matrices
M
M on 4 Oct 2023
Edited: M on 4 Oct 2023
@recent works what is current_time ?
I have x y polarity timestamp as matrices
M
M on 4 Oct 2023
Edited: M on 4 Oct 2023
@recent works I didnt get these lines
if timestamp - current_time > time_window
current_time = timestamp;
% Update the pixel based on event polarity
if polarity
frames(y, x) = 255;
end
please elaborate

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

M
M
on 4 Oct 2023

Edited:

M
M
on 4 Oct 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!