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

Function auto_threshold_image

dlib/image_transforms/thresholding.h:446–550  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

444 typename out_image_type
445 >
446 void auto_threshold_image (
447 const in_image_type& in_img_,
448 out_image_type& out_img_
449 )
450 {
451 COMPILE_TIME_ASSERT( pixel_traits<typename image_traits<in_image_type>::pixel_type>::has_alpha == false );
452 COMPILE_TIME_ASSERT( pixel_traits<typename image_traits<out_image_type>::pixel_type>::has_alpha == false );
453 COMPILE_TIME_ASSERT( pixel_traits<typename image_traits<in_image_type>::pixel_type>::is_unsigned == true );
454 COMPILE_TIME_ASSERT( pixel_traits<typename image_traits<out_image_type>::pixel_type>::is_unsigned == true );
455
456 COMPILE_TIME_ASSERT(pixel_traits<typename image_traits<out_image_type>::pixel_type>::grayscale);
457
458 image_view<out_image_type> out_img(out_img_);
459
460 // if there isn't any input image then don't do anything
461 if (image_size(in_img_) == 0)
462 {
463 out_img.clear();
464 return;
465 }
466
467 unsigned long thresh;
468 // find the threshold we should use
469 matrix<unsigned long,1> hist;
470 get_histogram(in_img_,hist);
471
472 const_image_view<in_image_type> in_img(in_img_);
473
474 // Start our two means (a and b) out at the ends of the histogram
475 long a = 0;
476 long b = hist.size()-1;
477 bool moved_a = true;
478 bool moved_b = true;
479 while (moved_a || moved_b)
480 {
481 moved_a = false;
482 moved_b = false;
483
484 // catch the degenerate case where the histogram is empty
485 if (a >= b)
486 break;
487
488 if (hist(a) == 0)
489 {
490 ++a;
491 moved_a = true;
492 }
493
494 if (hist(b) == 0)
495 {
496 --b;
497 moved_b = true;
498 }
499 }
500
501 // now do k-means clustering with k = 2 on the histogram.
502 moved_a = true;
503 moved_b = true;

Callers

nothing calls this directly

Calls 6

image_sizeFunction · 0.85
get_histogramFunction · 0.85
absFunction · 0.85
threshold_imageFunction · 0.85
clearMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected