Page 1 of 1

convert image to vector

PostPosted: Sun Oct 09, 2011 9:16 pm
by jarain78
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?

thanks

jaime

Re: convert image to vector

PostPosted: Sun Oct 09, 2011 11:43 pm
by Dufresne
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);
unsafe
{
            try
            {
                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;
            }
            finally
            {
                bmp.UnlockBits(imageData); //Unlock
            }
}



Another way is using GetPixel() function of Bitmap in iteration.However, it has some performance problems.

Re: convert image to vector

PostPosted: Mon Oct 10, 2011 4:34 am
by andrew.kirillov
Hello,

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.

@jaime
What is so intelligent about converting image to vector? The topic seems to be more related to image processing.

Re: convert image to vector

PostPosted: Mon Oct 10, 2011 10:36 pm
by jarain78
Hi, thanks for answer me. It was what I need. Sorry for post in the wrong place.
:D :D

jaime