detect_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. package detect
  5. import (
  6. "image"
  7. "image/draw"
  8. "testing"
  9. )
  10. var (
  11. c0 = Classifier{
  12. Feature: []Feature{
  13. Feature{Rect: image.Rect(0, 0, 3, 4), Weight: 4.0},
  14. },
  15. Threshold: 0.2,
  16. Left: 0.8,
  17. Right: 0.2,
  18. }
  19. c1 = Classifier{
  20. Feature: []Feature{
  21. Feature{Rect: image.Rect(3, 4, 4, 5), Weight: 4.0},
  22. },
  23. Threshold: 0.2,
  24. Left: 0.8,
  25. Right: 0.2,
  26. }
  27. c2 = Classifier{
  28. Feature: []Feature{
  29. Feature{Rect: image.Rect(0, 0, 1, 1), Weight: +4.0},
  30. Feature{Rect: image.Rect(0, 0, 2, 2), Weight: -1.0},
  31. },
  32. Threshold: 0.2,
  33. Left: 0.8,
  34. Right: 0.2,
  35. }
  36. )
  37. func TestClassifier(t *testing.T) {
  38. m := image.NewGray(image.Rect(0, 0, 20, 20))
  39. b := m.Bounds()
  40. draw.Draw(m, image.Rect(0, 0, 20, 20), image.White, image.ZP, draw.Src)
  41. draw.Draw(m, image.Rect(3, 4, 4, 5), image.Black, image.ZP, draw.Src)
  42. w := newWindow(m)
  43. pr := newProjector(b, b)
  44. if res := c0.classify(w, pr); res != c0.Right {
  45. t.Errorf("c0 got %f want %f", res, c0.Right)
  46. }
  47. if res := c1.classify(w, pr); res != c1.Left {
  48. t.Errorf("c1 got %f want %f", res, c1.Left)
  49. }
  50. if res := c2.classify(w, pr); res != c1.Left {
  51. t.Errorf("c2 got %f want %f", res, c1.Left)
  52. }
  53. }
  54. func TestClassifierScale(t *testing.T) {
  55. m := image.NewGray(image.Rect(0, 0, 50, 50))
  56. b := m.Bounds()
  57. draw.Draw(m, image.Rect(0, 0, 8, 10), image.White, b.Min, draw.Src)
  58. draw.Draw(m, image.Rect(8, 10, 10, 13), image.Black, b.Min, draw.Src)
  59. w := newWindow(m)
  60. pr := newProjector(b, image.Rect(0, 0, 20, 20))
  61. if res := c0.classify(w, pr); res != c0.Right {
  62. t.Errorf("scaled c0 got %f want %f", res, c0.Right)
  63. }
  64. if res := c1.classify(w, pr); res != c1.Left {
  65. t.Errorf("scaled c1 got %f want %f", res, c1.Left)
  66. }
  67. if res := c2.classify(w, pr); res != c1.Left {
  68. t.Errorf("scaled c2 got %f want %f", res, c1.Left)
  69. }
  70. }