0

So my computer is not too strong.. to say the least.. Yet I want to create a median of all pixels in an entire specific movie. I was able to do it for a sequence of frames in memory.. but I am not sure on how to do it when reading more frames each time... how do I give median weight? (like I'll read 100 frames each time but the median has to update according to the current median * 100 * times I read + 100 * current image..) I have this code:

mov = VideoReader('MVI_3478.MOV');
seq = read(mov, [1 frames]);
% create background
channels = size(seq, 3);
height = size(seq,1);
width = size(seq,2);
BG = zeros(height, width, channels, 'uint8');
for c = 1:channels
    for y = 1:height
        for x = 1:width
            BG(y,x,c) = median(seq(y,x,c,:));
        end
    end
end

and my question is, given that I will add another loop above everything, how to give median weight?

Thanks!

Alon
  • 1,776
  • 13
  • 31

2 Answers2

0

There is no possibility to calculate the median this way. The required Information is lost.

Example:

median([1,2,3,4,5,6,7]) is 4
median([1,2,3,3,5,6,7]) is 3
median([1,2,3])=2
median([4,5,6,7])=5
median([3,5,6,7])=5

Thus, for both subsequence you get the partial results 2 and 5, while the median is 3 in one case and 4 in the other case.

The only possibility I see is some binary search approach:

smaller=0
larger=0
equal=0
el=numel(s)
while(smaller>=el/2||larger>el/2||equal==0)
    guess=..
    smaller=0
    larger=0
    equal=0
    for c = 1:channels
        for y = 1:height
            for x = 1:width
                s=seq(y,x,c,:)
                smaller=smaller+numel(s(s<guess);
                larger=larger+numel(s(s>guess);
                equal=equal+numel(s(s=guess);
            end
        end
    end
end

This is only a sketch, the code has to be completed. Guess has to be filled with some binary search strategy.

Daniel
  • 36,610
  • 3
  • 36
  • 69
0

In case of a large number of frames, calculating the median in a progressive manner can be problem since the median is a global order statistic and does not have a structure. The classical method is to use the fact that we are working with grayscale 8 bit values (256). Thus for any pixel p(x,y,n) one needs to maintain a histogram with 256 bins with each bin counting n values( as there are n frames).

Thus at each update we will have:

value = p(x,y,i); %for the ith frame
H(x,y,value) = H(x,y,value) + 1; %updating your histogram, 

and then sort the histogram by their frequencies and pick the middle value: https://math.stackexchange.com/questions/202302/how-to-calculate-median-and-standard-deviation-from-histogram

The size of this counter can be decided based on the number of frames you have in the video N = log2(n) bit. The median search now is simplified since its constant time search within a histogram. This also helps when concatenating many histograms since the search remains a constant time search independent. Thus finally the total size of your histograms would be XYN bits, where X and Y are the dimensions of your image.

Community
  • 1
  • 1
beedot
  • 652
  • 5
  • 12