/usr/share/pyshared/gamera/plugins/segmentation.py is in python-gamera 3.3.3-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# Copyright (C) 2001-2005 Ichiro Fujinaga, Michael Droettboom, and Karl MacMillan
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from gamera.plugin import *
from gamera import util
import _segmentation
class Segmenter(PluginFunction):
self_type = ImageType([ONEBIT])
return_type = ImageList("ccs")
doc_examples = [(ONEBIT,)]
class cc_analysis(Segmenter):
"""
Performs connected component analysis on the image.
This algorithm assumes 8-connected components, meaning any two
pixels are considered "connected" if they are adjacent in any
direction, including diagonally.
The original image will have all of its pixels "labeled" with a
number representing each connected component. This is so the
connected components can share data with their source image and
makes things much more efficient.
Returns a list of ccs found in the image. Since all the CC's
share the same data with the original image, changing the CC's
will affect the original. If you do not want this behavior, use
the image_copy_ function on each of the CCs::
ccs = [x.image_copy() for x in ccs]
.. _image_copy: utility.html#image-copy
"""
pass
class cc_and_cluster(Segmenter):
"""
Performs connected component analysis using cc_analysis_ and then
clusters the CC's according to their similarity.
TODO: We need some more detailed documentation here.
"""
pure_python = True
args = Args([Float('ratio', default = 1.0), Int('distance', default=2)])
return_type = ImageList("ccs")
def __call__(image, ratio = 1.0, distance = 2):
from gamera import cluster
cc = image.cc_analysis()
return cluster.cluster(cc, ratio, distance)
__call__ = staticmethod(__call__)
doc_examples = [(ONEBIT,)]
class splitx(Segmenter):
"""
Splits an image vertically.
The split point is determined automatically by finding a valley in
the projections near *center*.
This function is overloaded to work both with a single value
and a list of splitting point candidates as input.
"""
args = Args([FloatVector("center", default=[0.5])])
doc_examples = [(ONEBIT,)]
def __call__(self, center=0.5):
if not util.is_sequence(center):
return _segmentation.splitx(self, [center])
else:
return _segmentation.splitx(self, center)
__call__ = staticmethod(__call__)
author = "Michael Droettboom, Karl MacMillan and Christoph Dalitz"
class splitx_max(Segmenter):
"""Splits an image vertically.
The split point is determined automatically by finding a peak in
the projections near *center*.
This function is overloaded to work both with a single value and a
list of splitting point canidates as input.
"""
args = Args([FloatVector("center", default=[0.5])])
def __call__(self, center=0.5):
if not util.is_sequence(center):
return _segmentation.splitx_max(self, [center])
else:
return _segmentation.splitx_max(self, center)
__call__ = staticmethod(__call__)
author = "Michael Droettboom, Karl MacMillan and Christoph Dalitz"
class splity(Segmenter):
"""
Splits an image horizontally.
The split point is determined automatically by finding a valley in
the projections near *center*.
This function is overloaded to work both with a single value and a
list of splitting point canidates as input.
"""
args = Args([FloatVector("center", default=[0.5])])
def __call__(self, center=[0.5]):
if not util.is_sequence(center):
return _segmentation.splity(self, [center])
else:
return _segmentation.splity(self, center)
__call__ = staticmethod(__call__)
author = "Michael Droettboom, Karl MacMillan and Christoph Dalitz"
class splitx_base(Segmenter):
pure_python = True
return_type = ImageList("splits")
class splitx_left(splitx_base):
"""
Splits an image vertically.
The split point is determined automatically by finding a valley in
the projections near the left of the image.
"""
_center = 0.25
def __call__(self):
return self.splitx(0.25)
__call__ = staticmethod(__call__)
class splitx_right(splitx_base):
"""
Splits an image vertically.
The split point is determined automatically by finding a valley in
the projections near the right of the image.
"""
_center = 0.75
def __call__(self):
return self.splitx(0.75)
__call__ = staticmethod(__call__)
class splity_base(Segmenter):
pure_python = True
return_type = ImageList("splits")
class splity_top(splity_base):
"""
Splits an image horizontally.
The split point is determined automatically by finding a valley in
the projections near the top of the image.
"""
_center = 0.25
def __call__(self):
return self.splity(0.25)
__call__ = staticmethod(__call__)
class splity_bottom(splity_base):
"""
Splits an image horizontally.
The split point is determined automatically by finding a valley in
the projections near the bottom of the image.
"""
_center = 0.75
def __call__(self):
return self.splity(0.75)
__call__ = staticmethod(__call__)
# connected-component filters
def filter_wide(ccs, max_width):
tmp = []
for x in ccs:
if x.ncols > max_width:
x.fill_white()
else:
tmp.append(x)
return tmp
def filter_narrow(ccs, min_width):
tmp = []
for x in ccs:
if x.ncols < min_width:
x.fill_white()
else:
tmp.append(x)
return tmp
def filter_tall(ccs, max_height):
tmp = []
for x in ccs:
if x.nrows > max_height:
x.fill_white()
else:
tmp.append(x)
return tmp
def filter_short(ccs, min_height):
tmp = []
for x in ccs:
if x.nrows < min_height:
x.fill_white()
else:
tmp.append(x)
return tmp
def filter_small(ccs, min_size):
tmp = []
for x in ccs:
if x.nrows < min_size or x.ncols < min_size:
x.fill_white()
else:
tmp.append(x)
return tmp
def filter_large(ccs, max_size):
tmp = []
for x in ccs:
if x.nrows > max_size or x.ncols > max_size:
x.fill_white()
else:
tmp.append(x)
return tmp
def filter_black_area_small(ccs, min_size):
tmp = []
for x in ccs:
if x.black_area()[0] < min_size:
x.fill_white()
else:
tmp.append(x)
return tmp
def filter_black_area_large(ccs, max_size):
tmp = []
for x in ccs:
if x.black_area()[0] > max_size:
x.fill_white()
else:
tmp.append(x)
return tmp
class SegmentationModule(PluginModule):
category = "Segmentation"
cpp_headers=["segmentation.hpp"]
functions = [cc_analysis, cc_and_cluster, splitx, splity,
splitx_left, splitx_right, splity_top, splity_bottom,
splitx_max]
author = "Michael Droettboom and Karl MacMillan"
url = "http://gamera.sourceforge.net/"
module = SegmentationModule()
del Segmenter
del splitx_base
del splity_base
|