#define TABLE_HEAD 80
#define TABLE_HEAD_BIG 800
/*
不用所有的点参与变换计算,可以抽出一半的点参与计算,那么速度又有很大提高!
*/
// [-80 , 81], step 0.1 . zero offset -- 800 (table head)
const static LONG g_lTanTableBig[] =
{
-743346, -735834, -728467, -721242, -714155, -707201, -700377, -693678, -687103, -680647,
-674306, -668079, -661961, -655951, -650044, -644239, -638532, -632922, -627406, -621981,
-616645, -611396, -606232, -601150, -596149, -591227, -586382, -581612, -576915, -572290,
-567735, -563248, -558828, -554473, -550182, -545954, -541786, -537678, -533629, -529637,
-525701, -521819, -517991, -514215, -510491, -506817, -503193, -499616, -496087, -492604,
-489167, -485774, -482425, -479118, -475853, -472630, -469447, -466303, -463198, -460131,
-457102, -454109, -451152, -448231, -445344, -442491, -439672, -436885, -434131, -431408,
-428717, -426056, -423425, -420823, -418251, -415707, -413191, -410702, -408241, -405806,
-403398, -401015, -398657, -396325, -394016, -391732, -389472, -387235, -385021, -382829,
-380660, -378513, -376387, -374282, -372199, -370135, -368092, -366069, -364066, -362082,
-360117, -358171, -356243, -354333, -352441, -350567, -348711, -346871, -345049, -343243,
-341454, -339681, -337923, -336182, -334456, -332745, -331050, -329369, -327703, -326051,
-324414, -322791, -321182, -319586, -318004, -316435, -314880, -313337, -311807, -310290,
-308786, -307294, -305813, -304345, -302889, -301444, -300011, -298590, -297180, -295780,
-294392, -293015, -291648, -290292, -288946, -287611, -286286, -284970, -283665, -282370,
-281084, -279808, -278542, -277284, -276036, -274798, -273568, -272347, -271135, -269932,
-268737, -267551, -266373, -265204, -264042, -262889, -261744, -260607, -259478, -258357,
-257243, -256137, -255038, -253947, -252863, -251786, -250717, -249655, -248600, -247551,
-246510, -245476, -244448, -243427, -242412, -241404, -240403, -239407, -238419, -237436,
-236460, -235489, -234525, -233567, -232615, -231668, -230728, -229793, -228864, -227941,
-227023, -226111, -225204, -224302, -223406, -222516, -221630, -220750, -219875, -219005,
-218140, -217280, -216425, -215575, -214730, -213890, -213054, -212223, -211397, -210575,
-209759, -208946, -208138, -207335, -206536, -205741, -204951, -204165, -203383, -202606,
-201833, -201064, -200299, -199538, -198781, -198028, -197279, -196534, -195793, -195055,
-194322, -193592, -192866, -192144, -191425, -190710, -189999, -189292, -188587, -187887,
-187190, -186496, -185806, -185119, -184436, -183756, -183079, -182405, -181735, -181068,
-180405, -179744, -179087, -178432, -177781, -177133, -176488, -175846, -175207, -174571,
-173938, -173308, -172680, -172056, -171435, -170816, -170200, -169587, -168977, -168369,
-167764, -167162, -166562, -165965, -165371, -164780, -164191, -163604, -163020, -162439,
-161860, -161284, -160710, -160138, -159569, -159003, -158438, -157876, -157317, -156760,
-156205, -155653, -155102, -154554, -154009, -153465, -152924, -152385, -151848, -151313,
-150781, -150250, -149722, -149196, -148672, -148149, -147629, -147111, -146596, -146082,
-145570, -145060, -144552, -144046, -143542, -143039, -142539, -142041, -141544, -141050,
-140557, -140066, -139577, -139090, -138604, -138121, -137639, -137159, -136680, -136203,
-135729, -135255, -134784, -134314, -133846, -133379, -132915, -132451, -131990, -131530,
-131071, -130615, -130160, -129706, -129254, -128804, -128355, -127907, -127461, -127017,
-126574, -126133, -125693, -125255, -124818, -124382, -123948, -123516, -123084, -122655,
-122226, -121799, -121374, -120949, -120526, -120105, -119685, -119266, -118848, -118432,
-118017, -117604, -117191, -116780, -116371, -115962, -115555, -115149, -114744, -114341,
-113939, -113538, -113138, -112739, -112342, -111946, -111551, -111157, -110764, -110372,
-109982, -109593, -109205, -108818, -108432, -108047, -107663, -107281, -106899, -106519,
-106140, -105761, -105384, -105008, -104633, -104259, -103886, -103514, -103143, -102773,
-102404, -102036, -101669, -101303, -100939, -100575, -100212, -99850, -99489, -99128,
-98769, -98411, -98054, -97698, -97342, -96988, -96634, -96281, -95930, -95579,
-95229, -94880, -94532, -94184, -93838, -93492, -93148, -92804, -92461, -92118,
-91777, -91437, -91097, -90758, -90420, -90083, -89746, -89411, -89076, -88742,
-88409, -88076, -87745, -87414, -87084, -86754, -86426, -86098, -85771, -85444,
-85119, -84794, -84470, -84146, -83824, -83502, -83180, -82860, -82540, -82221,
-81902, -81585, -81268, -80951, -80636, -80321, -80006, -79693, -79380, -79067,
-78756, -78444, -78134, -77824, -77515, -77207, -76899, -76592, -76285, -75979,
-75674, -75369, -75065, -74762, -74459, -74156, -73855, -73554, -73253, -72953,
-72654, -72355, -72057, -71759, -71462, -71166, -70870, -70575, -70280, -69985,
-69692, -69399, -69106, -68814, -68522, -68231, -67941, -67651, -67361, -67072,
-66784, -66496, -66209, -65922, -65635, -65350, -65064, -64779, -64495, -64211,
-63928, -63645, -63362, -63080, -62799, -62518, -62237, -61957, -61677, -61398,
-61119, -60841, -60563, -60286, -60009, -59732, -59456, -59181, -58906, -58631,
-58357, -58083, -57809, -57536, -57263, -56991, -56719, -56448, -56177, -55906,
-55636, -55366, -55097, -54828, -54560, -54291, -54023, -53756, -53489, -53222,
-52956, -52690, -52425, -52159, -51895, -51630, -51366, -51102, -50839, -50576,
-50313, -50051, -49789, -49527, -49266, -49005, -48745, -48485, -48225, -47965,
-47706, -47447, -47188, -46930, -46672, -46415, -46157, -45900, -45644, -45387,
-45131, -44875, -44620, -44365, -44110, -43856, -43601, -43347, -43094, -42840,
-42587, -42335, -42082, -41830, -41578, -41326, -41075, -40824, -40573, -40323,
-40072, -39822, -39572, -39323, -39074, -38825, -38576, -38328, -38079, -37831,
-37584, -37336, -37089, -36842, -36595, -36349, -36103, -35857, -35611, -35365,
-35120, -34875, -34630, -34386, -34141, -33897, -33653, -33409, -33166, -32923,
-32679, -32437, -32194, -31951, -31709, -31467, -31225, -30984, -30742, -30501,
-30260, -30019, -29778, -29538, -29298, -29057, -28818, -28578, -28338, -28099,
-27860, -27621, -27382, -27143, -26905, -26666, -26428, -26190, -25952, -25715,
-25477, -25240, -25003, -24766, -24529, -24292, -24056, -23819, -23583, -23347,
-23111, -22875, -22640, -22404, -22169, -21933, -21698, -21463, -21229, -20994,
-20759, -20525, -20291, -20056, -19822, -19588, -19355, -19121, -18887, -18654,
-18420, -18187, -17954, -17721, -17488, -17255, -17023, -16790, -16558, -16325,
-16093, -15861, -15629, -15397, -15165, -14933, -14702, -14470, -14238, -14007,
-13776, -13544, -13313, -13082, -12851, -12620, -12389, -12159, -11928, -11697,
-11467, -11236, -11006, -10776, -10545, -10315, -10085, -9855, -9625, -9395,
-9165, -8935, -8705, -8476, -8246, -8016, -7787, -7557, -7328, -7098,
-6869, -6639, -6410, -6181, -5951, -5722, -5493, -5264, -5035, -4806,
-4577, -4348, -4119, -3890, -3661, -3432, -3203, -2974, -2745, -2516,
-2287, -2059, -1830, -1601, -1372, -1143, -915, -686, -457, -228,
0, 228, 457, 686, 915, 1143, 1372, 1601, 1830, 2059,
2287, 2516, 2745, 2974, 3203, 3432, 3661, 3890, 4119, 4348,
4577, 4806, 5035, 5264, 5493, 5722, 5951, 6181, 6410, 6639,
6869, 7098, 7328, 7557, 7787, 8016, 8246, 8476, 8705, 8935,
9165, 9395, 9625, 9855, 10085, 10315, 10545, 10776, 11006, 11236,
11467, 11697, 11928, 12159, 12389, 12620, 12851, 13082, 13313, 13544,
13776, 14007, 14238, 14470, 14702, 14933, 15165, 15397, 15629, 15861,
16093, 16325, 16558, 16790, 17023, 17255, 17488, 17721, 17954, 18187,
18420, 18654, 18887, 19121, 19355, 19588, 19822, 20056, 20291, 20525,
20759, 20994, 21229, 21463, 21698, 21933, 22169, 22404, 22640, 22875,
23111, 23347, 23583, 23819, 24056, 24292, 24529, 24766, 25003, 25240,
25477, 25715, 25952, 26190, 26428, 26666, 26905, 27143, 27382, 27621,
27860, 28099, 28338, 28578, 28818, 29057, 29298, 29538, 29778, 30019,
30260, 30501, 30742, 30984, 31225, 31467, 31709, 31951, 32194, 32437,
32679, 32923, 33166, 33409, 33653, 33897, 34141, 34386, 34630, 34875,
35120, 35365, 35611, 35857, 36103, 36349, 36595, 36842, 37089, 37336,
37584, 37831, 38079, 38328, 38576, 38825, 39074, 39323, 39572, 39822,
40072, 40323, 40573, 40824, 41075, 41326, 41578, 41830, 42082, 42335,
42587, 42840, 43094, 43347, 43601, 43856, 44110, 44365, 44620, 44875,
45131, 45387, 45644, 45900, 46157, 46415, 46672, 46930, 47188, 47447,
47706, 47965, 48225, 48485, 48745, 49005, 49266, 49527, 49789, 50051,
50313, 50576, 50839, 51102, 51366, 51630, 51895, 52159, 52425, 52690,
52956, 53222, 53489, 53756, 54023, 54291, 54560, 54828, 55097, 55366,
55636, 55906, 56177, 56448, 56719, 56991, 57263, 57536, 57809, 58083,
58357, 58631, 58906, 59181, 59456, 59732, 60009, 60286, 60563, 60841,
61119, 61398, 61677, 61957, 62237, 62518, 62799, 63080, 63362, 63645,
63928, 64211, 64495, 64779, 65064, 65350, 65635, 65922, 66209, 66496,
66784, 67072, 67361, 67651, 67941, 68231, 68522, 68814, 69106, 69399,
69692, 69985, 70280, 70575, 70870, 71166, 71462, 71759, 72057, 72355,
72654, 72953, 73253, 73554, 73855, 74156, 74459, 74762, 75065, 75369,
75674, 75979, 76285, 76592, 76899, 77207, 77515, 77824, 78134, 78444,
78756, 79067, 79380, 79693, 80006, 80321, 80636, 80951, 81268, 81585,
81902, 82221, 82540, 82860, 83180, 83502, 83824, 84146, 84470, 84794,
85119, 85444, 85771, 86098, 86426, 86754, 87084, 87414, 87745, 88076,
88409, 88742, 89076, 89411, 89746, 90083, 90420, 90758, 91097, 91437,
91777, 92118, 92461, 92804, 93148, 93492, 93838, 94184, 94532, 94880,
95229, 95579, 95930, 96281, 96634, 96988, 97342, 97698, 98054, 98411,
98769, 99128, 99489, 99850, 100212, 100575, 100939, 101303, 101669, 102036,
102404, 102773, 103143, 103514, 103886, 104259, 104633, 105008, 105384, 105761,
106140, 106519, 106899, 107281, 107663, 108047, 108432, 108818, 109205, 109593,
109982, 110372, 110764, 111157, 111551, 111946, 112342, 112739, 113138, 113538,
113939, 114341, 114744, 115149, 115555, 115962, 116371, 116780, 117191, 117604,
118017, 118432, 118848, 119266, 119685, 120105, 120526, 120949, 121374, 121799,
122226, 122655, 123084, 123516, 123948, 124382, 124818, 125255, 125693, 126133,
126574, 127017, 127461, 127907, 128355, 128804, 129254, 129706, 130160, 130615,
131071, 131530, 131990, 132451, 132915, 133379, 133846, 134314, 134784, 135255,
135729, 136203, 136680, 137159, 137639, 138121, 138604, 139090, 139577, 140066,
140557, 141050, 141544, 142041, 142539, 143039, 143542, 144046, 144552, 145060,
145570, 146082, 146596, 147111, 147629, 148149, 148672, 149196, 149722, 150250,
150781, 151313, 151848, 152385, 152924, 153465, 154009, 154554, 155102, 155653,
156205, 156760, 157317, 157876, 158438, 159003, 159569, 160138, 160710, 161284,
161860, 162439, 163020, 163604, 164191, 164780, 165371, 165965, 166562, 167162,
167764, 168369, 168977, 169587, 170200, 170816, 171435, 172056, 172680, 173308,
173938, 174571, 175207, 175846, 176488, 177133, 177781, 178432, 179087, 179744,
180405, 181068, 181735, 182405, 183079, 183756, 184436, 185119, 185806, 186496,
187190, 187887, 188587, 189292, 189999, 190710, 191425, 192144, 192866, 193592,
194322, 195055, 195793, 196534, 197279, 198028, 198781, 199538, 200299, 201064,
201833, 202606, 203383, 204165, 204951, 205741, 206536, 207335, 208138, 208946,
209759, 210575, 211397, 212223, 213054, 213890, 214730, 215575, 216425, 217280,
218140, 219005, 219875, 220750, 221630, 222516, 223406, 224302, 225204, 226111,
227023, 227941, 228864, 229793, 230728, 231668, 232615, 233567, 234525, 235489,
236460, 237436, 238419, 239407, 240403, 241404, 242412, 243427, 244448, 245476,
246510, 247551, 248600, 249655, 250717, 251786, 252863, 253947, 255038, 256137,
257243, 258357, 259478, 260607, 261744, 262889, 264042, 265204, 266373, 267551,
268737, 269932, 271135, 272347, 273568, 274798, 276036, 277284, 278542, 279808,
281084, 282370, 283665, 284970, 286286, 287611, 288946, 290292, 291648, 293015,
294392, 295780, 297180, 298590, 300011, 301444, 302889, 304345, 305813, 307294,
308786, 310290, 311807, 313337, 314880, 316435, 318004, 319586, 321182, 322791,
324414, 326051, 327703, 329369, 331050, 332745, 334456, 336182, 337923, 339681,
341454, 343243, 345049, 346871, 348711, 350567, 352441, 354333, 356243, 358171,
360117, 362082, 364066, 366069, 368092, 370135, 372199, 374282, 376387, 378513,
380660, 382829, 385021, 387235, 389472, 391732, 394016, 396325, 398657, 401015,
403398, 405806, 408241, 410702, 413191, 415707, 418251, 420823, 423425, 426056,
428717, 431408, 434131, 436885, 439672, 442491, 445344, 448231, 451152, 454109,
457102, 460131, 463198, 466303, 469447, 472630, 475853, 479118, 482425, 485774,
489167, 492604, 496087, 499616, 503193, 506817, 510491, 514215, 517991, 521819,
525701, 529637, 533629, 537678, 541786, 545954, 550182, 554473, 558828, 563248,
567735, 572290, 576915, 581612, 586382, 591227, 596149, 601150, 606232, 611396,
616645, 621981, 627406, 632922, 638532, 644239, 650044, 655951, 661961, 668079,
674306, 680647, 687103, 693678, 700377, 707201, 714155, 721242, 728467, 735834,
743346, 751008, 758825, 766802, 774944, 783255, 791741, 800408, 809262, 818309,
827556,
};
/*
HOUGH TRANSFORM GET LINE
x * cos(theta) + y * sin(theta) = r
theta: -Pi/2 ~ Pi/2
for any point (x,y) on line, r and theta is constant.
直线方程:y = k*x+b
可以改进的地方:参数b可以更加精确,比如精确到0.1。
*/
static LONG getKB( LONG* plCounter, LONG lStepsOfB, LONG lStepsOfAngle, LONG* plAngleRet, LONG* plB )
{
LONG j, lMax, lPos;
lPos = 0;
lMax = 0;
for( j=0; j<lStepsOfAngle*lStepsOfB; j++ )
{
if( plCounter[j] > lMax )
{
lMax = plCounter[j];
lPos = j;
}
}
if( lMax < 3 )
return 1;
*plAngleRet = lPos/lStepsOfB;
*plB = lPos%lStepsOfB;
return 0;
}
/*
plParams:
left, right, top, bottom
Min angle: 角度*10
Max angle: 角度*10
angle step: 角度*10
lPointNo: 参与变换的点数
psPoints: 点 (SHORT*)
*/
// b = x - k * y
LONG get_vertical_line( LONG* m_plCounter, LONG* plParams, LONG* plAngle, LONG* plB )
{
LONG j, k, lOffset;
LONG lMinB, lMaxB, lStepsOfB, lStepsOfAngle, lTemp;
LONG *plTemp;
LONG lReturn, lAngle, lB;
LONG lMinAngle, lMaxAngle;
SHORT *pEdge;
// angle time 10
lMinAngle = plParams[4];
lMaxAngle = plParams[5];
// b = x - k * y
if( lMinAngle>0 )
{
// left - tan(max_theta) * bottom;
lMinB = plParams[0] - ((g_lTanTableBig[lMaxAngle+TABLE_HEAD_BIG]*plParams[3])>>17) -2;
// right - tan(min_theta) * top;
lMaxB = plParams[1] - ((g_lTanTableBig[lMinAngle+TABLE_HEAD_BIG]*plParams[2])>>17) +2;
}
else if( lMaxAngle<0 )
{
// left - tan(max_theta) * top
lMinB = plParams[0] - ((g_lTanTableBig[lMaxAngle+TABLE_HEAD_BIG]*plParams[2])>>17) -2;
// right - tan(min_theta) * bottom
lMaxB = plParams[1] - ((g_lTanTableBig[lMinAngle+TABLE_HEAD_BIG]*plParams[3])>>17) +2;
}
else
{
// left - tan(max_theta) * bottom;
lMinB = plParams[0] - ((g_lTanTableBig[lMaxAngle+TABLE_HEAD_BIG]*plParams[3])>>17) -2;
// right - tan(min_theta) * bottom
lMaxB = plParams[1] - ((g_lTanTableBig[lMinAngle+TABLE_HEAD_BIG]*plParams[3])>>17) +2;
}
lStepsOfB = lMaxB-lMinB;
lStepsOfAngle = (lMaxAngle-lMinAngle+plParams[6]/2)/plParams[6];
pEdge = (SHORT*)(plParams[8]);
memset( m_plCounter, 0, sizeof(LONG)*lStepsOfAngle*lStepsOfB );
for( j=0; j<plParams[7]; j++ )//j++ )
{
plTemp = m_plCounter;
lOffset = lMinAngle+TABLE_HEAD_BIG;
for( k=0; k<lStepsOfAngle; k++ )
{
// b = x - k * y
lTemp = pEdge[j*2]-((g_lTanTableBig[lOffset]*pEdge[j*2+1])>>17);
// plTemp[lTemp-lMinB-2] += 1;
plTemp[lTemp-lMinB-1] += 1;
plTemp[lTemp-lMinB] += 1;
plTemp[lTemp-lMinB+1] += 1;
// plTemp[lTemp-lMinB+2] += 1;
plTemp += lStepsOfB;
lOffset += plParams[6];
}
}
lReturn = getKB( m_plCounter, lStepsOfB, lStepsOfAngle, &lAngle, &lB );
if( 0 != lReturn )
{
return -1;
}
else
{
*plAngle = lMinAngle+lAngle*plParams[6];
*plB = lMinB+lB;
return 0;
}
}
/*
plParams:
left, right, top, bottom
Min angle: 角度*10
Max angle: 角度*10
angle step: 角度*10
lPointNo: 参与变换的点数
psPoints: 点 (SHORT*)
*/
// b = y - k * x
LONG get_horizontal_line( LONG* m_plCounter, LONG* plParams, LONG* plAngle, LONG* plB )
{
LONG j, k, lOffset;
LONG lMinB, lMaxB, lStepsOfB, lStepsOfAngle, lTemp;
LONG *plTemp;
LONG lReturn, lAngle, lB;
LONG lMinAngle, lMaxAngle;
SHORT *pEdge;
// angle time 10
lMinAngle = plParams[4];
lMaxAngle = plParams[5];
// b = y - k * x
if( lMinAngle>0 )
{
// top - tan(max_theta) * right;
lMinB = plParams[2] - ((g_lTanTableBig[lMaxAngle+TABLE_HEAD_BIG]*plParams[1])>>17) -2;
// bottom - tan(min_theta) * left;
lMaxB = plParams[3] - ((g_lTanTableBig[lMinAngle+TABLE_HEAD_BIG]*plParams[0])>>17) +2;
}
else if( lMaxAngle<0 )
{
// top - tan(max_theta) * left
lMinB = plParams[2] - ((g_lTanTableBig[lMaxAngle+TABLE_HEAD_BIG]*plParams[0])>>17) -2;
// bottom - tan(min_theta) * right
lMaxB = plParams[3] - ((g_lTanTableBig[lMinAngle+TABLE_HEAD_BIG]*plParams[1])>>17) +2;
}
else
{
// top - tan(max_theta) * right;
lMinB = plParams[2] - ((g_lTanTableBig[lMaxAngle+TABLE_HEAD_BIG]*plParams[1])>>17) -2;
// bottom - tan(min_theta) * right
lMaxB = plParams[3] - ((g_lTanTableBig[lMinAngle+TABLE_HEAD_BIG]*plParams[1])>>17) +2;
}
lStepsOfB = lMaxB-lMinB;
lStepsOfAngle = (lMaxAngle-lMinAngle+plParams[6]/2)/plParams[6];
pEdge = (SHORT*)(plParams[8]);
memset( m_plCounter, 0, sizeof(LONG)*lStepsOfAngle*lStepsOfB );
for( j=0; j<plParams[7]; j++ )
{
plTemp = m_plCounter;
lOffset = lMinAngle+TABLE_HEAD_BIG;
for( k=0; k<lStepsOfAngle; k++ )
{
// b = y - k * x
lTemp = pEdge[j*2+1]-((g_lTanTableBig[lOffset]*pEdge[j*2])>>17);
// plTemp[lTemp-lMinB-2] += 1;
plTemp[lTemp-lMinB-1] += 1;
plTemp[lTemp-lMinB] += 1;
plTemp[lTemp-lMinB+1] += 1;
// plTemp[lTemp-lMinB+2] += 1;
plTemp += lStepsOfB;
lOffset += plParams[6];
}
}
lReturn = getKB( m_plCounter, lStepsOfB, lStepsOfAngle, &lAngle, &lB );
if( 0 != lReturn )
{
return -1;
}
else
{
*plAngle = lMinAngle+lAngle*plParams[6];
*plB = lMinB+lB;
return 0;
}
}
Hough
优化霍夫变换加速直线检测
最新推荐文章于 2025-12-06 00:42:07 发布
1164






