integral_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "bytes"
  7. "fmt"
  8. "image"
  9. "testing"
  10. )
  11. type integralTest struct {
  12. x int
  13. y int
  14. src []uint8
  15. res []uint8
  16. }
  17. var integralTests = []integralTest{
  18. {
  19. 1, 1,
  20. []uint8{0x01},
  21. []uint8{0x01},
  22. },
  23. {
  24. 2, 2,
  25. []uint8{
  26. 0x01, 0x02,
  27. 0x03, 0x04,
  28. },
  29. []uint8{
  30. 0x01, 0x03,
  31. 0x04, 0x0a,
  32. },
  33. },
  34. {
  35. 4, 4,
  36. []uint8{
  37. 0x02, 0x03, 0x00, 0x01,
  38. 0x01, 0x02, 0x01, 0x05,
  39. 0x01, 0x01, 0x01, 0x01,
  40. 0x01, 0x01, 0x01, 0x01,
  41. },
  42. []uint8{
  43. 0x02, 0x05, 0x05, 0x06,
  44. 0x03, 0x08, 0x09, 0x0f,
  45. 0x04, 0x0a, 0x0c, 0x13,
  46. 0x05, 0x0c, 0x0f, 0x17,
  47. },
  48. },
  49. }
  50. func sprintBox(box []byte, width, height int) string {
  51. buf := bytes.NewBuffer(nil)
  52. i := 0
  53. for y := 0; y < height; y++ {
  54. for x := 0; x < width; x++ {
  55. fmt.Fprintf(buf, " 0x%02x,", box[i])
  56. i++
  57. }
  58. buf.WriteByte('\n')
  59. }
  60. return buf.String()
  61. }
  62. func TestIntegral(t *testing.T) {
  63. for i, oc := range integralTests {
  64. src := &image.Gray{
  65. Pix: oc.src,
  66. Stride: oc.x,
  67. Rect: image.Rect(0, 0, oc.x, oc.y),
  68. }
  69. dst, _ := newIntegrals(src)
  70. res := make([]byte, len(dst.pix))
  71. for i, p := range dst.pix {
  72. res[i] = byte(p)
  73. }
  74. if !bytes.Equal(res, oc.res) {
  75. got := sprintBox(res, oc.x, oc.y)
  76. want := sprintBox(oc.res, oc.x, oc.y)
  77. t.Errorf("%d: got\n%s\n want\n%s", i, got, want)
  78. }
  79. }
  80. }
  81. func TestIntegralSum(t *testing.T) {
  82. src := &image.Gray{
  83. Pix: []uint8{
  84. 0x02, 0x03, 0x00, 0x01, 0x03,
  85. 0x01, 0x02, 0x01, 0x05, 0x05,
  86. 0x01, 0x01, 0x01, 0x01, 0x02,
  87. 0x01, 0x01, 0x01, 0x01, 0x07,
  88. 0x02, 0x01, 0x00, 0x03, 0x01,
  89. },
  90. Stride: 5,
  91. Rect: image.Rect(0, 0, 5, 5),
  92. }
  93. img, _ := newIntegrals(src)
  94. type sumTest struct {
  95. rect image.Rectangle
  96. sum uint64
  97. }
  98. var sumTests = []sumTest{
  99. {image.Rect(0, 0, 1, 1), 2},
  100. {image.Rect(0, 0, 2, 1), 5},
  101. {image.Rect(0, 0, 1, 3), 4},
  102. {image.Rect(1, 1, 3, 3), 5},
  103. {image.Rect(2, 2, 4, 4), 4},
  104. {image.Rect(4, 3, 5, 5), 8},
  105. {image.Rect(2, 4, 3, 5), 0},
  106. }
  107. for _, st := range sumTests {
  108. s := img.sum(st.rect)
  109. if s != st.sum {
  110. t.Errorf("%v: got %d want %d", st.rect, s, st.sum)
  111. return
  112. }
  113. }
  114. }
  115. func TestIntegralSubImage(t *testing.T) {
  116. m0 := &image.Gray{
  117. Pix: []uint8{
  118. 0x02, 0x03, 0x00, 0x01, 0x03,
  119. 0x01, 0x02, 0x01, 0x05, 0x05,
  120. 0x01, 0x04, 0x01, 0x01, 0x02,
  121. 0x01, 0x02, 0x01, 0x01, 0x07,
  122. 0x02, 0x01, 0x09, 0x03, 0x01,
  123. },
  124. Stride: 5,
  125. Rect: image.Rect(0, 0, 5, 5),
  126. }
  127. b := image.Rect(1, 1, 4, 4)
  128. m1 := m0.SubImage(b)
  129. mi0, _ := newIntegrals(m0)
  130. mi1, _ := newIntegrals(m1)
  131. sum0 := mi0.sum(b)
  132. sum1 := mi1.sum(b)
  133. if sum0 != sum1 {
  134. t.Errorf("b got %d want %d", sum0, sum1)
  135. }
  136. r0 := image.Rect(2, 2, 4, 4)
  137. sum0 = mi0.sum(r0)
  138. sum1 = mi1.sum(r0)
  139. if sum0 != sum1 {
  140. t.Errorf("r0 got %d want %d", sum1, sum0)
  141. }
  142. }