/// <summary>
/// 将IplImage指针转换成位图对象;
/// 对于不支持的像素格式,可以先使用cvCvtColor函数转换成支持的图像指针
/// </summary>
/// <param name="ptr">IplImage指针</param>
/// <returns>返回位图对象</returns>
public static Bitmap IplImagePointerToBitmap(IntPtr ptr)
{
MIplImage mi = IplImagePointerToMIplImage(ptr);
PixelFormat pixelFormat; //像素格式
string unsupportedDepth = "不支持的像素位深度IPL_DEPTH";
string unsupportedChannels = "不支持的通道数(仅支持1,2,4通道)";
switch(mi.nChannels)
{
case 1:
switch (mi.depth)
{
case IPL_DEPTH.IPL_DEPTH_8U:
pixelFormat = PixelFormat.Format8bppIndexed;
break;
case IPL_DEPTH.IPL_DEPTH_16U:
pixelFormat = PixelFormat.Format16bppGrayScale;
break;
default:
throw new NotImplementedException(unsupportedDepth);
}
break;
case 3:
switch (mi.depth)
{
case IPL_DEPTH.IPL_DEPTH_8U:
pixelFormat = PixelFormat.Format24bppRgb;
break;
case IPL_DEPTH.IPL_DEPTH_16U:
pixelFormat = PixelFormat.Format48bppRgb;
break;
default:
throw new NotImplementedException(unsupportedDepth);
}
break;
case 4:
switch (mi.depth)
{
case IPL_DEPTH.IPL_DEPTH_8U:
pixelFormat = PixelFormat.Format32bppArgb;
break;
case IPL_DEPTH.IPL_DEPTH_16U:
pixelFormat = PixelFormat.Format64bppArgb;
break;
default:
throw new NotImplementedException(unsupportedDepth);
}
break;
default:
throw new NotImplementedException(unsupportedChannels);
}
Bitmap bitmap = new Bitmap(mi.width, mi.height, mi.widthStep, pixelFormat, mi.imageData);
//对于灰度图像,还要修改调色板
if (pixelFormat == PixelFormat.Format8bppIndexed)
SetColorPaletteOfGrayscaleBitmap(bitmap);
return bitmap;
}
/// <summary>
/// 将位图转换成IplImage指针
/// </summary>
/// <param name="bitmap">位图对象</param>
/// <returns>返回IplImage指针</returns>
public static IntPtr BitmapToIplImagePointer(Bitmap bitmap)
{
IImage iimage = null;
switch (bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
iimage = new Image<Gray, Byte>(bitmap);
break;
case PixelFormat.Format16bppGrayScale:
iimage = new Image<Gray, UInt16>(bitmap);
break;
case PixelFormat.Format24bppRgb:
iimage = new Image<Bgr, Byte>(bitmap);
break;
case PixelFormat.Format32bppArgb:
iimage = new Image<Bgra, Byte>(bitmap);
break;
case PixelFormat.Format48bppRgb:
iimage = new Image<Bgr, UInt16>(bitmap);
break;
case PixelFormat.Format64bppArgb:
iimage = new Image<Bgra, UInt16>(bitmap);
break;
default:
Image<Bgra, Byte> tmp1 = new Image<Bgra, Byte>(bitmap.Size);
Byte[, ,] data = tmp1.Data;
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
Color color = bitmap.GetPixel(i, j);
data[j, i, 0] = color.B;
data[j, i, 1] = color.G;
data[j, i, 2] = color.R;
data[j, i, 3] = color.A;
}
}
iimage = tmp1;
break;
}
return iimage.Ptr;
}
/// <summary>
/// 设置256级灰度位图的调色板
/// </summary>
/// <param name="bitmap"></param>
public static void SetColorPaletteOfGrayscaleBitmap(Bitmap bitmap)
{
PixelFormat pixelFormat = bitmap.PixelFormat;
if (pixelFormat == PixelFormat.Format8bppIndexed)
{
ColorPalette palette = bitmap.Palette;
for (int i = 0; i < palette.Entries.Length; i++)
palette.Entries[i] = Color.FromArgb(255, i, i, i);
bitmap.Palette = palette;
}
}