Ejemplo n.º 1
0
        /// <summary>
        /// OpenCVのIplImageに変換
        /// </summary>
        /// <returns>IplImage形式の画像</returns>
        public IplImage ToIplImage()
        {
            // Bitmapから大きさの情報をとる
            IplImage img = new IplImage(Cv.Size(data.Width, data.Height), BitDepth.U8, 4);
            int[] tmp = new int[data.Width * data.Height];

            unsafe
            {
                SDI.BitmapData bd = data.LockBits(
                  new SD.Rectangle(0, 0, data.Width, data.Height),
                  SDI.ImageLockMode.ReadOnly,
                  SDI.PixelFormat.Format32bppArgb
                  );

                uint* byteData = (uint*)bd.Scan0;

                // Switch bgra -> rgba
                for (int i = 0; i < data.Width * data.Height; i++)
                    byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000);

                // copy data
                Marshal.Copy(bd.Scan0, tmp, 0, tmp.Length);
                Marshal.Copy(tmp, 0, img.ImageData, tmp.Length);

                // unlock bitmap
                data.UnlockBits(bd);
            }
            IplImage cvt = img.Clone();
            img.Flip(cvt, FlipMode.XY);

            return cvt;
        }