How can I get the median value of the temporal filtering of 8 RGB frames using medfilt2()?

2 views (last 30 days)
I would like to perform median temporal filtering of a video. So far my understanding of spatial median filtering and temporal median filtering is that spatial is performed on a single frame whereas temporal is performed over K frames. The video from which the frames are being read is RGB, hence I split the frames into there separate component channels before attempting to calulcate the median filtering value of 8 frames. Can anyone explain how to go about this?
videoFile = 'walk1.mp4';
threshold = 35;
hs_row = 3;
hs_col = 3;
% Reading and playing data from a stored video file
videoReader = VideoReader(videoFile);
% displays the input video
videoPlayer1 = vision.VideoPlayer('Name','Input_Image','Position',...
[100,200,300,300]);
% displays the output video
videoPlayer2 = vision.VideoPlayer('Name','Output_Image','Position',...
[400,200,300,300]);
% =========================================================
% acquire k frames
% =========================================================
firstFrame = readFrame(videoReader);
secondFrame = readFrame(videoReader);
thirdFrame = readFrame(videoReader);
fourthFrame = readFrame(videoReader);
fifthFrame = readFrame(videoReader);
sixthFrame = readFrame(videoReader);
sevethFrame = readFrame(videoReader);
eigthFrame = readFrame(videoReader);
% get the frame size so that the BGI is the same size
[My,Nx,~] = size(firstFrame);
while hasFrame(videoReader)
% =========================================================
% acquire k + 1 frame
% =========================================================
newFrame = readFrame(videoReader);
newFrame = double(newFrame);
% intensity of each channel of K + 1 frame
newFrame_r = newFrame(:,:,1);
newFrame_g = newFrame(:,:,2);
newFrame_b = newFrame(:,:,3);
% =========================================================
% compute the difference between this frame and the current
% background at each pixel
% =========================================================
% intialised variables for k frames (I know this may be possible
% to make this method have a variable for k in a for loop
% but I haven't yet understood cell arrays enough to attmept it)
firstFrame = double(firstFrame);
secondFrame = double(secondFrame);
thirdFrame = double(thirdFrame);
fourthFrame = double(fourthFrame);
fifthFrame = double(fifthFrame);
sixthFrame = double(sixthFrame);
seventhFrame = double(sevethFrame);
eigthFrame = double(eigthFrame);
% HOW CAN I APPLY THE MEDIAN FILTERING PROCESS OVER 8 FRAMES?
% this code is incorrect as medfilt2() only performs 2D filtering
medFramesSum_r = medfilt2([firstFrame(:,:,1), secondFrame(:,:,1), thirdFrame(:,:,1),...
fourthFrame(:,:,1), fifthFrame(:,:,1), sixthFrame(:,:,1),...
seventhFrame(:,:,1), eigthFrame(:,:,1)], [hs_row, hs_col]);
medFramesSum_g = medfilt2([firstFrame(:,:,2), secondFrame(:,:,2), thirdFrame(:,:,2),...
fourthFrame(:,:,2), fifthFrame(:,:,2), sixthFrame(:,:,2),...
seventhFrame(:,:,2), eigthFrame(:,:,2)], [hs_row, hs_col]);
medFramesSum_b = medfilt2([firstFrame(:,:,3), secondFrame(:,:,3), thirdFrame(:,:,3),...
fourthFrame(:,:,3), fifthFrame(:,:,3), sixthFrame(:,:,3),...
seventhFrame(:,:,3), eigthFrame(:,:,3)], [hs_row, hs_col]);
% compute difference
C1 = abs(newFrame_r-medFramesSum_r);
C2 = abs(newFrame_g-medFramesSum_g);
C3 = abs(newFrame_b-medFramesSum_b);
% Calculate the maximum difference over the 3 channels:
Cabs12 = max(C1,C2);
Cabs = max(Cabs12,C3);
Cmax = uint8(Cabs);
% covert the background indicator array to white where the
% max channel difference is above the threshold value for motion
BGI = zeros(My,Nx);
BGI(Cmax>threshold) = 255;
% convert to unit8 for displaying
BGI = uint8(BGI);
newFrame = uint8(newFrame);
% display
videoPlayer1(newFrame);
videoPlayer2(BGI);
pause(1/videoReader.FrameRate);
% update the frame for the next background median
firstFrame = secondFrame;
secondFrame = thirdFrame;
thirdFrame = fourthFrame;
fourthFrame = fifthFrame;
fifthFrame = sixthFrame;
sixthFrame = seventhFrame;
seventhFrame = eigthFrame;
eigthFrame = newFrame;
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!