I created a filter just for that, use it if you like it.
using AForge.Imaging;
using AForge.Imaging.Filters;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
namespace ISpyFilterTest.service.camera.filter {
public class ColorMaskFilter : BaseInPlacePartialFilter {
#region Init
private Dictionary<PixelFormat, PixelFormat> formatTranslations;
private Bitmap mask;
public override Dictionary<PixelFormat, PixelFormat> FormatTranslations { get { return formatTranslations; } }
private int maxDiferenceRed;
private int maxDiferenceGreen;
private int maxDiferenceBlue;
public ColorMaskFilter(Bitmap mask) : this(mask, 50) { }
public ColorMaskFilter(Bitmap mask, int maxDiferenceColor) : this(mask, maxDiferenceColor, maxDiferenceColor, maxDiferenceColor) { }
public ColorMaskFilter(Bitmap mask, int maxDiferenceRed, int maxDiferenceGreen, int maxDiferenceBlue) {
formatTranslations = new Dictionary<PixelFormat, PixelFormat>();
formatTranslations[PixelFormat.Format24bppRgb] = PixelFormat.Format24bppRgb;
formatTranslations[PixelFormat.Format32bppRgb] = PixelFormat.Format32bppRgb;
formatTranslations[PixelFormat.Format32bppArgb] = PixelFormat.Format32bppArgb;
this.mask = mask;
this.maxDiferenceRed = maxDiferenceRed;
this.maxDiferenceGreen = maxDiferenceGreen;
this.maxDiferenceBlue = maxDiferenceBlue;
}
#endregion
protected unsafe override void ProcessFilter(UnmanagedImage uImage, Rectangle rect) {
int pixelSize = System.Drawing.Image.GetPixelFormatSize(uImage.PixelFormat) / 8;
var src = (byte*)uImage.ImageData.ToPointer();
mask.Unmanaged(mUImage => {
var msk = (byte*)mUImage.ImageData.ToPointer();
var size = uImage.Width * uImage.Height;
while (size-- > 0) {
var rSrcp = src + 2; var rMskp = msk + 2;
var gSrcp = src + 1; var gMskp = msk + 1;
var bSrcp = src + 0; var bMskp = msk + 0;
if (
Math.Abs(*rSrcp - *rMskp) <= maxDiferenceRed &&
Math.Abs(*gSrcp - *gMskp) <= maxDiferenceGreen &&
Math.Abs(*bSrcp - *bMskp) <= maxDiferenceBlue
) {
*rSrcp = 0;
*gSrcp = 0;
*bSrcp = 0;
}
src += pixelSize;
msk += pixelSize;
}
}, ImageLockMode.ReadOnly);
}
}
}
using AForge.Imaging;
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace ISpyFilterTest.service {
public static class BitmapExt {
public static void Unmanaged(this Bitmap image, Action<UnmanagedImage> action, ImageLockMode lockMode = ImageLockMode.ReadWrite) {
var imageData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), lockMode, image.PixelFormat);
try {
var uImage = new UnmanagedImage(imageData);
action(uImage);
} catch (Exception) {
throw;
} finally {
image.UnlockBits(imageData);
}
}
}
}