Color Extraction

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

   



									Public Shared Sub ApplyColorExtraction(ByRef bmp As Bitmap, threshold As Integer, extractionColor As System.Drawing.Color, otherPixelsColor As System.Drawing.Color)
	Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
	Dim ExtractionTotalRGB As Integer = extractionColor.R + extractionColor.G + extractionColor.B
	Dim TotalRGB As Integer
	Dim ptr As IntPtr = bmpData.Scan0
	Dim stopAddress As Integer = CInt(ptr) + bmpData.Stride * bmpData.Height

	While CInt(ptr) <> stopAddress
		TotalRGB = CInt(Marshal.ReadByte(ptr)) + CInt(Marshal.ReadByte(ptr + 1)) + CInt(Marshal.ReadByte(ptr + 2))

		If Math.Abs(TotalRGB - ExtractionTotalRGB) >= threshold Then
			Marshal.WriteByte(ptr, otherPixelsColor.B)
			Marshal.WriteByte(ptr + 1, otherPixelsColor.G)
			Marshal.WriteByte(ptr + 2, otherPixelsColor.R)
		End If

		ptr += 3
	End While

	bmp.UnlockBits(bmpData)
End Sub
								


Example

									DIm b As Bitmap = CType(Image.FromFile("rose.jpg"), Bitmap)
ApplyColorExtraction(b, 50, Color.Green, Color.White)