求助 关于DC 哪位帮我改下啊...........
目的:统计测试数据(图片上数字)和。思路:先切割出0-9 10个数字
把图片放到内存DC上
在DC上取像素点 比较
相同最多的即为该位数
程序如下:(哪位帮我改下啊.................)
程序代码:
#ifndef BMP_CLASS #define BMP_CLASS #include <afxwin.h> #include <stdio.h> #include <string.h> class BMP { HDC NumDC[10]; int num; public: BMP() { char lpFileName[20]="num (x).BMP"; num = 0; for(int i=0;i<10;i++)//读入10张数字图片 { lpFileName[5]='0'+i; NumDC[i]=ReadBmp(lpFileName); } }; int GetNum()const{return num;}; void Calculate(LPCTSTR lpFileName); private: //读取图片的函数,返回内存DC HDC ReadBmp(LPCTSTR lpFileName)const; //读取图片的函数,返回内存DC }; //读取图片的函数,返回内存DC HDC BMP::ReadBmp(LPCTSTR lpFileName)const { POINT dwBitmapSize;//定义图片的高,宽 //打开图片 HANDLE hFile=::CreateFile(lpFileName,GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL); if(hFile==INVALID_HANDLE_VALUE) { printf("读取文件失败!\n"); return 0; } //创建一个内存映射文件对象 HANDLE hMap=::CreateFileMapping(hFile,NULL,PAGE_READONLY,NULL,NULL,NULL); //将文件内容与线性地址对应,返回地址 LPVOID lpBase=::MapViewOfFile(hMap,FILE_MAP_READ,0,0,0); BITMAPFILEHEADER *pFileHeader;//图片的BITMAPFILEHEADER结构 BITMAPINFO *pInfoHeader; //图片的BITMAPINFO结构 pFileHeader=(BITMAPFILEHEADER*)lpBase; //图片象素部分内容地址 BYTE *pBits=(BYTE*)lpBase+pFileHeader->bfOffBits; //BITMAPINFO结构地址并获取图片大小 pInfoHeader=(BITMAPINFO*)( (BYTE*)lpBase+sizeof(BITMAPFILEHEADER) ); dwBitmapSize.x=pInfoHeader->bmiHeader.biWidth; dwBitmapSize.y=pInfoHeader->bmiHeader.biHeight; //创建内存DC和位图 HDC hCompatibleDC=::CreateCompatibleDC(NULL); HBITMAP hBitmap=::CreateCompatibleBitmap(NULL,dwBitmapSize.x,dwBitmapSize.y); ::SelectObject(hCompatibleDC,hBitmap); //将图片信息放到内存DC中 ::SetDIBitsToDevice(hCompatibleDC,0,0,dwBitmapSize.x,dwBitmapSize.y, 0,0,0,dwBitmapSize.y,pBits,pInfoHeader,DIB_RGB_COLORS); //关闭句柄 ::DeleteObject(hBitmap); ::UnmapViewOfFile(lpBase); ::CloseHandle(hMap); ::CloseHandle(hFile); //返回内存DC return hCompatibleDC; } //计算 void BMP::Calculate(LPCTSTR lpFileName) { int now=0,max=0,nownum; COLORREF c1,c2; //ReadBmp(LPCTSTR lpFileName,HDC hdc) HDC hdc; hdc=BMP::ReadBmp(lpFileName);//读入待计算的图片 for(int number=0;number<5;number++)//读取第number个数 { now =0; nownum = 0; for(int i=0;i<10;i++)//用第i个数和它比较 { for(int x=0+number*10;x<10+number*10;x++)//读取第x列的像素点 { for(int y=0;y<11;y++)//读取第y行的像素点 { c1 = ::GetPixel(hdc,x,y); c2 = ::GetPixel(NumDC[i],x-number*10,y); if(c1==c2)now++;printf("%ld,%ld\n",c1,c2); } } if(now>max){max=now;nownum = i;} printf("nownum is %d\n",nownum); } switch(number) { case 0: num+=10000*nownum;break; case 1: num+=1000*nownum;break; case 2: num+=100*nownum;break; case 3: num+=10*nownum;break; case 4: num+=1*nownum;break; } } } #endif int main() { BMP NUM; char filename[20]; int i=0,j=0,k=0; strcpy(filename,"0.bmp"); for(i=0;i<10;i++) { filename[0]='0'+i; NUM.Calculate(filename); printf("%d\n",NUM.GetNum()); getchar(); } strcpy(filename,"10.bmp"); for(i=1;i<10;i++) { for(j=0;j<10;j++) { filename[0]='0'+i; filename[1]='0'+j; NUM.Calculate(filename); } } strcpy(filename,"100.bmp"); for(i=1;i<10;i++) { for(j=0;j<10;j++) { for(k=0;k<10;k++) { filename[0]='0'+i; filename[1]='0'+j; filename[2]='0'+k; NUM.Calculate(filename); } } } printf("[ %d ]\n",NUM.GetNum()); getchar(); return 0; }
测试数据.rar
(36.15 KB)