I wrote this code to remove annoying patterns in a video due to camera malfunction. The problem is that to encode a 2 Minute video this algorithm needs more than 2 hours. I want to significantly reduce the time needed.
The algorithm iterates over each image, looks at each 4 pixels from there, creates the average and if the average is below a threshold, sets the current pixel to white. I could use step = 2 and set the 2x2 matrix to white, yet this deteriorates images quality and only increases speed by half.
I already added the lockbitmap
, lockbits
and improved the auxiliary functions.
Before and after the using (d2 = reader.ReadVideoFrame())
-snippet I have an aforge videoreader and writer based on ffmpeg.
using (d2 = reader.ReadVideoFrame())
{
LockBitmap lockBitmap = new LockBitmap(d2);
lockBitmap.LockBits();
Color v = Color.FromArgb(240, 237, 241);
for (int x = 0; x < lockBitmap.Width-1; x = x + 1)
{
for (int y = 0; y < lockBitmap.Height-1; y = y + 1)
{
Color dus = durchschnitt(lockBitmap.GetPixel(x, y),
lockBitmap.GetPixel(x + 1, y),
lockBitmap.GetPixel(x, y + 1),
lockBitmap.GetPixel(x + 1, y + 1));
if (abstand(dus, v) < 50)
{
lockBitmap.SetPixel(x, y, Color.White);
}
}
}
lockBitmap.UnlockBits();
}
Auxiliary functions:
private Color durchschnitt(Color c1, Color c2, Color c3, Color c4)
{
return Color.FromArgb((int)((c1.R + c2.R + c3.R + c4.R) / 4),
(int)((c1.G + c2.G + c3.G + c4.G) / 4),
(int)((c1.B + c2.B + c3.B + c4.B) / 4));
}
and
private double abstand(Color c1, Color c2)
{
return Math.Sqrt(Math.Pow(c2.R - c1.R, 2) + Math.Pow(c2.G - c1.G, 2) + Math.Pow(c2.B - c1.B, 2));
}
LockBitmap
is from here.