最近用到了一个C#写成的图像算法类,在C++ OpenCV下使用,其中涉及到图像格式在 cv::Mat 和 .Net Bitmap 的互换。网上搜了一些相关程序,总结如下:
(1)cv::Mat 至 .Net Bitmap
/*---------------------------- * 功能 : 将图像格式由 cv::Mat 转换为 System::Drawing::Bitmap * - 不拷贝图像数据 *---------------------------- * 函数 : ConvertMatToBitmap * 访问 : public * 返回 : Bitmap图像指针,若转换失败,则返回的图像宽高均为1 * * 参数 : cvImg [in] OpenCV 图像 */ System::Drawing::Bitmap^ ConvertMatToBitmap(cv::Mat& cvImg) { System::Drawing::Bitmap^ bmpImg; //检查图像位深 if(cvImg.depth() != CV_8U) { cout << "输入图像位深:" << cvImg.depth() << ". 只处理每通道8位深度的图像!" << endl; bmpImg = gcnew System::Drawing::Bitmap(1,1,System::Drawing::Imaging::PixelFormat::Format8bppIndexed); return (bmpImg); } //彩色图像 if(cvImg.channels() == 3) { bmpImg = gcnew Bitmap( cvImg.cols, cvImg.rows, cvImg.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, (System::IntPtr)cvImg.data ); } //灰度图像 else if(cvImg.channels() == 1) { bmpImg = gcnew Bitmap( cvImg.cols, cvImg.rows, cvImg.step, System::Drawing::Imaging::PixelFormat::Format8bppIndexed, (System::IntPtr)cvImg.data); } return (bmpImg); }
(2).Net Bitmap 至 cv::Mat
/*---------------------------- * 功能 : 将图像格式由 System::Drawing::Bitmap 转换为 cv::Mat * - 不拷贝图像数据 *---------------------------- * 函数 : ConvertBitmapToMat * 访问 : public * 返回 : 0:转换成功 * * 参数 : bmpImg [in] .Net 图像 * 参数 : cvImg [out] OpenCV 图像 */ int ConvertBitmapToMat(System::Drawing::Bitmap^ bmpImg, cv::Mat& cvImg) { int retVal = 0; //锁定Bitmap数据 System::Drawing::Imaging::BitmapData^ bmpData = bmpImg->LockBits( System::Drawing::Rectangle(0, 0, bmpImg->Width, bmpImg->Height) , System::Drawing::Imaging::ImageLockMode::ReadWrite, bmpImg->PixelFormat); //若cvImg非空,则清空原有数据 if (!cvImg.empty()) { cvImg.release(); } //将 bmpImg 的数据指针复制到 cvImg 中,不拷贝数据 if(bmpImg->PixelFormat == System::Drawing::Imaging::PixelFormat::Format8bppIndexed) // 灰度图像 { cvImg = cv::Mat(bmpImg->Height, bmpImg->Width, CV_8UC1, (char*)bmpData->Scan0.ToPointer()); } else if (bmpImg->PixelFormat == System::Drawing::Imaging::PixelFormat::Format24bppRgb) // 彩色图像 { cvImg = cv::Mat(bmpImg->Height, bmpImg->Width, CV_8UC3, (char*)bmpData->Scan0.ToPointer()); } //解锁Bitmap数据 bmpImg->UnlockBits(bmpData); return (retVal); }
/*---------------------------- * 功能 : 将 cv::Mat 转换为 Drawing::Bitmap,拷贝图像数据 *---------------------------- * 函数 : CopyMatToBitmap * 访问 : public * 返回 : System::Drawing::Bitmap^ * 参数 : cv::Mat * src */ System::Drawing::Bitmap^ CopyMatToBitmap(cv::Mat *src) { // bitmap 初始化 System::Drawing::Bitmap ^dst = gcnew System::Drawing::Bitmap( src->cols, src->rows, System::Drawing::Imaging::PixelFormat::Format24bppRgb); // 获取 bitmap 数据指针 System::Drawing::Imaging::BitmapData ^data = dst->LockBits( *(gcnew System::Drawing::Rectangle(0, 0, dst->Width, dst->Height)), System::Drawing::Imaging::ImageLockMode::ReadWrite, System::Drawing::Imaging::PixelFormat::Format24bppRgb ); // 获取 cv::Mat 数据地址 src->addref(); // 复制图像数据 if (src->channels() == 3 && src->isContinuous()) { memcpy(data->Scan0.ToPointer(), src->data, src->rows * src->cols * src->channels()); } else { for (int i = 0; i < src->rows * src->cols; i++) { byte *p = (byte *)data->Scan0.ToPointer(); *(p + i * 3) = *(p + i * 3 + 1) = *(p + i * 3 + 2) = *(src->data + i); } } // 释放 cv::Mat 数据 src->release(); // 解除 bitmap 数据保护 dst->UnlockBits(data); return dst; }
参考 :
http://code.google.com/p/kuradevsandbox/source/browse/trunk/UFOHunt/OpenCVTest/Form1.h?spec=svn9&r=9
http://heartthrobkai.pixnet.net/blog/post/6378046-opencv-iplimage-%3C%3D%3D%3E.net-bitmap