Color Extraction

This algorithm filters specified color and replaces all other colors with other specified color.

   



									public static void ApplyColorExtraction(ref Bitmap bmp, int threshold, System.Drawing.Color extractionColor, System.Drawing.Color otherPixelsColor)
{
	BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

	unsafe
	{
		int ExtractionTotalRGB = extractionColor.R + extractionColor.G + extractionColor.B;
		int TotalRGB;

		byte* ptr = (byte*)bmpData.Scan0.ToPointer();
		int stopAddress = (int)ptr + bmpData.Stride * bmpData.Height;

		while ((int)ptr != stopAddress)
		{
			TotalRGB = ptr[0] + ptr[1] + ptr[2];

			if (Math.Abs(TotalRGB - ExtractionTotalRGB) >= threshold)
			{
				ptr[0] = otherPixelsColor.B;
				ptr[1] = otherPixelsColor.G;
				ptr[2] = otherPixelsColor.R;
			}

			ptr += 3;
		}
	}

	bmp.UnlockBits(bmpData);
}
								


Example

									Bitmap b = (Bitmap)Image.FromFile("rose.jpg");
ApplyColorExtraction(ref b, 50, Color.Green, Color.White);