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); }
 

 

arrow
arrow
    創作者介紹
    創作者 Cuby 56 的頭像
    Cuby 56

    Cuby56

    Cuby 56 發表在 痞客邦 留言(0) 人氣()