MCPcopy Create free account
hub / github.com/davisking/dlib / segment_image

Function segment_image

dlib/image_transforms/segment_image.h:336–426  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

334 typename out_image_type
335 >
336 void segment_image (
337 const in_image_type& in_img_,
338 out_image_type& out_img_,
339 const double k = 200,
340 const unsigned long min_size = 10
341 )
342 {
343 using namespace dlib::impl;
344 typedef typename image_traits<in_image_type>::pixel_type ptype;
345 typedef typename edge_diff_funct<ptype>::diff_type diff_type;
346
347 // make sure requires clause is not broken
348 DLIB_ASSERT(is_same_object(in_img_, out_img_) == false,
349 "\t void segment_image()"
350 << "\n\t The input images can't be the same object."
351 );
352
353 COMPILE_TIME_ASSERT(is_unsigned_type<typename image_traits<out_image_type>::pixel_type>::value);
354
355 const_image_view<in_image_type> in_img(in_img_);
356 image_view<out_image_type> out_img(out_img_);
357
358 out_img.set_size(in_img.nr(), in_img.nc());
359 // don't bother doing anything if the image is too small
360 if (in_img.nr() < 2 || in_img.nc() < 2)
361 {
362 assign_all_pixels(out_img,0);
363 return;
364 }
365
366 disjoint_subsets sets;
367 sets.set_size(in_img.size());
368
369 std::vector<segment_image_edge_data_T<diff_type> > sorted_edges;
370 get_pixel_edges(in_img, sorted_edges);
371
372 std::vector<graph_image_segmentation_data_T<diff_type> > data(in_img.size());
373
374 // now start connecting blobs together to make a minimum spanning tree.
375 for (unsigned long i = 0; i < sorted_edges.size(); ++i)
376 {
377 const unsigned long idx1 = sorted_edges[i].idx1;
378 const unsigned long idx2 = sorted_edges[i].idx2;
379
380 unsigned long set1 = sets.find_set(idx1);
381 unsigned long set2 = sets.find_set(idx2);
382 if (set1 != set2)
383 {
384 const diff_type diff = sorted_edges[i].diff;
385 const diff_type tau1 = static_cast<diff_type>(k/data[set1].component_size);
386 const diff_type tau2 = static_cast<diff_type>(k/data[set2].component_size);
387
388 const diff_type mint = std::min(data[set1].internal_diff + tau1,
389 data[set2].internal_diff + tau2);
390 if (diff <= mint)
391 {
392 const unsigned long new_set = sets.merge_sets(set1, set2);
393 data[new_set].component_size = data[set1].component_size + data[set2].component_size;

Callers 1

test_segment_imageFunction · 0.85

Calls 10

assign_all_pixelsFunction · 0.85
get_pixel_edgesFunction · 0.85
is_same_objectFunction · 0.50
minFunction · 0.50
set_sizeMethod · 0.45
nrMethod · 0.45
ncMethod · 0.45
sizeMethod · 0.45
find_setMethod · 0.45
merge_setsMethod · 0.45

Tested by 1

test_segment_imageFunction · 0.68