AKTIVE

thresholds.tcl at trunk
Login

thresholds.tcl at trunk

File etc/accessor/thresholds.tcl artifact cebbfea34a on branch trunk


     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
## -*- mode: tcl ; fill-column: 90 -*-
# # ## ### ##### ######## ############# #####################
## Getter -- Threshold generation - Global
#
#	https://craftofcoding.wordpress.com/2021/10/27/thresholding-algorithms-bernsen-local/
#	https://craftofcoding.wordpress.com/2021/09/30/thresholding-algorithms-niblack-local/
#	https://craftofcoding.wordpress.com/2021/10/06/thresholding-algorithms-sauvola-local/
#	https://craftofcoding.wordpress.com/2021/09/28/thresholding-algorithms-phansalkar-local/

# # ## ### ##### ######## ############# #####################
## Mean

def ref-bernsen    \[Bernsen\](https://craftofcoding.wordpress.com/2021/10/27/thresholding-algorithms-bernsen-local)
def ref-sauvola    \[Sauvola\](https://craftofcoding.wordpress.com/2021/10/06/thresholding-algorithms-sauvola-local)
def ref-niblack    \[Niblack\](https://craftofcoding.wordpress.com/2021/09/30/thresholding-algorithms-niblack-local)
def ref-phansalkar \[Phansalkar\](https://craftofcoding.wordpress.com/2021/09/28/thresholding-algorithms-phansalkar-local)
def ref-otsu       \[Otsu\](https://en.wikipedia.org/wiki/Otsu%27s_method)

operator image::threshold::global::mean {
    section accessor threshold generate

    example {
	scancrop
	@1 | -text
    }

    example {
	butterfly
	@1 | -text
    }

    note Returns a global threshold for the input, as the image mean.

    note The operator "<!xref: aktive image mask per global mean>" \
	uses this to generate a mask of the input.

    note There are better methods. Extensions to the simple mean, in order \
	of creation (and complexity), are @@ref-sauvola@@, @@ref-niblack@@, and \
	@@ref-phansalkar@@. Each of these modifies the plain mean with a bias \
	based on a mix of mean, standard deviation, and parameters.

    input

    strict single \
	The computed pixels are not materialized. \
	They are immediately reduced to the threshold.

    body {
	# t = meanI
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#
	# T = meanI - src
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	return [aktive op image mean $src]
    }
}

# # ## ### ##### ######## ############# #####################
## Bernsen

operator image::threshold::global::bernsen {
    section accessor threshold generate

    example {
	scancrop
	@1 | -text
    }

    example {
	butterfly
	@1 | -text
    }

    note Returns a global threshold for the input, \
	according to @@ref-bernsen@@'s method.

    note The operator "<!xref: aktive image mask per global bernsen>" \
	uses this to generate a mask of the input.

    input

    strict single \
	The computed pixels are not materialized. \
	They are immediately reduced to the threshold.

    body {
	# t = (minI + maxI) / 2
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#
	#               maxI
	#              /    \.
	# T = (*) - (+)      >- src
	#        \     \.   /
	#         0.5   maxI
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	set min [aktive op image min $src]
	set max [aktive op image max $src]

	return [expr {0.5 * ($min + $max)}]
    }
}

# # ## ### ##### ######## ############# #####################
## Niblack

operator image::threshold::global::niblack {
    section accessor threshold generate

    example {
	scancrop
	@1 | -text
    }

    example {
	butterfly
	@1 | -text
    }

    note Returns a global threshold for the input, \
	according to @@ref-niblack@@'s method.

    note The operator "<!xref: aktive image mask per global niblack>" \
	uses this to generate a mask of the input.

    double? -0.2 k	niblack parameter

    input

    strict single \
	The computed pixels are not materialized. \
	They are immediately reduced to the threshold.

    body {
	# t = meanI + (k * stdI)
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#
	#          meanI ---\.
	#        /           \.
	# t = (+)            /- src
	#        \.         /
	#         (*) - stdI
	#            \.
	#             k
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	set mean [aktive op image mean   $src]
	set std  [aktive op image stddev $src]

	return [expr {$mean + $k * $std}]
    }
}

# # ## ### ##### ######## ############# #####################
## Sauvola

operator image::threshold::global::sauvola {
    section accessor threshold generate

    example {
	scancrop
	@1 | -text
    }

    example {
	butterfly
	@1 | -text
    }

    note Returns a global threshold for the input, \
	according to @@ref-sauvola@@'s method.

    note The operator "<!xref: aktive image mask per global sauvola>" \
	uses this to generate a mask of the input.

    double? 0.5 k	sauvola parameter
    double? 128 R	sauvola parameter

    input

    strict single \
	The computed pixels are not materialized. \
	They are immediately reduced to the threshold.

    body {
	# t = meanI * (1 + k * ((stdI/R) - 1))
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#
	#          meanI -------------------\.
	#        /                          /- src
	# t = (*)     (*) - (-) - (/) - stdI
	#        \.  /   \     \     \.
	#         (+)     k     1     R
	#            \.
	#             1
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	set mean [aktive op image mean   $src]
	set std  [aktive op image stddev $src]

	return [expr {$mean * (1. + $k * ($std / double($R) - 1.))}]
    }
}

# # ## ### ##### ######## ############# #####################
## Phansalkar

operator image::threshold::global::phansalkar {
    section accessor threshold generate

    example {
	scancrop
	@1 | -text
    }

    example {
	butterfly
	@1 | -text
    }

    note Returns a global threshold for the input, \
	according to @@ref-phansalkar@@'s method.

    note The operator "<!xref: aktive image mask per global phansalkar>" \
	uses this to generate a mask of the input.

    double? 0.25 k	phansalkar parameter
    double? 0.5  R	phansalkar parameter
    double? 3    p	phansalkar parameter
    double? 10   q	phansalkar parameter

    input

    strict single \
	The computed pixels are not materialized. \
	They are immediately reduced to the threshold.

    body {
	# t = meanN * (p * exp(-q * meanN) + 1 + k * ((stdN / R) - 1))
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#
	#            /------------------- meanN
	#           /                   /      \.
	#          /         (exp) - (*)        \.
	#         /         /           \ -q     \- src
	#        /      (*) - p                  /
	# t = (*)      /                        /
	#        \- (+) - (*) - (-) - (/) - stdN
	#              \     \     \     \.
	#               1     k     -1    R
	#
	# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	set mean [aktive op image mean   $src]
	set std  [aktive op image stddev $src]

	return [expr {$mean * ($p * exp(- $q * $mean) + 1. + $k * ($std / double($R) - 1.))}]
    }
}

# # ## ### ##### ######## ############# #####################
## Otsu

operator image::threshold::global::otsu {
    section accessor threshold generate

    example {
	scancrop
	@1 | -text
    }

    note Returns a global threshold for the input, \
	according to @@ref-otsu@@'s method.

    note The operator "<!xref: aktive image mask per global otsu>" \
	uses this to generate a mask of the input.

    int? 256 bins \
	The number of bins used by the internal histogram. \
	The pixel values are quantized to fit. \
	Only values in the range of `\[0..1\]` are considered valid. \
	Values outside of that range are placed into the smallest/largest \
	bins, respectively. \
	\
	The default quantizes the image values to 8-bit.

    input

    strict single \
	The computed pixels are not materialized. \
	They are immediately reduced to the threshold.

    body {
	# t = scaled (extract (row otsu (image histogram I)))
	#     the threshold is scaled by the number of bins, to be in [0..1]

	set e [aktive op image histogram $src bins $bins]
	set e [aktive op row otsu $e]
	return [expr {[lindex [aktive query values $e] 0] / double($bins)}]
    }
}

##
# # ## ### ##### ######## ############# #####################
::return