github.com/aloncn/graphics-go@v0.0.1/graphics/detect/detect_test.go (about)

     1  // Copyright 2011 The Graphics-Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package detect
     6  
     7  import (
     8  	"image"
     9  	"image/draw"
    10  	"testing"
    11  )
    12  
    13  var (
    14  	c0 = Classifier{
    15  		Feature: []Feature{
    16  			Feature{Rect: image.Rect(0, 0, 3, 4), Weight: 4.0},
    17  		},
    18  		Threshold: 0.2,
    19  		Left:      0.8,
    20  		Right:     0.2,
    21  	}
    22  	c1 = Classifier{
    23  		Feature: []Feature{
    24  			Feature{Rect: image.Rect(3, 4, 4, 5), Weight: 4.0},
    25  		},
    26  		Threshold: 0.2,
    27  		Left:      0.8,
    28  		Right:     0.2,
    29  	}
    30  	c2 = Classifier{
    31  		Feature: []Feature{
    32  			Feature{Rect: image.Rect(0, 0, 1, 1), Weight: +4.0},
    33  			Feature{Rect: image.Rect(0, 0, 2, 2), Weight: -1.0},
    34  		},
    35  		Threshold: 0.2,
    36  		Left:      0.8,
    37  		Right:     0.2,
    38  	}
    39  )
    40  
    41  func TestClassifier(t *testing.T) {
    42  	m := image.NewGray(image.Rect(0, 0, 20, 20))
    43  	b := m.Bounds()
    44  	draw.Draw(m, image.Rect(0, 0, 20, 20), image.White, image.ZP, draw.Src)
    45  	draw.Draw(m, image.Rect(3, 4, 4, 5), image.Black, image.ZP, draw.Src)
    46  	w := newWindow(m)
    47  	pr := newProjector(b, b)
    48  
    49  	if res := c0.classify(w, pr); res != c0.Right {
    50  		t.Errorf("c0 got %f want %f", res, c0.Right)
    51  	}
    52  	if res := c1.classify(w, pr); res != c1.Left {
    53  		t.Errorf("c1 got %f want %f", res, c1.Left)
    54  	}
    55  	if res := c2.classify(w, pr); res != c1.Left {
    56  		t.Errorf("c2 got %f want %f", res, c1.Left)
    57  	}
    58  }
    59  
    60  func TestClassifierScale(t *testing.T) {
    61  	m := image.NewGray(image.Rect(0, 0, 50, 50))
    62  	b := m.Bounds()
    63  	draw.Draw(m, image.Rect(0, 0, 8, 10), image.White, b.Min, draw.Src)
    64  	draw.Draw(m, image.Rect(8, 10, 10, 13), image.Black, b.Min, draw.Src)
    65  	w := newWindow(m)
    66  	pr := newProjector(b, image.Rect(0, 0, 20, 20))
    67  
    68  	if res := c0.classify(w, pr); res != c0.Right {
    69  		t.Errorf("scaled c0 got %f want %f", res, c0.Right)
    70  	}
    71  	if res := c1.classify(w, pr); res != c1.Left {
    72  		t.Errorf("scaled c1 got %f want %f", res, c1.Left)
    73  	}
    74  	if res := c2.classify(w, pr); res != c1.Left {
    75  		t.Errorf("scaled c2 got %f want %f", res, c1.Left)
    76  	}
    77  }