4
4
5
5
#ifndef THRESHOLD_H_
6
6
#define THRESHOLD_H_
7
- #define THRESHOLD_VERSION "3.0 "
7
+ #define THRESHOLD_VERSION "3.1 "
8
8
9
9
#include <stdbool.h>
10
10
#include <math.h>
@@ -23,6 +23,7 @@ THRESHOLDAPI int ImageThreshold(unsigned char* buf, bool* bufmask, unsigned int
23
23
THRESHOLDAPI int ImageThresholdBimod (unsigned char * buf , bool * bufmask , unsigned int width , unsigned int height , unsigned int channels , float part , float delta );
24
24
THRESHOLDAPI int ImageThresholdSauvola (unsigned char * buf , bool * bufmask , unsigned int width , unsigned int height , unsigned int channels , int radius , float sensitivity , float part , int lower_bound , int upper_bound , float delta );
25
25
THRESHOLDAPI int ImageThresholdBlur (unsigned char * buf , bool * bufmask , unsigned int width , unsigned int height , unsigned int channels , float raduis , float part , float delta , float sensitivity );
26
+ THRESHOLDAPI int ImageThresholdEdgePlus (unsigned char * buf , bool * bufmask , unsigned int width , unsigned int height , unsigned int channels , float raduis , float part , float delta , float sensitivity );
26
27
27
28
#ifdef __cplusplus
28
29
}
@@ -150,7 +151,7 @@ static void GaussLineMatrix (float *cmatrix, float radius)
150
151
t -= 0.5f ;
151
152
t += step * j ;
152
153
tt = - (t * t ) / (2.0f * std_dev );
153
- sum += exp (tt );
154
+ sum += ( float )( exp (tt ) );
154
155
}
155
156
cmatrix [i ] = sum * step ;
156
157
}
@@ -170,7 +171,7 @@ static void GaussLineMatrix (float *cmatrix, float radius)
170
171
}
171
172
}
172
173
173
- static float GaussBlurFilterY (unsigned char * src , int height , int width , int channels , float radius )
174
+ static float GaussBlurFilterY (unsigned char * src , unsigned int height , unsigned int width , unsigned int channels , float radius )
174
175
{
175
176
int iradius , y , x , yp , yn , i , dval ;
176
177
unsigned long int k , kp , kn , line = width * channels ;
@@ -199,9 +200,9 @@ static float GaussBlurFilterY (unsigned char *src, int height, int width, int ch
199
200
if (iradius > 1 )
200
201
{
201
202
k = 0 ;
202
- for (y = 0 ; y < height ; y ++ )
203
+ for (y = 0 ; y < ( int ) height ; y ++ )
203
204
{
204
- for (x = 0 ; x < width ; x ++ )
205
+ for (x = 0 ; x < ( int ) width ; x ++ )
205
206
{
206
207
for (d = 0 ; d < channels ; d ++ )
207
208
{
@@ -221,7 +222,7 @@ static float GaussBlurFilterY (unsigned char *src, int height, int width, int ch
221
222
kp -= line ;
222
223
}
223
224
yn = y + i ;
224
- if (yn < height )
225
+ if (yn < ( int ) height )
225
226
{
226
227
kn += line ;
227
228
}
@@ -242,9 +243,9 @@ static float GaussBlurFilterY (unsigned char *src, int height, int width, int ch
242
243
}
243
244
}
244
245
k = 0 ;
245
- for (y = 0 ; y < height ; y ++ )
246
+ for (y = 0 ; y < ( int ) height ; y ++ )
246
247
{
247
- for (x = 0 ; x < width ; x ++ )
248
+ for (x = 0 ; x < ( int ) width ; x ++ )
248
249
{
249
250
for (d = 0 ; d < channels ; d ++ )
250
251
{
@@ -264,7 +265,7 @@ static float GaussBlurFilterY (unsigned char *src, int height, int width, int ch
264
265
return gaussval ;
265
266
}
266
267
267
- static float GaussBlurFilterX (unsigned char * src , int height , int width , int channels , float radius )
268
+ static float GaussBlurFilterX (unsigned char * src , unsigned int height , unsigned int width , unsigned int channels , float radius )
268
269
{
269
270
int iradius , y , x , xp , xn , i , dval ;
270
271
unsigned long int k , kp , kn ;
@@ -293,9 +294,9 @@ static float GaussBlurFilterX (unsigned char *src, int height, int width, int ch
293
294
if (iradius > 1 )
294
295
{
295
296
k = 0 ;
296
- for (y = 0 ; y < height ; y ++ )
297
+ for (y = 0 ; y < ( int ) height ; y ++ )
297
298
{
298
- for (x = 0 ; x < width ; x ++ )
299
+ for (x = 0 ; x < ( int ) width ; x ++ )
299
300
{
300
301
for (d = 0 ; d < channels ; d ++ )
301
302
{
@@ -315,7 +316,7 @@ static float GaussBlurFilterX (unsigned char *src, int height, int width, int ch
315
316
kp -= channels ;
316
317
}
317
318
xn = x + i ;
318
- if (xn < width )
319
+ if (xn < ( int ) width )
319
320
{
320
321
kn += channels ;
321
322
}
@@ -336,9 +337,9 @@ static float GaussBlurFilterX (unsigned char *src, int height, int width, int ch
336
337
}
337
338
}
338
339
k = 0 ;
339
- for (y = 0 ; y < height ; y ++ )
340
+ for (y = 0 ; y < ( int ) height ; y ++ )
340
341
{
341
- for (x = 0 ; x < width ; x ++ )
342
+ for (x = 0 ; x < ( int ) width ; x ++ )
342
343
{
343
344
for (d = 0 ; d < channels ; d ++ )
344
345
{
@@ -464,7 +465,7 @@ THRESHOLDAPI int ImageThresholdBimod(unsigned char* buf, bool* bufmask, unsigned
464
465
ImageHist (buf , histogram , width , height , channels , Tmax );
465
466
part *= 0.5f ;
466
467
threshold = HistBiMod (histogram , Tmax , part );
467
- threshold += delta ;
468
+ threshold += ( int ) delta ;
468
469
threshold = ImageThreshold (buf , bufmask , width , height , channels , threshold );
469
470
470
471
return threshold ;
@@ -517,11 +518,11 @@ THRESHOLDAPI int ImageThresholdSauvola(unsigned char* buf, bool* bufmask, unsign
517
518
km = 0 ;
518
519
for (y = 0 ; y < height ; y ++ )
519
520
{
520
- y1 = (y < radius ) ? 0 : (y - radius );
521
+ y1 = (y < ( unsigned int ) radius ) ? 0 : (y - ( unsigned int ) radius );
521
522
y2 = (y + radius + 1 < height ) ? (y + radius + 1 ) : height ;
522
523
for (x = 0 ; x < width ; x ++ )
523
524
{
524
- x1 = (x < radius ) ? 0 : (x - radius );
525
+ x1 = (x < ( unsigned int ) radius ) ? 0 : (x - ( unsigned int ) radius );
525
526
x2 = (x + radius + 1 < width ) ? (x + radius + 1 ) : width ;
526
527
imm = 0 ;
527
528
imv = 0 ;
@@ -546,8 +547,8 @@ THRESHOLDAPI int ImageThresholdSauvola(unsigned char* buf, bool* bufmask, unsign
546
547
imv /= n ;
547
548
imv -= (imm * imm );
548
549
imv = (imv < 0 ) ? - imv : imv ;
549
- imv = sqrt (imv );
550
- ima = 1.0 - imv / dynamic_range ;
550
+ imv = ( float )( sqrt (imv ) );
551
+ ima = 1.0f - imv / dynamic_range ;
551
552
imx = 0.0f ;
552
553
for (d = 0 ; d < channels ; d ++ )
553
554
{
@@ -564,7 +565,7 @@ THRESHOLDAPI int ImageThresholdSauvola(unsigned char* buf, bool* bufmask, unsign
564
565
}
565
566
else
566
567
{
567
- t = imm * (1.0 - sensitivity * ima ) + delta ;
568
+ t = imm * (1.0f - sensitivity * ima ) + delta ;
568
569
}
569
570
bufmask [km ] = (imx < t );
570
571
km ++ ;
@@ -627,11 +628,71 @@ THRESHOLDAPI int ImageThresholdBlur(unsigned char* buf, bool* bufmask, unsigned
627
628
}
628
629
}
629
630
ImageMathDivide (buf , bufb , bufb , width , height , channels , -127.0f );
631
+ threshold = ImageThresholdBimod (bufb , bufmask , width , height , channels , part , delta );
632
+ free (bufb );
633
+ }
634
+ else
635
+ {
636
+ threshold = ImageThresholdBimod (buf , bufmask , width , height , channels , part , delta );
630
637
}
631
638
632
- threshold = ImageThresholdBimod (bufb , bufmask , width , height , channels , part , delta );
633
- if (bufb )
639
+ return threshold ;
640
+ }
641
+
642
+ /*
643
+ ImageThresholdEdgePlus()
644
+
645
+ input:
646
+ buf - unsigned char* image (height * width * channels)
647
+ part = 1.0f [1:1]
648
+ delta = 0.0f [off, regulator]
649
+ sensitivity = 0.2;
650
+
651
+ output:
652
+ bufmask - bool* image mask (height * width)
653
+ threshold - threshold value
654
+
655
+ Use:
656
+ int threshold = ImageThresholdEdgePlus(buf, bufmask, width, height, channels, raduis, part, delta, sensitivity);
657
+ */
658
+
659
+ THRESHOLDAPI int ImageThresholdEdgePlus (unsigned char * buf , bool * bufmask , unsigned int width , unsigned int height , unsigned int channels , float raduis , float part , float delta , float sensitivity )
660
+ {
661
+ int threshold = 0 ;
662
+ unsigned int y , x , d ;
663
+ float imo , imx , edge , edgeplus ;
664
+ unsigned long int k ;
665
+ unsigned char * bufb = NULL ;
666
+
667
+ if ((bufb = (unsigned char * )malloc (height * width * channels * sizeof (unsigned char ))))
668
+ {
669
+ ImageCopy (buf , bufb , width , height , channels );
670
+ (void )GaussBlurFilter (bufb , width , height , channels , raduis , raduis );
671
+ k = 0 ;
672
+ for (y = 0 ; y < height ; y ++ )
673
+ {
674
+ for (x = 0 ; x < width ; x ++ )
675
+ {
676
+ for (d = 0 ; d < channels ; d ++ )
677
+ {
678
+ imo = (float )buf [k ];
679
+ imx = (float )bufb [k ];
680
+ edge = (imo + 1.0f ) / (imx + 1.0f ) - 0.5f ;
681
+ edgeplus = imo * edge ;
682
+ imx = sensitivity * edgeplus + (1.0f - sensitivity ) * imo ;
683
+ imx = (imx < 0.0f ) ? 0.0f : (imx < 255.0f ) ? imx : 255.0f ;
684
+ bufb [k ] = (unsigned char )imx ;
685
+ k ++ ;
686
+ }
687
+ }
688
+ }
689
+ threshold = ImageThresholdBimod (bufb , bufmask , width , height , channels , part , delta );
634
690
free (bufb );
691
+ }
692
+ else
693
+ {
694
+ threshold = ImageThresholdBimod (buf , bufmask , width , height , channels , part , delta );
695
+ }
635
696
636
697
return threshold ;
637
698
}
0 commit comments