Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Image Recognition with Neural Networks

Rate me:
Please Sign up or sign in to vote.
4.82/5 (89 votes)
30 Oct 2007CPOL4 min read 949.5K   46.2K   286  
This article contains a brief description of BackPropagation Artificial Neural Network and its implementation for Image Recognition
#region Copyright (c), Some Rights Reserved
/*##########################################################################
 * 
 * ImageProcessing.cs
 * -------------------------------------------------------------------------
 * By
 * Murat FIRAT, September 2007
 * 
 * -------------------------------------------------------------------------
 * Description:
 * Some Useful Methods For Handling Images..
 * 
 * -------------------------------------------------------------------------
 ###########################################################################*/
#endregion

using System;
using System.Drawing;

namespace BPSimplified
{
    class ImageProcessing
    {
        //Convert RGB To Matrix [Of Double]
        public static double[] ToMatrix(Bitmap BM, int MatrixRowNumber, int MatrixColumnNumber)
        {
            double HRate = ((Double)MatrixRowNumber / BM.Height);
            double WRate = ((Double)MatrixColumnNumber / BM.Width);
            double[] Result = new double[MatrixColumnNumber * MatrixRowNumber];

            for (int r = 0; r < MatrixRowNumber; r++)
            {
                for (int c = 0; c < MatrixColumnNumber; c++)
                {
                    Color color = BM.GetPixel((int)(c / WRate), (int)(r / HRate));
                    Result[r * MatrixColumnNumber + c] = 1 - (color.R * .3 + color.G * .59 + color.B * .11) / 255;
                }
            }
            return Result;
        }

        //Convert Double To Grey Level  
        public static Bitmap ToImage(double[] Matrix, int MatrixRowNumber, int MatrixColumnNumber,
                                                     int ImageHeight, int ImageWidth)
        {
            double HRate = ((double)ImageHeight / MatrixRowNumber);
            double WRate = ((double)ImageWidth / MatrixColumnNumber);
            Bitmap Result = new Bitmap(ImageWidth, ImageHeight);

            for (int i = 0; i < ImageHeight; i++)
            {
                for (int j = 0; j < ImageWidth; j++)
                {
                    int x = (int)((double)j / WRate);
                    int y = (int)((double)i / HRate);

                    double temp = Matrix[y * MatrixColumnNumber + x];
                    Result.SetPixel(j, i, Color.FromArgb((int)((1 - temp) * 255), (int)((1 - temp) * 255), (int)((1 - temp) * 255)));

                }
            }
            return Result;
        }

        //public static Bitmap Scale(Bitmap Input, int newHeight, int newWidth)
        //{
        //    double HRate = (double)Input.Height / newHeight;
        //    double WRate = (double)Input.Width / newWidth;
        //    Bitmap Result = new Bitmap(newWidth, newHeight);
        //    for (int i = 0; i < newHeight; i++)
        //    {
        //        for (int j = 0; j < newWidth; j++)
        //        {
        //            int x = (int)((double)j * WRate);
        //            int y = (int)((double)i * HRate);
        //            Result.SetPixel(j, i, Input.GetPixel(x, y));
        //        }
        //    }

        //    return Result;
        //}
    }
}

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 (Senior)
Turkey Turkey
Has BS degree on computer science, working as software engineer in istanbul.

Comments and Discussions