I use Canon EOS SDK and want to realize live greenscreening with the camera stream.
I have a simple greenscreen algorithm with pixel operations optimizations.
public Bitmap greenScreen(Bitmap input, int tolerance)
{
FastPixel fpinput = new FastPixel(input);
fpinput.Lock();
Bitmap output = new Bitmap(input.Width, input.Height);
FastPixel fpoutput = new FastPixel(output);
fpoutput.Lock();
for (int y = 0; y < output.Height; y++)
{
for (int x = 0; x < output.Width; x++)
{
Color camColor = fpinput.GetPixel(x, y);
// Every component (red, green, and blue) can have a value from 0 to 255, so determine the extremes
byte max = Math.Max(Math.Max(camColor.R, camColor.G), camColor.B);
byte min = Math.Min(Math.Min(camColor.R, camColor.G), camColor.B);
bool replace =
camColor.G != min // green is not the smallest value
&& (camColor.G == max // green is the bsiggest value
|| max - camColor.G < 8) // or at least almost the biggest value
&& (max - min) > tolerance; // minimum difference between smallest/biggest value (avoid grays)
if (!replace)
fpoutput.SetPixel(x, y, camColor);
}
}
fpinput.Unlock(true);
fpoutput.Unlock(true);
return fpoutput.Bitmap;
}
for 800x600 pictures this takes around 300ms on my i5. Of course this is not fast enough for a smooth livestream.
What optimization options do I have? Is there an opportunity to use gpu functions? What would be an opportunity? I can't use DirectShow with the Canon Camera (no driver does exist)