在MFC编程中,用OpenCV来处理图像时,可能会进行Bitmap与IplImage之间的转换;所以在此留个记号,以免下次再用到的时候,还要去找。
IplImage* BitmapToIplImage(HBITMAP hBmp)
{
BITMAP bmp;
GetObject(hBmp, sizeof(BITMAP), &bmp);
int depth = (bmp.bmBitsPixel == 1) ? IPL_DEPTH_1U : IPL_DEPTH_8U;
int nChannels = (bmp.bmBitsPixel == 1) ? 1 : bmp.bmBitsPixel/8;
IplImage* img = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight), depth, nChannels);
BYTE *pBuffer = new BYTE[bmp.bmHeight*bmp.bmWidth*nChannels];
GetBitmapBits(hBmp, bmp.bmHeight*bmp.bmWidth*nChannels, pBuffer);
memcpy(img->imageData, pBuffer, bmp.bmHeight*bmp.bmWidth*nChannels);
delete pBuffer;
IplImage *dst = cvCreateImage(cvGetSize(img), img->depth,3);
cvCvtColor(img, dst, CV_BGRA2BGR);
cvReleaseImage(&img);
return dst;
}
如果要从CBitmap转为IplImage,可以先将CBitmap转为BITMAP,再由BITMAP转为IplImage;
// CBitmap 转为 BITMAP
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP);
BITMAP bmp;
bitmap.GetBitmap(&bmp);
// CBitmap与HBITMAP间的转换
// CBitmap转为HBITMAP
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP);
HBITMAP bmp = HBITMAP(bitmap);
// HBITMAP转为CBitmap
HBITMAP hbitmap;
CBitmap bitmap;
bitmap.Attach(hbitmap);
分享到:
相关推荐
IplImage和CBitmap转换的总结
Android图片Bitmap和字符串String之间相互转换的工具类,外加项目代码,封装好的,直接可以用,
Halcon_Bitmap转换方法 写的文档 开发笔记
CBitmap,HBITMAP,BITMAP的关系与转换CBitmap,HBITMAP,BITMAP的关系与转换CBitmap,HBITMAP,BITMAP的关系与转换
一个在VC中将Bitmap转换为Byte[]的小例子。
将图片由bitmap类型转换为stream类型再转换为byte[]类型,然后再还原
bitmap switcher 位图转换 32位/24位/16位/8位/4位/1位 注:会提示不安全。请加入信任!
Bitmap、File与Uri 1、将一个文件路径path转换成File String path ; File file = new File(path) 2、讲一个Uri转换成一个path 以选择一张图片为例: String path = FileTools.getRealPathFromUri(content...
Drawable Bitmap InputStream byte[]相互转化工具类,单例模式
Drawable Bitmap之间的转化Drawable Bitmap之间的转化
机器视觉技术必备,从位图到halcon图像,bitmap图像转为halcon图像,bitmap to Hobject
Camera onPreview中byte[] 转换为Bitmap 在录像时设置了YV12要保存图片时 YV12或者NV12时要转换时所用的工具的优化
BMP2H264 Bitmap图像转换为H264
C#语言下,官方好像没有提供直接的方式把Bitmap转为Mat,但是可以使用我的这个方式转变格式
软件开发网在此之前给大家介绍过图片加载框架Glide的基本用法介绍,大家可以先参考一下,本篇内容更加深入的分析了Glide获取图片Path、Bitmap用法,以及实现的代码分析。 1. 获取Bitmap: 1)在图片下载缓存好之后...
主要对于应用NI Vision和C#的,bitmap和Ni图像之间的一个转换(彩色)
文档中描述了Drawable Bitmap 几种转换方法供初学者使用。方法都是经过实践验证的。
android里面 canvas 转换成bitmap
改用原始图像数据流回调函数RegisterImageStreamCallback进行抓图 但回调函数帧为YUV420格式 需要转成Bitmap才用用于显示及本地查看 在网上找了好多都是基于R Y+1 4075 V 128 G Y 0 3455 U 128 – 0 7169 V 128 B Y+...
Android Drawable、Bitmap、byte、灰度 之间的转换