#include #include #include"bmp_io.h" LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); ATOM InitApp(HINSTANCE); BOOL InitInstance(HINSTANCE,int); void threshold(LPDWORD image_in,LPDWORD image_out,int thresh,int mode); void histgram(LPDWORD image_in,long hist[256]); void histsmooth(long hist_in[256],long hist_out[256]); char szClassName[]="template"; int iWidth,iHeight; const int HIGH=0x00ffffff,LOW=0x00000000; int WINAPI WinMain(HINSTANCE hCurInst,HINSTANCE hPrevInst, LPSTR lpsCmdLine,int nCmdShow) { MSG msg; BOOL bRet; if(!InitApp(hCurInst)) return FALSE; if(!InitInstance(hCurInst,nCmdShow)) return FALSE; while((bRet=GetMessage(&msg,NULL,0,0))!=0){ if(bRet==-1){ MessageBox(NULL,"GetMessageエラー","Error",MB_OK); break; }else{ TranslateMessage(&msg); DispatchMessage(&msg); } } return (int)msg.wParam; } ATOM InitApp(HINSTANCE hInst) { WNDCLASSEX wc; wc.cbSize=sizeof(WNDCLASSEX); wc.style=CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc=WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInst; wc.hIcon=(HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0,0, LR_DEFAULTSIZE|LR_SHARED); wc.hCursor=(HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0,0, LR_DEFAULTSIZE|LR_SHARED); wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName=NULL; wc.lpszClassName=(LPCTSTR)szClassName; wc.hIconSm=(HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0,0, LR_DEFAULTSIZE|LR_SHARED); return RegisterClassEx(&wc); } BOOL InitInstance(HINSTANCE hInst,int nCmdShow) { HWND hWnd; hWnd=CreateWindow(szClassName, "第3章 物体を抜き出す", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL); if(!hWnd) return FALSE; ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp) { static BMP_IO mybmp; static LPBITMAPINFO lpbiInfo; static LPDWORD lpPixel,lpPixel2; long hist[256]; static long hist2[256]; static int py=275; int i; HPEN hPen,hOldPen; RECT rc={300,0,800,250}; char text[64]; static char *info="8ビットBMPファイルをドロップして下さい\n\n" "<左上>原画像 or 処理結果\n<左下>ヒストグラム(平滑化しています)\n<右中>赤いラインの示すヒストグラム値\n\n" "↑↓:赤いラインが上下します\nEnter:(赤いラインがヒストグラム上にある場合)\n" "赤いラインの示す濃度値を閾値として2値化します\n(赤いラインがヒストグラム上にない場合)\n原画像に戻します"; HDC hdc; PAINTSTRUCT ps; HDROP hDrop; char szFileName[256]; switch(msg){ case WM_CREATE: DragAcceptFiles(hWnd,TRUE); break; case WM_DROPFILES: hDrop=(HDROP)wp; DragQueryFile(hDrop,0,szFileName,sizeof(szFileName)); DragFinish(hDrop); if(!mybmp.bmp_in(szFileName,hWnd)) break; lpbiInfo=&mybmp.biInfo; lpPixel=mybmp.lpPixel; iWidth=mybmp.iWidth; iHeight=mybmp.iHeight; lpPixel2=(LPDWORD)HeapAlloc(GetProcessHeap(),0,iWidth*iHeight*4); CopyMemory(lpPixel2,lpPixel,iWidth*iHeight*4); histgram(lpPixel,hist); histsmooth(hist,hist2); InvalidateRect(hWnd,NULL,TRUE); break; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); for(i=0;i<256;i++){ MoveToEx(hdc,0,300+i,NULL); LineTo(hdc,hist2[i]/2,300+i); } DrawText(hdc,info,-1,&rc,DT_WORDBREAK); if(py>=300 && py<300+256){ sprintf(text,"濃度値%d: %d個(%lf%%)", py-300,hist2[py-300],(double)hist2[py-300]/(iWidth*iHeight)*100.0); TextOut(hdc,300,230,text,(int)strlen(text)); } StretchDIBits(hdc,0,0,256,256, 0,0,iWidth,iHeight,lpPixel2,lpbiInfo, DIB_RGB_COLORS,SRCCOPY); hPen=CreatePen(PS_SOLID,1,RGB(255,0,0)); hOldPen=(HPEN)SelectObject(hdc,hPen); MoveToEx(hdc,0,py,NULL); LineTo(hdc,2048,py); SelectObject(hdc,hOldPen); DeleteObject(hPen); EndPaint(hWnd,&ps); break; case WM_KEYDOWN: switch(wp){ case VK_UP: py--; break; case VK_DOWN: py++; break; case VK_RETURN: if(py>=300 && py<300+256) threshold(lpPixel,lpPixel2,py-300,2); else CopyMemory(lpPixel2,lpPixel,iWidth*iHeight*4); break; } InvalidateRect(hWnd,NULL,TRUE); break; case WM_CLOSE: HeapFree(GetProcessHeap(),0,lpPixel2); DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wp,lp); } return 0; } void threshold(LPDWORD image_in,LPDWORD image_out,int thresh,int mode) { int x,y,n; for(y=0;y=thresh) image_out[x+y*iWidth]=HIGH; else image_out[x+y*iWidth]=LOW; } } } void histgram(LPDWORD image_in,long hist[256]) { int x,y,n; for(n=0;n<256;n++) hist[n]=0; for(y=0;y255) i=255; sum+=hist_in[i]; } hist_out[n]=(long)(sum/5.0+0.5); } }