Click here to Skip to main content
15,885,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends
I am working on a semester project that includes Aforge.net and C#.
I want to track color, color extraction is working fine.
but I have problem with background extraction. I have Background image, Foreground image and and want to get the result image. following is the work I have done.
C#
//BackgroundFrame , resultFrame and sourceImage are Bitmap images
Difference differenceFilter = new Difference(); //AForge.Imaging.Filters.Difference
// difference filter
differenceFilter.OverlayImage = sourceImage;
// apply the filter
resultFrame = differenceFilter.Apply(BackgroundFrame);

here is link to snapshot which explain my needs well.
here Desired frame is the result I am expecting as a result frame. (need to be implemented)
is anyone have any idea about it plz share. Thanks

[edit] more detail about question
according to needs the algorithm can be like this

if (sourceimage.pixel== overlayimage.pixel)
then result.pixel =black
else
result.pixel=overlay.pixel
return result
****

is there any method that work like this? (because in this case it do not return the difference of overlay and source image. and will fullfil my needs)
Posted
Updated 27-Apr-13 11:31am
v3

Why not using either EuclideanColorFiltering (that was my first idea) or, say, YCbCrFiltering instead?
Please see:
http://www.aforgenet.com/framework/docs/html/67fa83b5-dede-8d3a-8d3b-b7a6b9859538.htm[^],
http://www.aforgenet.com/framework/docs/html/5566544c-57c0-d928-da95-42e11c689cc9.htm[^].

Apparently, for your palm image, you will need to estimate and set wider ranges (EuclideanColorFiltering.Radius or YCbCrFiltering.Cb and YCbCrFiltering.Cr) then in example shown in the help pages referenced above.

Another idea is: maybe you could get better isolation if you work only with red or blue channel, or combination of the two (excluding green). Look at the colors.

By the way, I have no idea how did you get your "result frame". Apparently, this is a combination of background and foreground frames and way more difficult then what you called "desired frame" — it looks quite simple to me.

—SA
 
Share this answer
 
Comments
Member 8973214 26-Apr-13 17:14pm    
Yeah SA , I have used EuclideanColorFiltering and YCbCrFiltering. it track a range of colors, as in example , but if the same color exist in the background then it fails by providing two or more color objects.
as in my attached image, i get the result frame by the provided code. but i want to get clear colors of hand (in this case) ,
("desired frame" is made in photoshop, I need the same results)
Sergey Alexandrovich Kryukov 26-Apr-13 17:21pm    
Ah, clear colors in hand? But this is much simpler. (However, what do you mean by clear?) Contrast to to maximum, flood-feel with anything...
—SA
Member 8973214 26-Apr-13 17:54pm    
ok thanks I'll try contrast.
clear means I want to get the color of hand as in the "desired frame" (not like colors in result frame) but i am getting as in result frame.
the Difference filter code extracting the background nicely , but at the same time it damages the color of overlay/foreground (as in result frame in attached snapshot)
Sergey Alexandrovich Kryukov 26-Apr-13 18:02pm    
Clear... does it have to be realistic color, like the averaged color of the hand, or something?
—SA
Member 8973214 27-Apr-13 16:46pm    
Yes SA, if there is only hand in foreground image then in the result only hand should be its original colors("not working fine", it damages colors of foreground object "in this case hand") and all the background be in black(as working fine).
I created a filter just for that, use it if you like it.

C#
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);
            }
        }

    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900