如何在C ++ / CLI中将图像从picturebox转换为 :: MAT ?
谢谢
答案 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!!!