loading
 
how to convert pixel data from pipe into opencv IplImage
Scott from United States  [1 posts]
7 years
Hi, this is my first post. I am successfully piping image data from from my wireless linksys internet cam into you pipe program example in C++. I am then trying to run face recongition software on the stream, but to do so I need to convert the pixel stream into an opencv IplImage. Do you have any example code for how to convert the piped pixel stream into an IplImage type in opencv?
Thanks in advance for the help with my first post!
Best Regards,
-Scott
from United States  [214 posts] 7 years
Hi Scott,

I don't know if this will help, but I got the following pair of methods many moons ago from this forum and they work for me in C#.  It converts the image byte array obtained from the RoboRealm API to a bitmap.  Perhaps from there you could convert it into the OpenCV format (but I'm not familiar with that).  This is in C# but you can probably easily translated it into C++.

--patrick

        // This method grabs the image bytes for one frame from RoboRealm
        // using the getImage() method and the marker name.
        public void getImageBytes(string marker)
        {
            int width = 1280;
            int height = 960;

            byte[] imageData = new byte[width * height];

            Dimension d = null;

            while (d == null)
            {
                d =RoboRealm.getImage(marker, imageData, width * height);
                RoboRealm.waitImage(1);
            }
        }

        // Create RGB24 bitmap from Byte array
        public Bitmap BitsToBitmapRGB24(Byte[] bytes, int width, int height)
        {
            //swap RGB to BGR
            Byte tmp;
            for (int x = 3; x < bytes.GetLength(0); x += 3)
            {
                tmp = bytes[(x + 2)];
                bytes[x + 2] = bytes[x];
                bytes[x] = tmp;
            }

            if (bytes.GetLength(0) < width * height * 3)
            {
                return null;
            }

            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            int i;

            BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);

            if (data.Stride == width * 3)
            {
                Marshal.Copy(bytes, 0, data.Scan0, width * height * 3);
            }
            else
            {
                for (i = 0; i < bmp.Height; i++)
                {
                    IntPtr p = new IntPtr(data.Scan0.ToInt32() + data.Stride * i);
                    Marshal.Copy(bytes, i * bmp.Width * 3, p, bmp.Width * 3);
                }
            }

            bmp.UnlockBits(data);

            return bmp;
        }
from United States  [214 posts] 7 years
Ooops.  That first method should have returned a byte array.  The correction is:

public byte[] getImageBytes(string marker)
{
   int width = 1280;
   int height = 960;

   byte[] imageData = new byte[width * height];

   Dimension d = null;

   while (d == null)
   {
      d = RoboRealm.getImage(marker, imageData, width * height);
      RoboRealm.waitImage(1);
   }

   return imageData;
}

This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index