Click here to Skip to main content
15,885,278 members
Articles / Multimedia / GDI+

Image Processing: Skin Detection, Some Filters and EXIF Tags

Rate me:
Please Sign up or sign in to vote.
4.93/5 (19 votes)
17 Jul 2009CPOL2 min read 87.8K   6K   84  
Simple algorithms of skin detection and some useful filters
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace RedMatter
{
    /// <summary>
    /// Soft contour filter.
    /// </summary>
    public class SoftContourFilter
    {
        private Bitmap sourceBitmap;

        public Bitmap SourceBitmap
        {
            get { return sourceBitmap; }
            set { sourceBitmap = value; }
        }

        private Bitmap resultImage;

        public Bitmap ResultImage
        {
            get { return resultImage; }
            set { resultImage = value; }
        }

        private double threshold = 50;

        public double Threshold
        {
            get { return threshold; }
            set { threshold = value; }
        }

        private double softness = 10;

        public double Softness
        {
            get { return softness; }
            set { softness = value; }
        }

        private double[,] core = new double[3, 3];

        public SoftContourFilter(Bitmap bmp)
        {
            sourceBitmap = bmp;
            resultImage = new Bitmap(sourceBitmap);
            InitCore();
            BuildImage();
        }

        private void InitCore()
        {
            core[0, 0] = -1;
            core[0, 1] = -2;
            core[0, 2] = -1;
            core[1, 0] = 0;
            core[1, 1] = 0;
            core[1, 2] = 0;
            core[2, 0] = 1;
            core[2, 1] = 2;
            core[2, 2] = 1;
        }

        private void BuildImage()
        {
            for (int i = 1; i < sourceBitmap.Width-1; i++)
            {
                for (int j = 1; j < sourceBitmap.Height-1; j++)
                {
                    double sx = 0;
                    sx = DrawUtils.GetGray(sourceBitmap.GetPixel(i - 1, j - 1)) * core[0, 0] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i, j - 1)) * core[0, 1] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i + 1, j - 1)) * core[0, 2] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i - 1, j)) * core[1, 0] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i, j)) * core[1, 1] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i + 1, j)) * core[1, 2] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i - 1, j + 1)) * core[2, 0] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i, j + 1)) * core[2, 1] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i + 1, j + 1)) * core[2, 2];

                    double sy = 0;
                    sy = DrawUtils.GetGray(sourceBitmap.GetPixel(i - 1, j - 1)) * core[0, 0] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i, j - 1)) * core[1, 0] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i + 1, j - 1)) * core[2, 0] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i - 1, j)) * core[0, 1] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i, j)) * core[1, 1] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i + 1, j)) * core[2, 1] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i - 1, j + 1)) * core[0, 2] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i, j + 1)) * core[1, 2] +
                            DrawUtils.GetGray(sourceBitmap.GetPixel(i + 1, j + 1)) * core[2, 2];

                    double level = Math.Sqrt(sx * sx + sy * sy);
                    if (level > threshold)
                    {
                        Color color = sourceBitmap.GetPixel(i, j);
                        int r = color.R - (int)(((level - threshold) / threshold) * softness);
                        int g = color.G - (int)(((level - threshold) / threshold) * softness);
                        int b = color.B - (int)(((level - threshold) / threshold) * softness);
                        resultImage.SetPixel(i, j, DrawUtils.GetCorrectColor(r, g, b));
                    }
                    else
                    {
                        //resultImage.SetPixel(i, j, Color.White);
                        resultImage.SetPixel(i, j, sourceBitmap.GetPixel(i, j));
                    }

                }
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
Russian Federation Russian Federation
Hello! My name is Maxim Subbotin.

Now I work in sphere of web-development. I'm interesting researches in SEO field.
If you interesting, you can see this tool:

KeywordCompetitor

Comments and Discussions