:: AForge.NET Framework :: Articles :: Forums ::

convert image to vector

The forum is to discuss topics from different artificial intelligence areas, like neural networks, genetic algorithms, machine learning, etc.

convert image to vector

Postby jarain78 » Sun Oct 09, 2011 9:16 pm

hi, mi name is jaime, and in this moment I`m making a program, using AForge.NET, wich is a manuscript recognition, but i need conver a image of 16x16 to vector 1x256, and i don't know how to do it. Could you help me with this?


Posts: 6
Joined: Sun Oct 09, 2011 8:58 pm

Re: convert image to vector

Postby Dufresne » Sun Oct 09, 2011 11:43 pm

I suppose , you have a grayscale image, then you have to iterate through each pixel and assign them to vector.

Here's one way to do it with pointer.
Code: Select all
byte []vector = new byte[256];
Bitmap bmp  ; //Image to be converted, load from somewhere ,
BitmapData imageData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
               ImageLockMode.ReadOnly, bmp.PixelFormat);
                UnmanagedImage img = new UnmanagedImage(imageData);

                int height = img.Height;
                int width = img.Width;
                byte* p = (byte*)img.ImageData.ToPointer();
                int i = 0;

                for (int y = 0; y < height; y++)
                    for (int x = 0; x < width; x++, p++)
                        vector[i++] = *p;
                bmp.UnlockBits(imageData); //Unlock

Another way is using GetPixel() function of Bitmap in iteration.However, it has some performance problems.
Posts: 31
Joined: Thu Jun 03, 2010 8:59 pm
Location: Istanbul

Re: convert image to vector

Postby andrew.kirillov » Mon Oct 10, 2011 4:34 am


One thing about the code is to remember changing this:
Dufresne wrote:byte []vector = new byte[256];

to this:
Code: Select all
byte []vector = new byte[bmp.Width*bmp.Height];

Also don't forget about things like stride - length of line in bytes. This code assumes that stride equals to width, which is not always true. Using UnmanagedImage it is possible to get an image, which is actually rectangle of another image. This is where stride gives such a flexibility, so you don’t need to crop image by copying it.

What is so intelligent about converting image to vector? The topic seems to be more related to image processing.
With best regards,

Interested in supporting AForge.NET Framework?
User avatar
Site Admin, AForge.NET Developer
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: convert image to vector

Postby jarain78 » Mon Oct 10, 2011 10:36 pm

Hi, thanks for answer me. It was what I need. Sorry for post in the wrong place.
:D :D

Posts: 6
Joined: Sun Oct 09, 2011 8:58 pm

Return to Artificial Intelligence