opencv__IplImage与Bitmap转换

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值