001: //----------------------------------------------------------------- 002: // hough_proc.cpp: 003: // Hough変換用関数 (直線検出, 円検出) 004: // Last Update: <2004/12/10 09:56:39 A.Murakami> 005: //----------------------------------------------------------------- 006: #include <windows.h> 007: #include "wingui.h" 008: #include "ipcommon.h" 009: #include "binalize.h" 010: #include "hough.h" 011: //----------------------------------------------------------------- 012: extern UINT iHeight,iWidth,iLength,iSize;// 高さ,幅,1ラインの長さ,サイズ 013: extern LPBYTE lpOrgBMP,lpBMP; // オリジナル画像, 表示画像 014: //----------------------------------------------------------------- 015: #define CIR_RADIUS 125 // 円の半径 016: LPBYTE iBin; // 2値画像用 017: //----------------------------------------------------------------- 018: void toOrg(); 019: void toBin(); 020: void Extract_Line(); 021: void Extract_cir(); 022: //----------------------------------------------------------------- 023: // メニューへの追加内容 024: //----------------------------------------------------------------- 025: MenuInfo MI[] = { 026: {"元画像 ",toOrg}, 027: {"2値化 ",toBin}, 028: {"線検出 ",Extract_Line}, 029: {"円検出 ",Extract_cir}, 030: {NULL,NULL} 031: }; 032: //----------------------------------------------------------------- 033: // 元画像の表示 034: //----------------------------------------------------------------- 035: void toOrg() 036: { 037: CopyMemory(lpBMP,lpOrgBMP,iLength*iHeight); 038: } 039: //----------------------------------------------------------------- 040: // 2値画像の表示 041: //----------------------------------------------------------------- 042: void toBin() 043: { 044: int sobel_x[9] = { // ソーベルフィルタ[横] 045: -1, 0, 1, 046: -2, 0, 2, 047: -1, 0, 1 048: }; 049: int sobel_y[9] = { // ソーベルフィルタ[縦] 050: -1, -2, -1, 051: 0, 0, 0, 052: 1, -2, -1 053: }; 054: LPBYTE iTemp = GetGray(); 055: iBin = GetGray(); 056: // 空間フィルタ適用 057: spacial_filtering(iBin ,sobel_x,0.5); 058: spacial_filtering(iTemp,sobel_y,0.5); 059: // 2値化 060: GrayToBin(iBin ,BW_BKG_AUTO); 061: GrayToBin(iTemp,BW_BKG_AUTO); 062: // 縦横/画像の加算 063: IPfunc_OR(iBin,iTemp); 064: // 表示用 065: GrayToColor(iBin,lpBMP); 066: } 067: //----------------------------------------------------------------- 068: // 画像中から線の抽出 069: //----------------------------------------------------------------- 070: void Extract_Line() 071: { 072: // 2値化画像の取得 073: toBin(); 074: CopyMemory(lpBMP,lpOrgBMP,iLength*iHeight); 075: //-------------------------------------------------- 076: // Hough 変換実行/表示 077: //-------------------------------------------------- 078: // 0.25: 検出尺度[0..1] 079: // オプション: 080: // HOUGH_LINE_MC : m(傾き),c(切片) 空間への写像 081: // [傾き∞の問題に対処したもの] 082: // HOUGH_LINE_RTH: ρ(距離),θ(角度) 空間への写像 083: LHough_proc(iBin,0.25,HOUGH_LINE_MC); 084: // 後片付け 085: GlobalFree(iBin); 086: } 087: //----------------------------------------------------------------- 088: // 画像中から円の抽出 089: //----------------------------------------------------------------- 090: void Extract_cir() 091: { 092: int cir_n; POINT cir_p[100]; 093: // 2値化画像の取得 094: toBin(); 095: //-------------------------------------------------- 096: // Hough 変換実行 097: //-------------------------------------------------- 098: // cir_p: 円の検出位置 099: // CIR_RADIUS: 円の半径 100: // 100: 最大検出数 101: // 0.5: 検出尺度[0..1] 102: cir_n = CHough_proc(iBin,cir_p,CIR_RADIUS,100,0.5); 103: // 表示 104: TCHAR msg[512]; 105: wsprintf(msg,"%d circle found.",cir_n); 106: MessageBox(NULL,msg,0,0); 107: draw_hough_cir(lpBMP,cir_n,cir_p,CIR_RADIUS); 108: // 後片付け 109: GlobalFree(iBin); 110: }