First time here? Check out the FAQ!

Ask Your Question
3

Extracting a vector of pixel values across multiple frames

asked Jul 11 '12

rich gravatar image

updated Aug 2 '12

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?

Preview: (hide)

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 (Aug 1 '12)edit

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

2 answers

Sort by » oldest newest most voted
4

answered Jul 13 '12

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.

Preview: (hide)
3

answered Jul 12 '12

Michael Burdinov gravatar image

updated Jul 14 '12

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()).

Preview: (hide)

Question Tools

Stats

Asked: Jul 11 '12

Seen: 2,523 times

Last updated: Jul 14 '12