简要介绍:在windows编程中,很多图片信息,都是基于windows系统所谓的DIB(设备无关位图)的结构,其定义在结构体 BITMAPINFOHEADER 中。本方法可以讲 windows 定义的DIB 结构和Opencv内部定义的IPLImage结构做相互转化。 具体使用方法:将文件 bmp2ipl.h 和 bmp2ipl.cpp 添加到你的代码中。 头文件 bmp2ipl.h
实现文件bmp2ipl.cpp
IsSupport 这个函数没有定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | //bmp2ipl.h #ifndef BMP2IPL_H #define BMP2IPL_H class BMP { public : BMP():bmpData(NULL) { memset (&biHeader, 0, sizeof (biHeader)); } BMP( const BMP & img); BMP( const IplImage &img); BMP( int width, int height, int bitCount); ~BMP(){ delete [] bmpData; } bool CreateImage( const BITMAPINFOHEADER &biHeader); //Export IplImage * BMP2Ipl(); //void Show(HWND hWnd, int nID); //void Show(CDC *pDC, CRect & rect); //void Show(HWND hWnd); void ReSize( int newW, int newH); private : void CopyData( char *dest, const char *src, int dataByteSize, bool isConvert, int height); // isConvert=true 进行顶左和底左之间的转换(顶左到底左或底左到顶左),否则不转换 // biSizeImage may be set to zero for BI_RGB bitmaps, so calculate it. int ImgSize() { return ( biHeader.biHeight * ((biHeader.biWidth * biHeader.biBitCount / 8 + 3) & (-4))); } public : void Clear(); BITMAPINFOHEADER biHeader; unsigned char * bmpData; }; #endif |
实现文件bmp2ipl.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #include "bmp2ipl.h" /************************************************************************************************** * * * NOTE: * * Only use the "8 bit, 1 or 3 channels" image, the BitMap use LowerLeft (底左), * * the IplImage use TopLeft (顶左) * * IplImage: * * nChannels = 1 or 3 number of channels * * depth = IPL_DEPTH_8U pixel depth (8 bit), IPL_DEPTH_8U=8 * * dataOrder = 0 交叉存取颜色通道 * * origin = IPL_ORIGIN_TL 图像数据保存形式,IPL_ORIGIN_BL底左结构, IPL_ORIGIN_TL顶左结构 * * align = 4 行数据对齐方式,保证下一行数据从整4字节位置开始 * * width * * height * * widthStep 图像数据行大小,单位字节 * * imageSize 图像数据大小(=height*widthStep),单位字节 * * imageData 指向图像数据区,char * * * BITMAPINFOHEADER: * * biSize Specifies the number of bytes required by the structure * * biWidth * * biHeight(>0) biHeight>0底左结构;biHeight<0顶左结构 * * biPlanes = 1 Specifies the number of planes for the target device. This value * * must be set to 1. * * biBitCount =8 or 24 bits-per-pixel,8-(pixelDepth=8,channels=1), * * 24-(pixelDepth=8,channels=3) * * biCompression = BI_RGB An uncompressed format * * biSizeImage Specifies the size, in bytes, of the image.This may be set to zero * * for BI_RGB bitmaps. * * biXPelsPerMeter = 0 * * biYPelsPerMeter = 0 * * biClrUsed = 0 * * biClrImportant = 0 * * * ***************************************************************************************************/ BMP::BMP( const BMP & img) { if (!IsSupport(img)) { BMP(); return ; } //biHeader = img.biHeader; PBITMAPINFOHEADER pBmpH = (PBITMAPINFOHEADER)&img.biHeader; memcpy (&biHeader, pBmpH, sizeof (BITMAPINFOHEADER)); biHeader.biSizeImage = ImgSize(); bool isLowerLeft = biHeader.biHeight>0; //int rowSize=0; if (!isLowerLeft) biHeader.biHeight=-biHeader.biHeight; if (bmpData!=NULL) delete [] bmpData; bmpData = new unsigned char [biHeader.biSizeImage]; //memcpy(bmpData, img.bmpData, img.biHeader.biSizeImage); CopyData(( char *)bmpData, ( char *)img.bmpData, biHeader.biSizeImage, !isLowerLeft, biHeader.biHeight); } BMP::BMP( const IplImage &img) { if (!IsSupport(img)) { BMP(); return ; } bool isTopLeft = (img.origin == IPL_ORIGIN_TL); biHeader.biSize = sizeof (BITMAPINFOHEADER); biHeader.biWidth = img.width; biHeader.biHeight = img.height; biHeader.biPlanes = 1; biHeader.biBitCount = img.depth * img.nChannels; biHeader.biCompression = BI_RGB; biHeader.biSizeImage = img.imageSize; biHeader.biXPelsPerMeter = 0; biHeader.biYPelsPerMeter = 0; biHeader.biClrUsed = 0; biHeader.biClrImportant = 0; if (bmpData!=NULL) delete [] bmpData; bmpData = new unsigned char [img.imageSize]; //memcpy(bmpData, img.ImageData, img.imageSize); CopyData(( char *)bmpData, ( char *)img.imageData, img.imageSize, isTopLeft, img.height); /*int i,j; CvScalar s; for(i=0;i<img.width;i++) for(j="0;j<img.height;j++){" s="cvGet2D(&img,i,j);" }="" *="" bmp::bmp(int="" width,="" int="" height,="" bitcount)="" {="" if(bitcount!="8" &&="" bitcount!="24)" return;="" biheader.bisize="sizeof(BITMAPINFOHEADER);" biheader.biwidth="width;" biheader.biheight="height;" biheader.biplanes="1;" biheader.bibitcount="bitCount;" biheader.bicompression="BI_RGB;" biheader.bisizeimage="ImgSize();" biheader.bixpelspermeter="0;" biheader.biypelspermeter="0;" biheader.biclrused="0;" biheader.biclrimportant="0;" if(bmpdata!="NULL)" delete[]="" bmpdata;="" bmpdata="new" unsigned="" char="" [biheader.bisizeimage];="" clear();="" dest:="" the="" destination="" image="" databytesize:="" source="" height:="" height="" void="" bmp::copydata(char="" *dest,="" const="" *src,="" databytesize,="" bool="" isconvert,="" height)="" p="dest;" if(!isconvert)="" memcpy(dest,="" src,="" databytesize);="" if(height<="0)" rowbytesize="dataByteSize" height;="" src="src" +="" databytesize="" -="" ;="" for(int="" i="0;" i<height;="" i++)="" rowbytesize);="" dest="" iplimage="" bmp::bmp2ipl()="" if(!issupport(*this))="" return="" null;="" *iplimg;="" islowerleft="biHeader.biHeight">0; height = (biHeader.biHeight>0) ? biHeader.biHeight : -biHeader.biHeight; iplImg = cvCreateImage( cvSize(biHeader.biWidth, height), IPL_DEPTH_8U, biHeader.biBitCount / 8); //iplImg = cvCreateImageHeader( cvSize(biHeader.biWidth, height), IPL_DEPTH_8U, biHeader.biBitCount / 8); //cvSetData(iplImg,(char*)bmpData,biHeader.biSizeImage/height); CopyData( iplImg->imageData, (char*)bmpData, biHeader.biSizeImage, isLowerLeft, height); /*int i,j; CvScalar s; int channels=biHeader.biBitCount / 8; int step=(biHeader.biWidth*channels+3) & -4; int loc=0; for(i=0;i<iplimg->height;i++){ for(j=0;j<iplimg->width;j++){ loc=i*step + j*channels; s.val[0]=bmpData[loc]; if(channels==3){ s.val[1]=bmpData[loc+1]; s.val[2]=bmpData[loc+2]; } cvSet2D(iplImg,i,j,s); } }*/ return iplImg; } void BMP::Clear() { if (bmpData == NULL) return ; memset (bmpData, 0, ImgSize()); } void BMP::ReSize( int newW, int newH) { biHeader.biWidth = newW; biHeader.biHeight = newH; biHeader.biSizeImage = ImgSize(); if (bmpData!=NULL) delete [] bmpData; bmpData = new unsigned char [biHeader.biSizeImage]; Clear(); } bool BMP::CreateImage( const BITMAPINFOHEADER &bih) { memset (&biHeader,0, sizeof (BITMAPINFOHEADER)); delete [] bmpData; bmpData = NULL; memcpy (&biHeader, &bih, sizeof (BITMAPINFOHEADER)); biHeader.biSizeImage = ImgSize(); bmpData = new unsigned char [ biHeader.biSizeImage ]; if (bmpData == NULL) return false ; else { Clear(); return true ; } } </iplimg-></iplimg-></img.width;i++)> |
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2302
- 用户1336
- 访客10976374
每日一句
If you want to achieve greatness, stop asking for permission.
如果你想获得伟大,别再请求许可。
如果你想获得伟大,别再请求许可。
UAC的限制引起WM_DROPFILES无法响应的解决办法
MeasureSpec中三种模式:UNSPECIFIED,AT_MOST,EXACTLY
发几个实用的chrome插件
CentOS下使用 svnsync迁移SVN代码库
仙剑奇侠传3d回合-PC端辅助
【转载】C++实现EXE加载到内存执行
【收藏】OpenCV一些常用库函数
《闲来麻将》搭建教程
文本转语音系统Spark-TTS
wordpress转xiuno附件自动插入工具
Mac OS最简单及(Karabiner)快捷键设置
使用Putty上传文件?
ndk神奇问题之non-numeric second argument to `wordlist' function: '8.7z'
新会员