《位真彩色转换为位灰度图片(完整代码)》由会员分享,可在线阅读,更多相关《位真彩色转换为位灰度图片(完整代码)(23页珍藏版)》请在金锄头文库上搜索。
1、24 位真彩色转换为位灰度图片(完整代码)分类: C#2011-03-04 09:29 4343 人阅读 评论(5) 收藏 举报nullfloatgdi+byte 图像处理 image 图像的灰度与二值化图像的灰度与二值化http:/ = red * 0.299 + green * 0.587 + blue * 0.114,如果在程序代码中直接套用了这个公式,因浮点数的缘故导致代码执行效率较低,如改为定点整因浮点数的缘故导致代码执行效率较低,如改为定点整数运算,可使执行效率大大提高。数运算,可使执行效率大大提高。下面是图像的灰度与二值化代码:/ 定义 ARGB 像素结构typedef unio
2、nARGB Color;structBYTE Blue;BYTE Green;BYTE Red;BYTE Alpha;ARGBQuad, *PARGBQuad;/-/ 图像数据 data 灰度化VOID Gray(BitmapData *data)PARGBQuad p = (PARGBQuad)data-Scan0;INT offset = data-Stride - data-Width * sizeof(ARGBQuad);for (UINT y = 0; y Height; y +, (BYTE*)p += offset)for (UINT x = 0; x Width; x +, p
3、 +)p-Blue = p-Green = p-Red =(UINT)(p-Blue * 29 + p-Green * 150 + p-Red * 77 + 128) 8;/-/ 图像数据 data 灰度同时二值化,threshold 阀值VOID GrayAnd2Values(BitmapData *data, BYTE threshold)PARGBQuad p = (PARGBQuad)data-Scan0;INT offset = data-Stride - data-Width * sizeof(ARGBQuad);for (UINT y = 0; y Height; y +, (B
4、YTE*)p += offset)for (UINT x = 0; x Width; x +, p +)if (p-Blue * 29 + p-Green * 150 + p-Red * 77 + 128) 8) Color elsep-Color |= 0x00ffffff;/-因本文使用的是 32 位图像数据,所以图像的二值化没有采用通常的赋值操作 p-Blue = p-Green = p-Red = 0(或者 255),而是采用了位运算。下面是使用 BCB2007 和 GDI+图像数据实现图像灰度和二值化的例子代码:/ 锁定 GDI+位位图扫描线到 dataFORCEINLINEVOID
5、 LockBitmap(Gdiplus:Bitmap *bmp, BitmapData *data)Gdiplus:Rect r(0, 0, bmp-GetWidth(), bmp-GetHeight();bmp-LockBits(/-/ GDI+位图扫描线解锁FORCEINLINEVOID UnlockBitmap(Gdiplus:Bitmap *bmp, BitmapData *data)bmp-UnlockBits(data);/-void _fastcall TForm1:Button1Click(TObject *Sender)Gdiplus:Bitmap *bmp = new Gd
6、iplus:Bitmap(L“d:source1.jpg“);Gdiplus:Graphics *g = new Gdiplus:Graphics(Canvas-Handle);g-DrawImage(bmp, 0, 0);BitmapData data;LockBitmap(bmp, / Gray(GrayAnd2Values(UnlockBitmap(bmp, g-DrawImage(bmp, data.Width, 0);delete g;delete bmp;/-24 位真彩色转换为位灰度图片(完整代码)位真彩色转换为位灰度图片(完整代码)/Code By xets007/转载请注明出
7、处#include BOOL BMP24to8(char *szSourceFile,char *szTargetFile);int main(int argc,char* argv)/调用这个函数直接把 24 位真彩色灰度化BOOL stat=BMP24to8(“c:/source.bmp“,“c:/target.bmp“);return 0;BOOL BMP24to8(char *szSourceFile,char *szTargetFile)HANDLE hSourceFile=INVALID_HANDLE_VALUE,hTargetFile=INVALID_HANDLE_VALUE;D
8、WORD dwSourceSize=0,dwTargetSize=0;PBYTE pSource=NULL,pTarget=NULL;hSourceFile=CreateFile(szSourceFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);if(hSourceFile=INVALID_HANDLE_VALUE)return FALSE;dwSourceSize=GetFileSize(hSourceFile,NULL);pSource=(PBYTE)VirtualAlloc(N
9、ULL,dwSourceSize,MEM_COMMIT,PAGE_READWRITE);/分配空间失败或者文件太小(BMP 文件不可能小于 54 个字节)if(pSource=NULL|dwSourceSizebfType!=0x4d42|pSourceInfoHeader-biBitCount!=24)CloseHandle(hSourceFile);VirtualFree(pSource,NULL,MEM_RELEASE);return FALSE;CloseHandle(hSourceFile);LONG nWidth=pSourceInfoHeader-biWidth;LONG nHe
10、ight=pSourceInfoHeader-biHeight;LONG nSourceWidth=nWidth*3;if(nSourceWidth%4) nSourceWidth=(nSourceWidth/4+1)*4;LONG nTargetWidth=nWidth;if(nTargetWidth%4) nTargetWidth=(nTargetWidth/4+1)*4;dwTargetSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256+nHeight*nTargetWidth;pTarge
11、t=(PBYTE)VirtualAlloc(NULL,dwTargetSize,MEM_COMMIT,PAGE_READWRITE);memset(pTarget,0,dwTargetSize);if(pTarget=NULL)VirtualFree(pTarget,NULL,MEM_RELEASE);return FALSE;BITMAPFILEHEADER *pTargetFileHeader=(BITMAPFILEHEADER *)pTarget;BITMAPINFOHEADER *pTargetInfoHeader =(BITMAPINFOHEADER *)(pTarget+sizeo
12、f(BITMAPFILEHEADER);pTargetFileHeader-bfType=pSourceFileHeader-bfType;pTargetFileHeader-bfSize=dwTargetSize;pTargetFileHeader-bfReserved1=0;pTargetFileHeader-bfReserved2=0;pTargetFileHeader-bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256;pTargetInfoHeader-biBitCount=8
13、;pTargetInfoHeader-biClrImportant=0;pTargetInfoHeader-biClrUsed=256;pTargetInfoHeader-biCompression=BI_RGB;pTargetInfoHeader-biHeight=pSourceInfoHeader-biHeight;pTargetInfoHeader-biPlanes=1;pTargetInfoHeader-biSize=sizeof(BITMAPINFOHEADER);pTargetInfoHeader-biSizeImage=nHeight*nTargetWidth;pTargetIn
14、foHeader-biWidth=pSourceInfoHeader-biWidth;pTargetInfoHeader-biXPelsPerMeter=pSourceInfoHeader-biXPelsPerMeter;pTargetInfoHeader-biYPelsPerMeter=pSourceInfoHeader-biYPelsPerMeter;RGBQUAD *pRgb;for(int i=0;irgbBlue=i;pRgb-rgbGreen=i;pRgb-rgbRed=i;pRgb-rgbReserved=0;for (int m=0;mbfOffBits+m*nTargetWidth+n =pSourcepSourceFileHeader-bfOffBits+m*nSo