将System :: Drawing :: Bitmap ^转换为:: MAT

时间:2014-12-27 03:48:24

标签: opencv c++-cli

如何在C ++ / CLI中将图像从picturebox转换为 :: MAT

谢谢

1 个答案:

答案 0 :(得分:1)

您需要 cast 将Drawing.Image转换为 Bitmap (假设图片确实是位图)。
然后锁定 System.Drawing.Bitmap ,并使用BitmapData的Scan0属性访问内部缓冲区。

System::Drawing::Bitmap ^ bitmapFrame = safe_cast< System::Drawing::Bitmap ^ >(pictureBox1->Image);

BitmapData^ bmpData = bitmapFrame->LockBits(gcnew Rectangle(0, 0, bitmapFrame->Width, bitmapFrame->Height), System::Drawing::Imaging::ImageLockMode::ReadWrite, 
            bitmapFrame->Format);
try
{    
    void* data = bmpData.Scan0;

    //use the data in the ::Mat constructor.
}
finally { bitmapFrame->UnlockBits(bmpData); }//Remember to unlock!!!