Extracting frames using mmreader and calculate and substract mean from data

2 views (last 30 days)
hello all, i am using mmreader to input video,
i want to extract all Y frames of video object and calculate their mean and finally subtract their mean from the data.
so that y frames will be mean zero.
plz help.
thanks....

Accepted Answer

Sean de Wolski
Sean de Wolski on 20 Dec 2011
xyloObj = mmreader('xylophone.mpg');
%Make a 4d rgb volume
V = zeros(xyloObj.Height,xyloObj.width,3,xyloObj.NumberOfFrames,'uint8');
for ii = 1:size(V,4)
V(:,:,:,ii) = read(xyloObj,ii);
end
%subtract mean (V zero mean)
V = double(V); %must be double (or single)
Vzmean = bsxfun(@minus,V,mean(mean(V,1),2));
  2 Comments
Walter Roberson
Walter Roberson on 20 Dec 2011
converting to int8 is going to clamp the values below -128 or above 127.
The part before subtracting the mean can be replaced by
xyloObj = mmreader('xylophone.mpg');
V = read(xyloObj);
and V will already be filled in with all the frames and of the correct size.
Sean's code subtracts the mean of each frame from that frame. The code I showed subtracts the mean of each pixel from the corresponding pixels over all frames. The posting is ambiguous as to which mean is intended.
Sean de Wolski
Sean de Wolski on 20 Dec 2011
true it is, and good to know about read(). And yes, I guess there will be clipping. I'd incorrectly assumed the mean would be around 127.5

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 20 Dec 2011
By default, mmreader's read() method reads all frames and stores them in to a single 4D matrix. You can calculate the mean for each pixel across all the frames by using mean(TheArray,4) . You can then repmat() that with the factor [1,1,1,size(TheArray,4)] and subtract that from TheArray
Warning: when you do that, you will end up with negative entries if your data is double precision, but if your datatype is uint8 (as would be common) the negative entries would be clamped to 0. double(TheArray) - themean in order to get an unclamped result. But you will still need to figure out what you want to do about the negative entries and about the fact that your unclamped values will range from -255 to +255 (if the data was uint8)

Community Treasure Hunt

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

Start Hunting!