Experimental Image::BoxFind module:    Image/t/17-center_on_edge.t


     # Test file created outside of h2xs framework.
# Run this like so: `perl Image-BoxFind.t'
#   doom@kzsu.stanford.edu     2007/10/22 04:50:12

use warnings;
use strict;
$|=1;
my $DEBUG = 0;
use Data::Dumper;

use Test::More;
BEGIN { plan tests => 14 };
use Test::Number::Delta;

use File::Basename qw( basename fileparse);

use FindBin qw( $Bin );
use lib "$Bin/../..";
use lib "$Bin/lib";

BEGIN { #1
  use_ok( 'Image::BoxFind' );
}
use Image::BoxFind::Testing::Eyeballs ':all';

#2
ok(1, "Traditional: If we made it this far, we're ok.");

my $image_bin = "$Bin/dat/images";

### x_plus direction
my $bf_attributes = {
     pixpat_delta_method => 'has_pixpat_changed_past_threshold',
     allow_pixpat_drift  =>  1,
     pixpat_threshold    =>  500,
     rectangle_finder    =>  'boxfind_downward_via_pixpat',
     direction          => 'x_plus',
#     spotsize_x         => 3,
#     spotsize_y         => 4,
     refocus_factor     =>  4,
     forward_horizon    =>  3,
     transverse_horizon =>  4,
#     looks_like_edge    =>  10, # TODO play with this?
#     edge_contrast_cutoff => 10,
     edge_contrast_cutoff => 100,

     # used by 'looks_rectangular'
     fuzziness   => 5,
     minimum_height => 10,
     minimum_width  => 10,

     # used by 'roughly_raster'
     beware => 0,
     step_x => 25,
     step_y => 10,
    };

my @image_cases =  # For the x_plus direction
  ( {
     image_file => "$image_bin/firefox_save_as.png",
     start    => [430,122],
     fin      => 127,
    },
    {
     image_file => "$image_bin/firefox_save_as.png",
     start    => [200,28],
     fin      => 31,
    },
    {
     image_file => "$image_bin/rhythmbox_prefs.png",
     start    => [200,11],
     fin      => 16,
    },
    {
     image_file => "$image_bin/rhythmbox_prefs.png",
     start    => [245,70],
     fin      => 77,
    },
    {
     image_file => "$image_bin/gimp_open_dialog.png",
     start    => [470,355],
     fin      => 359,
    },
    {
     image_file => "$image_bin/gimp_open_dialog.png",
     start    => [330,0],
     fin      => 10,
    },
    {
     image_file => "$image_bin/gimp_open_dialog.png",
     start    => [500,0],
     fin      => 12,
    },
    {
     image_file => "$image_bin/firefox_save_as.png",
     start    => [300,65],
     fin      => 71,
    },
    {
     image_file => "$image_bin/rhythmbox_prefs.png",
     start    => [200,0],
     fin      => 0,
    },


  );

my $test_name = "Testing center_on_edge direction: x_plus";
foreach my $image_case ( @image_cases ) {
  my $image_file = $image_case->{image_file};
  my $basename = (  fileparse($image_file, qr{\.png} ) )[0];

  my $extra_attributes =
    {
     image_file  => $image_file,
     DEBUG       => $DEBUG,
    };

  my @keys = (keys %{ $extra_attributes });
  @{ $bf_attributes }{ @keys }  = @{ $extra_attributes }{ @keys };

  my $bf = Image::BoxFind->new( $bf_attributes );

  my ($x0, $y0) = @{ $image_case->{start} };
  my $expected_y  = $image_case->{fin};

  my $end_point = $bf->center_on_edge( $x0, $y0 );
  my $result = $end_point->[1];

  my $tolerance = 2;
  delta_within( $result, $expected_y, $tolerance,
                "$test_name $basename start: ($x0, $y0) finish: $expected_y" );

}


### The 'y_plus' direction

$bf_attributes = {

     pixpat_delta_method => 'has_pixpat_changed_past_threshold',
     allow_pixpat_drift  =>  1,
     pixpat_threshold    =>  500,
     rectangle_finder    =>  'boxfind_downward_via_pixpat',
     direction          => 'y_plus',
#     spotsize_x         => 3,
#     spotsize_y         => 4,
     refocus_factor     => 4,

     forward_horizon    =>  3,
     transverse_horizon =>  4,
#     looks_like_edge    =>  10, # TODO play with this?
#     edge_contrast_cutoff => 2,
     edge_contrast_cutoff => 100,

     # used by 'looks_rectangular'
     fuzziness   => 5,
     minimum_height => 10,
     minimum_width  => 10,

     # used by 'roughly_raster'
     beware => 0,
     step_x => 25,
     step_y => 10,
    };


@image_cases =
  ( {
     image_file => "$image_bin/firefox_save_as.png",
     start    => [110,65],
     fin      => 112,
    },
    {
     image_file => "$image_bin/rhythmbox_prefs.png",
     start    => [237,110],
     fin      => 241,
    },
    {
     image_file => "$image_bin/gimp_open_dialog.png",
     start    => [203,200],
     fin      => 209,
    },
  );

$test_name = "Testing center_on_edge direction: y_plus";
foreach my $image_case ( @image_cases ) {
  my $image_file = $image_case->{image_file};
  my $basename = (  fileparse($image_file, qr{\.png} ) )[0];

  my $extra_attributes =
    {
     image_file  => $image_file,
     DEBUG       => $DEBUG,
    };

  my @keys = (keys %{ $extra_attributes });
  @{ $bf_attributes }{ @keys }  = @{ $extra_attributes }{ @keys };

  my $bf = Image::BoxFind->new( $bf_attributes );

  my ($x0, $y0) = @{ $image_case->{start} };
  my $expected_x  = $image_case->{fin};

  my $end_point = $bf->center_on_edge( $x0, $y0 );
  my $result = $end_point->[0];

  my $tolerance = 2;
  delta_within( $result, $expected_x, $tolerance,
                "$test_name $basename start: ($x0, $y0) finish: $result" );

}


     

Joseph Brenner, Tue Nov 27 17:40:02 2007