File: bmGrayHisto.c

package info (click to toggle)
ted 2.16-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,944 kB
  • ctags: 20,273
  • sloc: ansic: 167,980; makefile: 12,518; sh: 263
file content (665 lines) | stat: -rw-r--r-- 13,031 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#   include	"bitmapConfig.h"

#   include	<math.h>

#   include	<bmGrayHisto.h>

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Various gray scale histogram base Thresholding algorithms.		*/
/*									*/
/*  Most of the approaches and some of the implementation is derived	*/
/*  from the following book:						*/
/*									*/
/*  PARKER, J.R.: Algorithms for Image Processing and Computer Vision,	*/
/*	Wiley Computer Publishing, New York, 1996, ISBN 0-471-14056-2	*/
/*									*/
/************************************************************************/

void bmInitThresholderHistogram(	ThresholderHistogram *	th )
    {
    int		i;

    for ( i= 0; i < 256; i++ )
	{ th->thHistogram[i]= 0; }

    th->thHistogramSize= 0;
    th->thPixelCount= 0;
    th->thThreshold= -1;
    }

/************************************************************************/
/*									*/
/*  Determine the threshold using the Mean pixel value.			*/
/*									*/
/************************************************************************/

void bmThresholdMean(	ThresholderHistogram *		th )
    {
    int		i;

    double	sr;

    sr= 0;
    for ( i= 0; i < th->thHistogramSize; i++ )
	{ sr += th->thHistogram[i]* i;	}

    th->thThreshold= sr/ th->thPixelCount;

    return;
    }

/************************************************************************/
/*									*/
/*  Determine the threshold using a fraction.				*/
/*									*/
/************************************************************************/

void bmThresholdQuantile(	ThresholderHistogram *		th,
				double				frac )
    {
    int		i;

    double	sr;

    sr= 0;
    for ( i= 0; i < th->thHistogramSize; i++ )
	{
	if  ( ( sr + th->thHistogram[i] )/ th->thPixelCount > frac )
	    { break;	}

	sr += th->thHistogram[i];
	}

    th->thThreshold= i;

    return;
    }

/************************************************************************/
/*									*/
/*  Determine the threshold using the two peak method.			*/
/*									*/
/*  1)  Determine the primary peak.					*/
/*  2)  Determine the second peak.					*/
/*  3)  Assume lowest value is background.				*/
/*  4)  Guess the threshold value is exactly between the peaks.		*/
/*  5)  But look for a valley between the two peaks.			*/
/*									*/
/************************************************************************/

void bmThreshold2Peaks(	ThresholderHistogram *		th )
    {
    int		i0;
    int		i1;

    int		p1= 0;
    int		p2= 1;

    long	v;
    long	v1= 0;
    long	v2= 0;

    int		i;
    int		p;

    /*  1  */
    i0= i1= 0;
    for ( i= 0; i < th->thHistogramSize; i++ )
	{
	v= th->thHistogram[i];

	if  ( v1 <  v )
	    { v1=   v; i0= i1= i; }
	if  ( v1 == v )
	    { i1= i; }
	}

    p1= ( i0+ i1 )/ 2;

    /*  2  */
    for ( i= 0; i < th->thHistogramSize; i++ )
	{
	v= ( i- p1 );
	v= v* v* th->thHistogram[i];

	if  ( v2 <  v )
	    { v2=   v; i0= i1= i; }
	if  ( v2 == v )
	    { i1= i; }
	}

    p2= ( i0+ i1 )/ 2;

    /*  3  */
    if  ( p1 > p2 )
	{ i= p1; p1= p2; p2= i; }

    /*  4  */
    th->thThreshold= ( p1+ p2 )/ 2;

    /*  5  */
    i0= i1= th->thThreshold;
    v1= th->thHistogram[th->thThreshold];

    i0= i1= p1;
    v1= th->thHistogram[p1];
    p= ( p1+ p2 )/ 2;

    for ( i= p1; i <= p2; i++ )
	{
	v= ( i- p1 )* ( p2- i );
	v= v* ( p- th->thHistogram[i] );

	if  ( v1 <  v )
	    { v1=   v; i0= i1= i; }
	if  ( v1 == v )
	    { i1= i; }
	}

    th->thThreshold= ( i0+ i1 )/ 2;

    return;
    }

/************************************************************************/
/*									*/
/*  Determine the threshold using the Ridler Iterative Selection	*/
/*  method.								*/
/*									*/
/*  1)  First guess: the average pixel value;				*/
/*  2)  Determine the mean gray level of pixels not over the threshold	*/
/*  3)  Determine the mean gray level of pixels over the threshold	*/
/*  4)  A new estimate of the threshold is the average of the means	*/
/*	calculated above.						*/
/*  5)  Iteration stops if the new threshold is equal to the previous	*/
/*	one.								*/
/*									*/
/************************************************************************/

void bmThresholdRidler(	ThresholderHistogram *		th )
    {
    int		i;

    double	sr;
    int		tr;

    /*  1  */
    sr= 0;
    for ( i= 0; i < th->thHistogramSize; i++ )
	{ sr += th->thHistogram[i]* i;	}
    tr= sr/ th->thPixelCount;

    for (;;)
	{
	int	no= 0;
	int	nb= 0;
	double	so= 0;
	double	sb= 0;

	int	tn;

	/*  2  */
	for ( i= 0; i <= tr; i++ )
	    {
	    nb += th->thHistogram[i];
	    sb += th->thHistogram[i]* i;
	    }

	/*  3  */
	for ( i= tr+ 1; i < th->thHistogramSize; i++ )
	    {
	    no += th->thHistogram[i];
	    so += th->thHistogram[i]* i;
	    }

	if  ( no == 0 )
	    { no=   1;	}
	if  ( nb == 0 )
	    { nb=   1;	}

	/*  4  */
	tn= ( so/no+ sb/nb )/ 2.0;

	/*  5  */
	if  ( tn == tr )
	    {
	    th->thThreshold= tr;
	    return;
	    }
	
	tr= tn;
	}
    }

/************************************************************************/
/*									*/
/*  Determine the threshold minimizing the ration of between- class and	*/
/*  overall variance.							*/
/*									*/
/*  1)  Overall mean and variance.					*/
/*  2)  Below threshold.						*/
/*  3)  Above threshold.						*/
/*									*/
/************************************************************************/

void bmThresholdVariance(	ThresholderHistogram *		th )
    {
    int		i;

    double	sum_b;
    double	ssq_b;
    double	ub;
    double	vb;
    int		nb;

    double	sum_o;
    double	ssq_o;
    double	uo;
    double	vo;
    int		no;

    int		tr;
    double	vmin;

    int		tr0;
    int		tr1;

#   if 0

    double	sum_t;
    double	ssq_t;
    double	ut;
    double	vt;

    /*  1  */
    sum_t= 0; ssq_t= 0;
    for ( i= 0; i < th->thHistogramSize; i++ )
	{
	sum_t += (double)th->thHistogram[i]* i;
	ssq_t += (double)th->thHistogram[i]* i* i;
	}
    ut= sum_t/ th->thPixelCount;
    vt= ssq_t/ th->thPixelCount- ut* ut;

#   endif

    tr= 0;

    /*  2  */
    vb= 0; ub= 0;
    sum_b= 0; ssq_b= 0; nb= 0;
    for ( i= 0; i <= tr; i++ )
	{
	nb    += th->thHistogram[i];
	sum_b += (double)th->thHistogram[i]* i;
	ssq_b += (double)th->thHistogram[i]* i* i;
	}
    if  ( nb > 0 )
	{
	ub= sum_b/ nb;
	vb= ssq_b/ nb- ub* ub;
	}

    /*  3  */
    vo= 0; uo= 0;
    sum_o= 0; ssq_o= 0; no= 0;
    for ( i= tr+ 1; i < th->thHistogramSize; i++ )
	{
	no    += th->thHistogram[i];
	sum_o += (double)th->thHistogram[i]* i;
	ssq_o += (double)th->thHistogram[i]* i* i;
	}
    if  ( no > 0 )
	{
	uo= sum_o/ no;
	vo= ssq_o/ no- uo* uo;
	}

    vmin= vo+ vb;
    tr0= tr1= tr;

    for ( tr= 1; tr < th->thHistogramSize; tr++ )
	{
	/*  2  */
	vb= 0; ub= 0;
	nb    += th->thHistogram[tr];
	sum_b += (double)th->thHistogram[tr]* tr;
	ssq_b += (double)th->thHistogram[tr]* tr* tr;

	if  ( nb > 0 )
	    {
	    ub= sum_b/ nb;
	    vb= ssq_b/ nb- ub* ub;
	    }

	/*  3  */
	vo= 0; uo= 0;
	no    -= th->thHistogram[tr];
	sum_o -= (double)th->thHistogram[tr]* tr;
	ssq_o -= (double)th->thHistogram[tr]* tr* tr;

	if  ( no > 0 )
	    {
	    uo= sum_o/ no;
	    vo= ssq_o/ no- uo* uo;
	    }

	if  ( vb+ vo <  vmin )
	    { tr0= tr1= tr; vmin= vb+ vo;	}
	if  ( vb+ vo == vmin )
	    { tr1= tr;				}
	}

    th->thThreshold= ( tr0+ tr1 )/ 2;

    return;
    }

/************************************************************************/
/*									*/
/*  Determine the threshold minimizing the error function.		*/
/*									*/
/*  1)  Overall mean and variance.					*/
/*  2)  Below threshold.						*/
/*  3)  Above threshold.						*/
/*									*/
/************************************************************************/

void bmThresholdMinimumError(	ThresholderHistogram *		th )
    {
    int		i;

    double	sum_b;
    double	ssq_b;
    double	ub;
    double	vb;
    double	sb;
    int		nb;

    double	sum_o;
    double	ssq_o;
    double	uo;
    double	vo;
    double	so;
    int		no;

    int		tr;
    double	j;
    double	jmin= 1e+20;

    int		tr0;
    int		tr1;

    int		s;
    int		p;

    s= 0; while( th->thHistogram[s] == 0 )
	{ s++; }
    p= th->thHistogramSize; while( th->thHistogram[p-1] == 0 )
	{ p--; }

#   if 0

    double	sum_t;
    double	ssq_t;
    double	ut;
    double	vt;

    /*  1  */
    sum_t= 0; ssq_t= 0;
    for ( i= 0; i < th->thHistogramSize; i++ )
	{
	sum_t += (double)th->thHistogram[i]* i;
	ssq_t += (double)th->thHistogram[i]* i* i;
	}
    ut= sum_t/ th->thPixelCount;
    vt= ssq_t/ th->thPixelCount- ut* ut;

#   endif

    tr= s+ 1;

    /*  2  */
    sb= 0.0; vb= 0; ub= 0;
    sum_b= 0; ssq_b= 0; nb= 0;
    for ( i= s; i <= tr; i++ )
	{
	nb    += th->thHistogram[i];
	sum_b += (double)th->thHistogram[i]* i;
	ssq_b += (double)th->thHistogram[i]* i* i;
	}

    /*  3  */
    so= 0.0; vo= 0; uo= 0;
    sum_o= 0; ssq_o= 0; no= 0;
    for ( i= tr+ 1; i < p; i++ )
	{
	no    += th->thHistogram[i];
	sum_o += (double)th->thHistogram[i]* i;
	ssq_o += (double)th->thHistogram[i]* i* i;
	}

    jmin= 1.0e+20;
    tr0= tr1= tr;

    for ( tr= tr+ 1; tr < p- 1; tr++ )
	{
	/*  2  */
	sb= 0.0; vb= 0; ub= 0;
	nb    += th->thHistogram[tr];
	sum_b += (double)th->thHistogram[tr]* tr;
	ssq_b += (double)th->thHistogram[tr]* tr* tr;

	if  ( nb > 0 )
	    {
	    ub= sum_b/ nb;
	    vb= ssq_b/ nb- ub* ub;
	    sb= sqrt( vb );
	    }

	/*  3  */
	so= 0.0; vo= 0; uo= 0;
	no    -= th->thHistogram[tr];
	sum_o -= (double)th->thHistogram[tr]* tr;
	ssq_o -= (double)th->thHistogram[tr]* tr* tr;

	if  ( no > 0 )
	    {
	    uo= sum_o/ no;
	    vo= ssq_o/ no- uo* uo;
	    so= sqrt( vo );
	    }

	if  ( no == 0 || nb == 0 || vb == 0 || vo == 0 )
	    { continue; }

	j= 1.0+
	    2* ( nb* log( sb     )+ no* log( so     ) )-
	    2* ( nb* log( 1.0*nb )+ no* log( 1.0*no ) );

	if  ( j <  jmin )
	    { tr0= tr1= tr; jmin= j;	}
	if  ( j == jmin )
	    { tr1= tr;				}
	}

    th->thThreshold= ( tr0+ tr1 )/ 2;

    return;
    }


/************************************************************************/
/*									*/
/*  Determine the threshold using the Kapur maximal entropy method.	*/
/*									*/
/************************************************************************/

void bmThresholdKapur(	ThresholderHistogram *		th )
    {
    int		i;

    double	p;
    double	Pt[256];

    double	Ht;

    int		tr;
    int		tr0;
    int		tr1;
    double	N= th->thPixelCount;

    double	Hmax;

    double	Hb;
    double	Ho;

    p= 0; Ht= 0;
    for ( i= 0; i < th->thHistogramSize; i++ )
	{
	p += (double)th->thHistogram[i];
	Pt[i]= p/ N;

#   	if 0
	if  ( th->thHistogram[i] > 0 && Pt[i] > 0 )
	    {
	    double	r= th->thHistogram[i]/ ( N* Pt[i] );

	    Ht -= r* log( r );
	    }
#	endif
	}

    tr= 0;

    Hb= 0;
    for ( i= 0; i <= tr; i++ )
	{
	if  ( th->thHistogram[i] > 0 && Pt[i] > 0 )
	    {
	    double	r= th->thHistogram[i]/ ( N* Pt[i] );

	    Hb -= r* log( r );
	    }
	}

    Ho= 0;
    for ( i= tr+ 1; i < th->thHistogramSize; i++ )
	{
	if  ( th->thHistogram[i] > 0 && Pt[i] < 1 )
	    {
	    double	r= th->thHistogram[i]/ ( N* ( 1- Pt[i] ) );

	    Ho -= r* log( r );
	    }
	}

    Hmax= Hb+ Ho;
    tr0= tr1= tr;

    for ( tr= 1; tr < th->thHistogramSize; tr++ )
	{
	if  ( th->thHistogram[tr] > 0 && Pt[tr] > 0 )
	    {
	    double	r= th->thHistogram[tr]/ ( N* Pt[tr] );

	    Hb -= r* log( r );
	    }

	if  ( th->thHistogram[tr] > 0 && Pt[tr] < 1 )
	    {
	    double	r= th->thHistogram[tr]/ ( N* ( 1- Pt[tr] ) );

	    Ho += r* log( r );
	    }

	if  ( Hb+ Ho >  Hmax )
	    { tr0= tr1= tr; Hmax= Hb+ Ho;	}
	if  ( Hb+ Ho == Hmax )
	    { tr1= tr;				}
	}

    th->thThreshold= ( tr0+ tr1 )/ 2;

    return;
    }

/************************************************************************/
/*									*/
/*  Determine the threshold using the Johannsen minimal entropy method.	*/
/*									*/
/************************************************************************/

void bmThresholdJohannsen(	ThresholderHistogram *		th )
    {
    double	Pt[256];
    double	Pq[256];

    int		tr;
    double	N= th->thPixelCount;

    int		e;
    int		s;

    double	Smin= N;

    {
    int		i;
    double	p;

    p= 0;
    Pt[0]= (double)th->thHistogram[0]/N;
    Pq[0]= 1- Pt[0];
    for ( i= 1; i < th->thHistogramSize; i++ )
	{
	p += (double)th->thHistogram[i];
	Pt[i]= p/ N;
	Pq[i]= 1.0- Pt[i-1];
	}
    }

    s= 0; e= 255;
    while( th->thHistogram[s] == 0 )
	{ s++; }
    while( th->thHistogram[e] == 0 )
	{ e--; }

    s++; e--;

    for ( tr= s; tr <= e; tr++ )
	{
	double		Sb;
	double		So;
	double		p;
	double		Ep;

	double		Epr= 0;
	double		Enx= 0;

	if  ( th->thHistogram[tr] == 0 )
	    { continue;	}

	p= (double)th->thHistogram[tr]/ N;
	Ep= -p* log( p );

	if  ( Pt[tr-1] > 0.0 )
	    { Epr= -Pt[tr-1]* log( Pt[tr-1] ); }
	Sb= log( Pt[tr] )+ ( 1.0/ Pt[tr] )* ( Ep+  Epr );

	if  ( Pq[tr+1] > 0.0 )
	    { Enx= -Pq[tr+1]* log( Pq[tr+1] ); }
	So= log( Pq[tr] )+ ( 1.0/ Pq[tr] )* ( Ep+  Enx );

	if  ( Sb+ So < Smin )
	    {
	    th->thThreshold= tr;
	    Smin= Sb+ So;
	    }
	}

    return;
    }