why is median of a sequence of images slower than mean
why is median of a sequence of images slower than mean
I’m wondering, why the calculation of a median of 3 images takes so much more time than mean. Now I understand a little bit more about it.
How is the intensity calculated? Just by (R+G+B) /3?
What about the ambiguities? Only a red, green or blue pixel could have the same intensity…
How is the intensity calculated? Just by (R+G+B) /3?
What about the ambiguities? Only a red, green or blue pixel could have the same intensity…

 Posts: 11092
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: median of a sequence of images operating pixelbypixel
Median takes longer than mean because median populates an array with pixel values, sorts it, and chooses the middle value. Mean simply adds, and divides by the number of images.
Intensity is usually calculated according to the current "intensity" setting. See http://www.imagemagick.org/script/comma ... #intensity
Intensity is usually calculated according to the current "intensity" setting. See http://www.imagemagick.org/script/comma ... #intensity
snibgo's IM pages: im.snibgo.com
Re: median of a sequence of images operating pixelbypixel
Thanks for response…
‘Rec709Luminance’ is used?
I know how to calculate a median. A median of 3 is not a big thing but probably always a sort routine is using and there is no optimization for small medians.
If I just call
Code: Select all
convert i1.jpg i2.jpg i3.jpg evaluatesequence median result.jpg

 Posts: 11092
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: median of a sequence of images operating pixelbypixel
Ah, sorry, I was wrong. For sorting pixels, median doesn't use "intensity" but a fixed formula:
EDIT: On sorting, yes, this always happens, even when there is only one image.
Code: Select all
return(0.212656*pixel>red+0.715158*pixel>green+0.072186*pixel>blue);
snibgo's IM pages: im.snibgo.com
 anthony
 Posts: 8875
 Joined: 20040531T19:27:0307:00
 Authentication code: 8675308
 Location: Brisbane, Australia
Re: median of a sequence of images operating pixelbypixel
It probably should be using intensity.
also rather than necroposting you should have started a new post.
I am splitting the post into a separate topic.
also rather than necroposting you should have started a new post.
I am splitting the post into a separate topic.
Anthony Thyssen  Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: why is median of a sequence of images slower than mean
Thanks…
It can’t be only the sorting on the median processing…
As I said before, I’m doing a lot of image processing in ‚MATLAB‘ for time lapse. On most parts a got similar runtimes between ‚MATLAB‘ and ‘ImageMagick’. But on the median processing there is a big difference.
For a median of three images (4000*3000) I need with 'MATLAB' 3.5s and with ‘ImageMagick’ 13s.
Just for Info, how I do the Median processing in MATLAB
Code: Select all
image_1=imread('IMG_0001.tif');
image_2=imread('IMG_0002.tif');
image_3=imread('IMG_0003.tif');
image_1=double(image_1);
image_2=double(image_2);
image_3=double(image_3);
G(:,:,1) = (image_1(:,:,1)*0.212656 + image_1(:,:,2)*0.715158 + image_1(:,:,3)*0.072186);
G(:,:,2) = (image_2(:,:,1)*0.212656 + image_2(:,:,2)*0.715158 + image_2(:,:,3)*0.072186);
G(:,:,3) = (image_3(:,:,1)*0.212656 + image_3(:,:,2)*0.715158 + image_3(:,:,3)*0.072186);
%G=int32(G*1000.0);
%G=uint8(G);
med=median(G,3);
%ind1=med==G(:,:,1);
ind2=med==G(:,:,2);
ind3=med==G(:,:,3);
ind2=repmat(ind2,1,1,3);
ind3=repmat(ind3,1,1,3);
Result=image_1;
Result=not(ind3).*Result + ind3.*image_3;
Result=not(ind2).*Result + ind2.*image_2;
Result=uint8(Result);
imwrite(Result,'Result_M.tif','tif')

 Posts: 11092
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: why is median of a sequence of images slower than mean
Yes, IM takes about 80 times as long to find the median of 3 4000x3000 images, compared to finding the mean.
I don't know why the difference is so great. True, the mean calculation is trivial: at each pixel we add the values of the channels of the images, and at the end we divide by the count of the images. For median we copy the values of the channels of the images to an array, then sort the array which involves (at each comparison) calculating the grayscale value of two pixels, then copy the midarray pixel to the result.
How many comparison are needed when sorting 3 elements? Perhaps 6. So we do 12 floatingpoint calculations of the form gray=a*red + b*green + b*blue for every pixel. Your Matlab code seems to do that only once per pixel.
I don't know why the difference is so great. True, the mean calculation is trivial: at each pixel we add the values of the channels of the images, and at the end we divide by the count of the images. For median we copy the values of the channels of the images to an array, then sort the array which involves (at each comparison) calculating the grayscale value of two pixels, then copy the midarray pixel to the result.
How many comparison are needed when sorting 3 elements? Perhaps 6. So we do 12 floatingpoint calculations of the form gray=a*red + b*green + b*blue for every pixel. Your Matlab code seems to do that only once per pixel.
snibgo's IM pages: im.snibgo.com
 fmw42
 Posts: 24332
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: why is median of a sequence of images slower than mean
There are more optimal ways to compute the median than what IM does. Search Google for "fast median filter". See for example https://nomis80.org/ctmf.pdf
Re: why is median of a sequence of images slower than mean
I don’t think the median code is the problem. In the MATLAB code, just 0,7s(20%) of the time is for the median.
The MATLAB Code gives me same result as the convert.
If I understand the code correctly:
1) From the 3 RGB pictures I calculate 3 gray pictures.
2) The median goes only over the 3 gray pictures.
3) If the median from pixel 1 comes from gray picture 1, than the RGB values from picture 1 copied to the result
Step 3 takes more time than median calculation. I think this part needs the most time.

 Posts: 11092
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: why is median of a sequence of images slower than mean
Yes. Your Matlab implementation converts image to grayscale, then finds the median. IM doesn't make the images gray, so needs to do that within the comparison function that is called by the sort function. (The code is in statistic.c.)c_joerg wrote:In the MATLAB code, just 0,7s(20%) of the time is for the median.
snibgo's IM pages: im.snibgo.com
Re: why is median of a sequence of images slower than mean
Not???
http://www.imagemagick.org/Usage/layers ... esequence
Intensity => 0.212656*pixel>red+0.715158*pixel>green+0.072186*pixel>blueThe "evaluatesequence Median" will look for the pixel which has an intensity of the middle pixel from all the images that are given.
That is for each position it collects and sorts the pixel intensity from each of the images. Then it will pick the pixel that falls in the middle of the sequence.
That is what I’m doing in the MATLAB code and the Result is similar to convert median….The key point is that each pixel will come completely from one image, and sorted by intensity. You will never get a mix of values, producing a color mixed from different images. The exact color of each pixel will come completely from one image.
 anthony
 Posts: 8875
 Joined: 20040531T19:27:0307:00
 Authentication code: 8675308
 Location: Brisbane, Australia
Re: why is median of a sequence of images slower than mean
The command line discards the source images, basically as it only has a single image list, so results of a image process replaces the source image list.
However the imagemagick library does not have that restriction. The results are returned as a completely separate image list, preserving the source images, for the calling program to do with as they like.
What this means is that IM will not simply convert the source images to intensity greyscale but process them as given.
Also the fuction does not return a intensity value but the full color pixel with the median intensity!
NOTE: Imagemagick design criterion was never specifically 'speed' but 'correctness' first. If a faster way can be used then programmers will implement it when they have time and inclination. But correctness was always first.
It is opensource and if you feel inclined you are welcome to try and add or fix things.
I did for a long time, and in the process influenced a lot of the direction IM has taken. But I have moved on now.
However the imagemagick library does not have that restriction. The results are returned as a completely separate image list, preserving the source images, for the calling program to do with as they like.
What this means is that IM will not simply convert the source images to intensity greyscale but process them as given.
Also the fuction does not return a intensity value but the full color pixel with the median intensity!
NOTE: Imagemagick design criterion was never specifically 'speed' but 'correctness' first. If a faster way can be used then programmers will implement it when they have time and inclination. But correctness was always first.
It is opensource and if you feel inclined you are welcome to try and add or fix things.
I did for a long time, and in the process influenced a lot of the direction IM has taken. But I have moved on now.
Anthony Thyssen  Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
 anthony
 Posts: 8875
 Joined: 20040531T19:27:0307:00
 Authentication code: 8675308
 Location: Brisbane, Australia
Re: why is median of a sequence of images slower than mean
I have updated IM Examples, adding a actual example for median
Its not a great example, but it does the job.
http://www.imagemagick.org/Usage/layers ... seq_median
Its not a great example, but it does the job.
http://www.imagemagick.org/Usage/layers ... seq_median
Anthony Thyssen  Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: why is median of a sequence of images slower than mean
Yes of course. The intensity value is only for sort of median. On the end I want to have a full color picture. That’s exact what my MATLAB code is doing. And again my result is similar to convert.
Yes, I understand this…
My intention is to remove fast objects from timelapse videos. For one video I have to call median at least 1000 times. So it’s a big difference if a call is 3,5s or 13s
Right now I can live with my MATLAB code. But I think a solution with ‘ImageMagick’ could be more common.