Experimental Image::BoxFind module:    Image/t/03-scans_and_turns_using_luminence_also_plus_edge_detect.t


     # Test file created outside of h2xs framework.
# Run this like so: `perl Image-BoxFind.t'
#   doom@kzsu.stanford.edu     2007/09/29 05:22:54

#########################

use warnings;
use strict;
$|=1;

my $DEBUG = 0;

use Data::Dumper;

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

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

BEGIN {
  use_ok( 'Image::BoxFind' );
}

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

my $tolerance = 3;

{
  my $test_name = "scan for change in average color";
  my $image_file = "$Bin/dat/images/simple_test.png";
  my $bf = Image::BoxFind->new( { image_file => $image_file,
                                  DEBUG      => $DEBUG,
                                } );

# instead of symmetric spot, we'll foreshorten it in direction of scan...
#  $bf->set_spotsize_x( 5 );
#  $bf->set_spotsize_y( 5 );

  $bf->set_luminance_threshold( 5000 );

  # gotta start somewhere
  $bf->set_cursor_x( 0 );
  $bf->set_cursor_y( 25 );

  # params for horizontal scan
  $bf->set_spotsize_x( 3 );
  $bf->set_spotsize_y( 8 );
  my $expected_x = 11;
  my $x_value = $bf->scan_right_spotcolor;

  delta_within( $x_value, $expected_x, $tolerance, "Testing $test_name: right" );

  # gotta start somewhere else
  $bf->set_cursor_x( 70 );
  $bf->set_cursor_y( 0 );

  # params for vertical scan
  $bf->set_spotsize_x( 8 );
  $bf->set_spotsize_y( 3 );

  my $expected_y = 8;
  my $y_value = $bf->scan_down_spotcolor;
  delta_within( $y_value, $expected_y, $tolerance, "Testing $test_name: down" );


  # gotta start somewhere else
  $bf->set_cursor_y( 11 );
  my $expected_y_2 = 14;

  $y_value = $bf->scan_down_spotcolor;
  delta_within( $y_value, $expected_y_2, $tolerance ,"Testing $test_name: restarted vertical" );

}

{
  my $test_name = "scan turn and scan again";
  my $image_file = "$Bin/dat/images/simple_test.png";
  my $bf = Image::BoxFind->new( { image_file => $image_file,
                                  DEBUG      => $DEBUG,
                                } );

  $bf->set_luminance_threshold( 5000 );

  # start in the middle, over the rectangles
  $bf->set_cursor_x( 50 );
  $bf->set_cursor_y( 0 );

  # params for vertical scan
  $bf->set_spotsize_x( 8 );
  $bf->set_spotsize_y( 3 );

  my $expected_y = 8;
  my $y_value = $bf->scan_down_spotcolor;
  delta_within( $y_value, $expected_y, $tolerance ,"Testing $test_name: vertical to $expected_y" );

  my $increment = 10; # nudge ahead a little to get off the edge (HACK)
  $bf->set_cursor_y( $y_value + $increment );

  # params for horizontal scan
  $bf->set_spotsize_x( 3 );
  $bf->set_spotsize_y( 8 );

  my $expected_x = 124;
  my $x_value = $bf->scan_right_spotcolor;
  delta_within( $x_value, $expected_x, $tolerance ,"Testing $test_name: horizontal to $expected_x" );

  my $step = 6; # step back a bit to get away from the corner (HACK)
  $bf->set_cursor_x( $x_value - $step );

  # params for vertical scan
  $bf->set_spotsize_x( 8 );
  $bf->set_spotsize_y( 3 );

  # again, nudge ahead a little to get off the edge (TODO how did I do without?)
  $y_value = $bf->cursor_y;
  $bf->set_cursor_y( $y_value + 4);

  $expected_y = 26; # lower-right corner of black box

  $y_value = $bf->scan_down_spotcolor;
  delta_within( $y_value, $expected_y, $tolerance, "Testing $test_name: vertical to $expected_y" );

}

# start at:
# 500, 350
#             eyeballed    empirical
#             gimp:        (1st try)
# y border:    360           373    -- missed "white" border, hit text (!)
# x border:    525           524
# y border:    390           390

# Okay: using "edge detect" now, which finds the top edge of the "Cancel"
# button, but effectively moves over the right and bottom edges.
# Entering empirical values.  (Ultimate goal is not *precise* locations)

{
  my $test_name = "scan-turn-and-scan-again with edge detect on";
  my $image_file = "$Bin/dat/images/gimp_open_dialog.png";
  my $bf = Image::BoxFind->new( { image_file  => $image_file,
                                  edge_detect => 1,
                                  DEBUG       => $DEBUG,
                                } );
  my $backup = 3;

  $bf->set_luminance_threshold( 3500 );

  # starting over the "Cancel" button, heading downward

  $bf->set_cursor_x( 480 );
  $bf->set_cursor_y( 350 );

  # params for vertical scan
  $bf->set_spotsize_x( 3 ); #
  $bf->set_spotsize_y( 3 );

  my $expected_y = 359;
  my $y_value = $bf->scan_down_spotcolor;
  delta_within( $y_value, $expected_y, $tolerance ,"Testing $test_name: down to $expected_y" );

  my $dive = 2*$backup;
  $bf->set_cursor_y( $y_value + $dive );

  # params for horizontal scan
  $bf->set_spotsize_x( 3 );
  $bf->set_spotsize_y( 3 ); # no need to be wider

  # Note, effective edge gets fatter on bottom and right side
  # when "edge detect" filter is on.

  my $expected_x = 522;

  my $x_value = $bf->scan_right_spotcolor;

  delta_within( $x_value, $expected_x, $tolerance,"Testing $test_name: right to $expected_x" );

  $bf->set_cursor_x( $x_value - $backup );

  # params for vertical scan
  $bf->set_spotsize_x( 3 ); # no need to be wider
  $bf->set_spotsize_y( 3 );

  $expected_y = 388;

  $y_value = $bf->scan_down_spotcolor;
  delta_within( $y_value, $expected_y, $tolerance, "Testing $test_name: down to $expected_y" );

  # step back up away from the edge.
  $bf->set_cursor_y( $y_value - $backup );


  # params for horizontal scan
  $bf->set_spotsize_x( 3 );
  $bf->set_spotsize_y( 3 ); # no need to be wider

  $x_value = $bf->scan_left_spotcolor;

  $expected_x = 442;
  delta_within( $x_value, $expected_x, $tolerance, "Testing $test_name: left to $expected_x" );

  # step into center, away from leftsize edge
  $bf->set_cursor_x( $x_value + $backup );

  # params for vertical scan
  $bf->set_spotsize_x( 3 ); # no need to be wider
  $bf->set_spotsize_y( 3 );

  $expected_y = 361;  # *almost* back to the beginning (2 pixels short)
  $y_value = $bf->scan_up_spotcolor;
  delta_within( $y_value, $expected_y, $tolerance, "Testing $test_name: up to $expected_y" );
}


     

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