Convert Bitmap to IplImage, and IplImage to Bitmap...
Bitmap to IplImage.
//================================================================= //=== Bitmap to IplImage === //================================================================= IplImage* BitmapToIplImage(Bitmap^ bitmap) { IplImage* tmp; System::Drawing::Imaging::BitmapData^ bmData = bitmap->LockBits( System::Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height) , System::Drawing::Imaging::ImageLockMode::ReadWrite, bitmap->PixelFormat); if(bitmap->PixelFormat == System::Drawing::Imaging::PixelFormat::Format8bppIndexed) { tmp = cvCreateImage(cvSize(bitmap->Width , bitmap->Height) , IPL_DEPTH_8U , 1); tmp->imageData = (char*)bmData->Scan0.ToPointer(); } else if (bitmap->PixelFormat == System::Drawing::Imaging::PixelFormat::Format24bppRgb) { tmp = cvCreateImage(cvSize(bitmap->Width , bitmap->Height) , IPL_DEPTH_8U , 3); tmp->imageData = (char*)bmData->Scan0.ToPointer(); } bitmap->UnlockBits(bmData); return tmp; }
IplImage to Bitmap.
//================================================================= //=== IplImage to Bitmap === //================================================================= Bitmap^ IplimgToBitmap(IplImage* ipl) { try{ return (ipl->depth == 8)? ( (ipl->nChannels == 3)? ( gcnew Bitmap(ipl->width , ipl->height , ipl->widthStep , Imaging::PixelFormat::Format24bppRgb , (System::IntPtr)ipl->imageData) ) : ( gcnew Bitmap(ipl->width , ipl->height , ipl->widthStep , Imaging::PixelFormat::Format8bppIndexed , (System::IntPtr)ipl->imageData) ) ) : ( gcnew Bitmap(ipl->width , ipl->height , ipl->widthStep , Imaging::PixelFormat::Format32bppPArgb , (System::IntPtr)ipl->imageData) ); } catch(...) { IplImage* iplimg = cvCreateImage( cvSize(100,100) , IPL_DEPTH_8U , 3 ); cvZero( iplimg ); return IplimgToBitmap( iplimg ); } }
String to Char
//================================================================= //=== String to Char === //================================================================= char* StringToChar(String^ str) { return (char*)(void*)Marshal::StringToHGlobalAnsi(str); }
文章標籤
全站熱搜