3 @brief Double-layer oscillatory network with phase oscillator for image segmentation. 4 @details Implementation based on paper @cite inproceedings::nnet::syncsegm::1. 6 @authors Andrei Novikov (pyclustering@yandex.ru) 8 @copyright GNU Public License 10 @cond GNU_PUBLIC_LICENSE 11 PyClustering is free software: you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation, either version 3 of the License, or 14 (at your option) any later version. 16 PyClustering is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 21 You should have received a copy of the GNU General Public License 22 along with this program. If not, see <http://www.gnu.org/licenses/>. 29 from math
import floor
33 except Exception
as error_instance:
34 warnings.warn(
"Impossible to import PIL (please, install 'PIL'), pyclustering's visualization " 35 "functionality is partially not available (details: '%s')." % str(error_instance))
47 @brief Result visualizer of double-layer oscillatory network 'syncsegm'. 54 @brief Shows output dynamic of the first layer. 56 @param[in] analyser (syncsegm_analyser): Analyser of output dynamic of the 'syncsegm' oscillatory network. 60 sync_visualizer.show_output_dynamic(analyser.get_first_layer_analyser());
66 @brief Shows output dynamic of the second layer. 68 @param[in] analyser (syncsegm_analyser): Analyser of output dynamic of the 'syncsegm' oscillatory network. 72 second_layer_analysers = analyser.get_second_layer_analysers();
73 analysers_sequence = [ object_segment_analyser[
'analyser']
for object_segment_analyser
in second_layer_analysers ]
75 sync_visualizer.show_output_dynamics(analysers_sequence);
80 @brief Performs analysis of output dynamic of the double-layer oscillatory network 'syncsegm' to extract information about segmentation results. 84 def __init__(self, color_analyser, object_segment_analysers = None):
86 @brief Constructor of the analyser. 88 @param[in] color_analyser (list): Analyser of coloring segmentation results of the first layer. 89 @param[in] object_segment_analysers (list): Analysers of objects on image segments - results of the second layer. 99 @brief Returns analyser of coloring segmentation of the first layer. 108 @brief Returns analysers of object segmentation of the second layer. 117 @brief Allocates color segments. 119 @param[in] eps (double): Tolerance level that define maximal difference between phases of oscillators in one segment. 120 @param[in] noise_size (uint): Threshold that defines noise - segments size (in pixels) that is less then the threshold is considered as a noise. 122 @return (list) Color segments where each color segment consists of indexes of pixels that forms color segment. 127 real_segments = [cluster
for cluster
in segments
if len(cluster) > noise_size];
128 return real_segments;
133 @brief Allocates object segments. 135 @param[in] eps (double): Tolerance level that define maximal difference between phases of oscillators in one segment. 136 @param[in] noise_size (uint): Threshold that defines noise - segments size (in pixels) that is less then the threshold is considered as a noise. 138 @return (list) Object segments where each object segment consists of indexes of pixels that forms object segment. 147 indexes = object_segment_analyser[
'color_segment'];
148 analyser = object_segment_analyser[
'analyser'];
150 segments += analyser.allocate_clusters(eps, indexes);
152 real_segments = [segment
for segment
in segments
if len(segment) > noise_size];
153 return real_segments;
158 @brief Class represents segmentation algorithm syncsegm. 159 @details syncsegm is a bio-inspired algorithm that is based on double-layer oscillatory network that uses modified Kuramoto model. 160 Algorithm extracts colors and colored objects. It uses only CCORE (C++ implementation of pyclustering) parts to implement the algorithm. 162 CCORE option is True by default to use sync network in the pyclustering core - C/C++ shared library for processing that significantly increases performance. 166 # create oscillatory for image segmentaion - extract colors (radius 128) and objects (radius 4), 167 # and ignore noise (segments with size that is less than 10 pixels) 168 algorithm = syncsegm(128, 4, 10); 170 # extract segments (colors and objects) 171 analyser = algorithm(path_to_file); 173 # obtain segmentation results (only colors - from the first layer) 174 color_segments = analyser.allocate_colors(0.01, 10); 175 draw_image_mask_segments(path_to_file, color_segments); 177 # obtain segmentation results (objects - from the second layer) 178 object_segments = analyser.allocate_objects(0.01, 10); 179 draw_image_mask_segments(path_to_file, object_segments); 184 def __init__(self, color_radius, object_radius, noise_size = 0, ccore = True):
186 @brief Contructor of the oscillatory network SYNC for cluster analysis. 188 @param[in] color_radius (double): Radius of color connectivity (color similarity) for the first layer. 189 @param[in] object_radius (double): Radius of object connectivity (object similarity) for the second layer, 190 if 'None' then object segmentation is not performed (only color segmentation). 191 @param[in] noise_size (double): Size of segment that should be considered as a noise and ignored by the second layer. 192 @param[in] ccore (bool): If 'True' then C/C++ implementation is used to increase performance. 207 def process(self, image_source, collect_dynamic = False, order_color = 0.9995, order_object = 0.999):
209 @brief Performs image segmentation. 211 @param[in] image_source (string): Path to image file that should be processed. 212 @param[in] collect_dynamic (bool): If 'True' then whole dynamic of each layer of the network is collected. 213 @param[in] order_color (double): Local synchronization order for the first layer - coloring segmentation. 214 @param[in] order_object (double): Local synchronization order for the second layer - object segmentation. 216 @return (syncsegm_analyser) Analyser of segmentation results by the network. 223 data = read_image(image_source)
229 object_segment_analysers = self.
__analyse_objects(image_source, color_analyser, collect_dynamic)
233 def __analyse_colors(self, image_data, collect_dynamic):
235 @brief Performs color segmentation by the first layer. 237 @param[in] image_data (array_like): Image sample as a array-like structure. 238 @param[in] collect_dynamic (bool): If 'True' then whole dynamic of the first layer of the network is collected. 240 @return (syncnet_analyser) Analyser of color segmentation results of the first layer. 245 analyser = network.process(self.
__order_color, solve_type.FAST, collect_dynamic);
250 def __analyse_objects(self, image_source, color_analyser, collect_dynamic):
252 @brief Performs object segmentation by the second layer. 254 @param[in] image_source (string): Path to image file that should be processed. 255 @param[in] color_analyser (syncnet_analyser): Analyser of color segmentation results. 256 @param[in] collect_dynamic (bool): If 'True' then whole dynamic of the first layer of the network is collected. 258 @return (map) Analysers of object segments. 263 pointer_image = Image.open(image_source);
264 image_size = pointer_image.size;
266 object_analysers = [];
268 color_segments = color_analyser.allocate_clusters();
270 for segment
in color_segments:
272 if (object_analyser
is not None):
273 object_analysers.append( {
'color_segment': segment,
'analyser': object_analyser } );
275 pointer_image.close();
276 return object_analysers;
279 def __analyse_color_segment(self, image_size, color_segment, collect_dynamic):
281 @brief Performs object segmentation of separate segment. 283 @param[in] image_size (list): Image size presented as a [width x height]. 284 @param[in] color_segment (list): Image segment that should be processed. 285 @param[in] collect_dynamic (bool): If 'True' then whole dynamic of the second layer of the network is collected. 287 @return (syncnet_analyser) Analyser of object segmentation results of the second layer. 295 network =
syncnet(coordinates, self.
__object_radius, initial_phases = initial_type.EQUIPARTITION, ccore =
True);
296 analyser = network.process(self.
__order_object, solve_type.FAST, collect_dynamic);
301 def __extract_location_coordinates(self, image_size, color_segment):
303 @brief Extracts coordinates of specified image segment. 305 @param[in] image_size (list): Image size presented as a [width x height]. 306 @param[in] color_segment (list): Image segment whose coordinates should be extracted. 308 @return (list) Coordinates of each pixel. 312 for index
in color_segment:
313 y = floor(index / image_size[0]);
314 x = index - y * image_size[0];
316 coordinates.append([x, y]);
def __init__(self, color_analyser, object_segment_analysers=None)
Constructor of the analyser.
def __analyse_objects(self, image_source, color_analyser, collect_dynamic)
Performs object segmentation by the second layer.
def get_second_layer_analysers(self)
Returns analysers of object segmentation of the second layer.
def get_first_layer_analyser(self)
Returns analyser of coloring segmentation of the first layer.
def __init__(self, color_radius, object_radius, noise_size=0, ccore=True)
Contructor of the oscillatory network SYNC for cluster analysis.
__object_segment_analysers
Utils that are used by modules of pyclustering.
def show_first_layer_dynamic(analyser)
Shows output dynamic of the first layer.
Class represents clustering algorithm SyncNet.
def __analyse_color_segment(self, image_size, color_segment, collect_dynamic)
Performs object segmentation of separate segment.
def __extract_location_coordinates(self, image_size, color_segment)
Extracts coordinates of specified image segment.
Result visualizer of double-layer oscillatory network 'syncsegm'.
Neural Network: Oscillatory Neural Network based on Kuramoto model.
def show_second_layer_dynamic(analyser)
Shows output dynamic of the second layer.
def allocate_objects(self, eps=0.01, noise_size=1)
Allocates object segments.
def allocate_colors(self, eps=0.01, noise_size=1)
Allocates color segments.
Cluster analysis algorithm: Sync.
def __analyse_colors(self, image_data, collect_dynamic)
Performs color segmentation by the first layer.
def process(self, image_source, collect_dynamic=False, order_color=0.9995, order_object=0.999)
Performs image segmentation.
Class represents segmentation algorithm syncsegm.
Performs analysis of output dynamic of the double-layer oscillatory network 'syncsegm' to extract inf...
Neural and oscillatory network module.