How to calculate the mean value of the 3rd dimension of YUV so as to filter the differences between consecutive frames ?

I was trying to resolve the issue of non-consecutive frames which extracted from a video. I want to go through each pixel and each frame.
Currently I have converted the RGB images to YUV and extracted each component. I want to calculate the mean value of the 3rd dimension of YUV so as to filter the difference between consecutive frames. . What should I do next ?
Thank you.
Below are the code that I have done so far: =================================================
caffe.reset_all();
caffe.set_mode_gpu();
caffe.set_device(0);
clear; close all;
close all;
clear;
%%Display RGB
Dir = 'C:\Users\Image\';
im_list = dir(fullfile(Dir, '*.bmp'));
% loop for all test images
for i = 1:length(im_list)
im_path = [Dir im_list(1).name];
x = imread(im_path);
R=x(:,:,1);
G=x(:,:,2);
B=x(:,:,3);
RGB = cat(3,R,G,B);
YUV = rgb2ycbcr(RGB);
%%Display each component
compY = YUV(:,:,1); %%extract Y component
compU = YUV(:,:,2); %%extract U component
compV = YUV(:,:,3); %%extract V component

1 Comment

I don't understand the question. The channels are the third dimension, so the mean in the third dimension is the mean of all channels (which you get from rgb2gray), not just the Y channel.

Sign in to comment.

Answers (2)

meanY = mean(compV(:));
or if you have the Image Processing Toolbox:
meanY = mean2(YUV(:,:,3));

1 Comment

Thanks for your kind help.
I have modified my questions. Could you please help to take a look at again ?
Thank you.

Sign in to comment.

It's still ambiguous what you mean by "third dimension". Thorsten gave you one way. The third channel/slice/plane of your 3D color space image is compV, and mean2() will take the mean of that color channel (the value channel).
meanV = mean2(compV); % A single scalar value.
Whereas
meanImage = mean(YUV, 2); % A 2-D image.
will give you an image where each pixel is the mean along the third dimension. So, the result is an image that is (compY+compU+compV)/3.
Which concept is what you're thinking of?

5 Comments

Hi ImageAnalyst,
Thanks a lot for your answer.
What I mean is to filter only the luminance channel.
These frames were extracted from a video. As the video is not playback smoothly, so I extracted the frames from the video, then try to find the differences between each frame.
In these frames, there are differences between the pixels that in the same location.
e.g. Frame 1
1 2 3
4 5 6
7 8 9
Frame 2
1 2 3
20 13 24 (In the same location, the pixel values here are too large)
7 8 9
What I want to do is to find the pixels that in the same location that have big differences, then use filter function to filter the gap so that the video can be played smoothly after converting images to a video.
Keep track of the current image's first channel which is the Y component which is the lightness. Also keep track of the prior, old Y channel. Then convert to double and subtract them and take the absolute value. If any pixel is more than some amount, replace with the average value or some weighted average of the old value and current value, or whatever you want.
diffImage = abs(double(oldY) - double(currentY));
bigChangePixels = diffImage > 10; % or whatever
averageImage = uint8(0.5 * (double(oldY) - double(currentY)));
% Save current y as old Y
oldY = currentY; % Save unaltered Y
% Get new current Y by changing big difference pixels.
currentY(bigChangePixels) = averageImage(bigChangePixels);
imshow(currentY);
drawnow;
That's untested, just off the top of my head, so try to work with that.
Thanks again for your kind help and good answers. :)
Hi ImageAnalyst,
I added below code to my previous code above. Could you please help to check whether I can do like this ?
[a,b,c] = size(x); %%a by b by c arrays
meanFrame = zeros(size(x));
sumFrame = 0;
%
for i = 1:a %for each pixel
for j = 1:b
for k =1:c %%for each frame
sumFrame = sumFrame+k;
end
meanFrame = sumFrame/k;
end
end
imshow(meanFrame);
You can d_do_ that - it will work - but won't do anything useful at all. Use the debugger to step through it and find out why.

Sign in to comment.

Tags

Asked:

awa
on 6 Jul 2016

Commented:

on 7 Jul 2016

Community Treasure Hunt

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

Start Hunting!