Ask Your Question
3

Extracting a vector of pixel values across multiple frames

asked 2012-07-11 15:34:24 -0600

rich gravatar image

updated 2012-08-02 10:10:47 -0600

Hi -- I am very new to OpenCV, so please forgive my naivety. I'd like to examine how pixel values, at particular pixel locations, change over multiple frames. As a result, I am interested in reading in a vector of pixels across multiple images, rather than all pixels of an image. I can do this by reading in entire frames and storing the pixel values of interest to a separate vector, but is there is a more efficient and elegant approach using OpenCV?

Eventually I will be performing statistical analysis (mean, variance, etc.) of pixel intensity values in time. It appears that OpenCV has some nice functions for computing statistics of pixels in space (i.e. within an image), such as cvMean_StdDev, but it is not clear to me if OpenCV supports similar capability across multiple frames. In other words, for N frames of video, with each frame of dimensions W x H, does OpenCV have a quick and easy way to return a single W x H image representing the mean, std, or variance of each pixel over time?

edit retag flag offensive close merge delete

Comments

Thank you Michael and Kirill for your answers. Both approaches work well. One issue that wasn't mentioned was the possibility of saturation. I am starting with an array of matrices ( Mat *frameArray ) of type CV_U16C1, but it is necessary to perform the accumulation and division in floating point math to avoid overflow. I implemented the following:

Mat sumImage, tempDouble; frameArray[0].convertTo(sumImage,CV_64FC1,0); // initialize accumlation to zeros for(int i=0; i < numFrames; i++) { frameArray[i].convertTo(tempDouble,CV_64FC1); sumImage += tempDouble; }

This works, but requires converting each frame to double precision, which seems slow and inefficient. Is there a way to recast the entire array pointer or some other more efficient approach?

rich gravatar imagerich ( 2012-08-01 14:31:12 -0600 )edit

Question above is discussed here: http://answers.opencv.org/question/1082

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-08-03 17:04:46 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
4

answered 2012-07-13 01:41:32 -0600

Kirill Kornyakov gravatar image

cv::accumulate function might help you. Also check accumulateSquare, accumulateProduct and accumulateWeighted. With accumulated image you can easily calculate statistics as Michael Burdinov says.

edit flag offensive delete link more
3

answered 2012-07-12 04:55:09 -0600

Michael Burdinov gravatar image

updated 2012-07-14 16:41:14 -0600

Your program will be much faster if instead of extracting pixels from different frames (which may be very inefficient in terms of memory access), it will use functions that work with pairs of images. Those are enough for calculation of mean or standard deviation. Here an example:

Allocate image of integers and set their values to zero. Use array of floats or doubles if large values are expected: Mat meanImage(Size(W,H),CV_32S,Scalar(0));

Calculate sum of all frames: for(i=0; i < N; i++) meanImage += frame[i];

Divide by N to get mean: meanImage /= N;

StdDev can be calculated in the same way (you may use functions like pow() or multiply()).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-07-11 15:34:24 -0600

Seen: 2,418 times

Last updated: Jul 14 '12