user1626589
user1626589

Reputation: 61

OpenCV: Filter multiple frames in real time

I'm trying to filter the same point through multiple frames. Basically, I want to take a single pixel (say at position (0,0)) and run a filter at that position across multiple frames.

I'm getting a frame (type Mat) from the webcam. I want to buffer about 30 frames from the camera, and make vectors that represent the same position for those 30 frames. For example, if the input is 640x480 @ 30fps. I want to have 640x480=307,200 vectors that are 30 points long. In MATLAB, this would basically be a matrix of vectors (3D matrix), where each vector is 30 elements long. I want this so that I can apply temporal filters for each pixel.

I think I need to make a 3D Mat (CvMatND) with 30 dimensions. Then I will put each new frame into the a new dimension until my matrix is 640x480x30. Then I can filter the vectors

(0, 0, :)

(0, 1, :)

(0, 2, :)

...

(640, 480, :)

Once I've applied the filter to each vector, I will have 30 frames of video to output.

My question is what is the best way to buffer 30 frames? Once I have the 30 frames, what is the best way to apply a filter (say a low pass filter) to each pixel?

Thanks for your help.

Upvotes: 1

Views: 1760

Answers (2)

user1626589
user1626589

Reputation: 61

This is what I came up with with Øystein W.'s help

Create a Mat for the new frame and a vector of mats for the buffer:

Mat frame;  // grab the newest frame
std::vector <cv::Mat> buffer;   // buffer for frames

Since I am getting frames from the webcam (the newest one is in 'frame'), I have to fill up the buffer before moving forward:

if (buffer.size() < 30)
{
    buffer.push_back(frame);
    continue;       // goes back to the beginning of the loop, program can't start until the buffer is full
}
else
{
    buffer.erase(buffer.begin());       // this part deletes the first element
    buffer.push_back(frame);            // this part adds the new frame to the end of the vector
}

This should keep the newest frame at the bottom and the oldest frame at the top.

Upvotes: 0

&#216;ystein W.
&#216;ystein W.

Reputation: 517

I'm using a

   std::vector <cv::Mat*> images 

as a buffer. It's easy to iterate though the vector and you can pop and push in back and front. I have no problems with the real-time processing.

Upvotes: -1

Related Questions