- Home /
How to get the median value of a set of data?
I am currently making a program which needs to evaluate colours received from a webcam input, to evaluate if, say, the currently introduced colour is within the RGBA limits of a previously defined green.
The function converts the image received from a webcam into a ImageWidth*ImageHeight length struct, through the GetPixels32() function. From here I evaluate based on 25 pixel values in the area that I wish to detect the colour introduced.
To avoid problems with Salt'n'Pepper noise, I would wish to be able to do this using a median value function, instead of merely going by mean values, but have found no reference as to how to do this.
The way I find the mean value, here shown using pseudo-code instead of actual code (to avoid a lot of confusion my values might cause), looks like this;
webcam.GetPixels32[ImageValues];
For (int i; i < 25; i++){
RedValue += ImageValues[i].r;
GreenValue += ImageValues[i].g;
BlueValue += ImageValues[i].b;
AlphaValue += ImageValues[i].a;
}
RedValue = RedValue / 25;
GreenValue = GreenValue / 25;
BlueValue = BlueValue / 25;
AlphaValue = AlphaValue / 25;
Any reference to how a median value might be found would be much appreciated.
Is there any reason why you can't just sort the ImageValues list and pick the middle element?
So sorry about the VERY late response; No, there isn't any reason, my $$anonymous$$d was just at a melt-down point I guess, and didn't see the option.