1
0

convolution_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package imaging
  2. import (
  3. "image"
  4. "testing"
  5. )
  6. func TestConvolve3x3(t *testing.T) {
  7. testCases := []struct {
  8. name string
  9. src image.Image
  10. kernel [9]float64
  11. options *ConvolveOptions
  12. want *image.NRGBA
  13. }{
  14. {
  15. "Convolve3x3 0x0",
  16. &image.NRGBA{
  17. Rect: image.Rect(0, 0, 0, 0),
  18. Stride: 0,
  19. Pix: []uint8{},
  20. },
  21. [9]float64{
  22. 0, 0, 0,
  23. 0, 1, 0,
  24. 0, 0, 0,
  25. },
  26. nil,
  27. &image.NRGBA{Rect: image.Rect(0, 0, 0, 0)},
  28. },
  29. {
  30. "Convolve3x3 4x4 identity",
  31. &image.NRGBA{
  32. Rect: image.Rect(-1, -1, 3, 3),
  33. Stride: 4 * 4,
  34. Pix: []uint8{
  35. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  36. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  37. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  38. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  39. },
  40. },
  41. [9]float64{
  42. 0, 0, 0,
  43. 0, 1, 0,
  44. 0, 0, 0,
  45. },
  46. nil,
  47. &image.NRGBA{
  48. Rect: image.Rect(0, 0, 4, 4),
  49. Stride: 4 * 4,
  50. Pix: []uint8{
  51. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  52. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  53. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  54. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  55. },
  56. },
  57. },
  58. {
  59. "Convolve3x3 4x4 abs",
  60. &image.NRGBA{
  61. Rect: image.Rect(-1, -1, 3, 3),
  62. Stride: 4 * 4,
  63. Pix: []uint8{
  64. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  65. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  66. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  67. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  68. },
  69. },
  70. [9]float64{
  71. 0, 0, 0,
  72. 0, -1, 0,
  73. 0, 0, 0,
  74. },
  75. &ConvolveOptions{Abs: true},
  76. &image.NRGBA{
  77. Rect: image.Rect(0, 0, 4, 4),
  78. Stride: 4 * 4,
  79. Pix: []uint8{
  80. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  81. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  82. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  83. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  84. },
  85. },
  86. },
  87. {
  88. "Convolve3x3 4x4 bias",
  89. &image.NRGBA{
  90. Rect: image.Rect(-1, -1, 3, 3),
  91. Stride: 4 * 4,
  92. Pix: []uint8{
  93. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  94. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  95. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  96. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  97. },
  98. },
  99. [9]float64{
  100. 0, 0, 0,
  101. 0, 1, 0,
  102. 0, 0, 0,
  103. },
  104. &ConvolveOptions{Bias: 0x10},
  105. &image.NRGBA{
  106. Rect: image.Rect(0, 0, 4, 4),
  107. Stride: 4 * 4,
  108. Pix: []uint8{
  109. 0x10, 0x11, 0x12, 0x03, 0x14, 0x15, 0x16, 0x07, 0x18, 0x19, 0x1a, 0x0b, 0x1c, 0x1d, 0x1e, 0x0f,
  110. 0x20, 0x21, 0x22, 0x13, 0x24, 0x25, 0x26, 0x17, 0x28, 0x29, 0x2a, 0x1b, 0x2c, 0x2d, 0x2e, 0x1f,
  111. 0x30, 0x31, 0x32, 0x23, 0x34, 0x35, 0x36, 0x27, 0x38, 0x39, 0x3a, 0x2b, 0x3c, 0x3d, 0x3e, 0x2f,
  112. 0x40, 0x41, 0x42, 0x33, 0x44, 0x45, 0x46, 0x37, 0x48, 0x49, 0x4a, 0x3b, 0x4c, 0x4d, 0x4e, 0x3f,
  113. },
  114. },
  115. },
  116. {
  117. "Convolve3x3 4x4 norm",
  118. &image.NRGBA{
  119. Rect: image.Rect(-1, -1, 3, 3),
  120. Stride: 4 * 4,
  121. Pix: []uint8{
  122. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  123. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  124. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  125. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  126. },
  127. },
  128. [9]float64{
  129. 1, 1, 1,
  130. 1, 1, 1,
  131. 1, 1, 1,
  132. },
  133. &ConvolveOptions{Normalize: true},
  134. &image.NRGBA{
  135. Rect: image.Rect(0, 0, 4, 4),
  136. Stride: 4 * 4,
  137. Pix: []uint8{
  138. 0x07, 0x08, 0x09, 0x03, 0x09, 0x0a, 0x0b, 0x07, 0x0d, 0x0e, 0x0f, 0x0b, 0x10, 0x11, 0x12, 0x0f,
  139. 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1b, 0x1c, 0x1d, 0x1f,
  140. 0x21, 0x22, 0x23, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2b, 0x2c, 0x2d, 0x2f,
  141. 0x2c, 0x2d, 0x2e, 0x33, 0x2f, 0x30, 0x31, 0x37, 0x33, 0x34, 0x35, 0x3b, 0x35, 0x36, 0x37, 0x3f,
  142. },
  143. },
  144. },
  145. {
  146. "Convolve3x3 3x3 laplacian",
  147. &image.NRGBA{
  148. Rect: image.Rect(-1, -1, 2, 2),
  149. Stride: 3 * 4,
  150. Pix: []uint8{
  151. 0x00, 0x01, 0x01, 0xff, 0x00, 0x01, 0x02, 0xff, 0x00, 0x01, 0x03, 0xff,
  152. 0x00, 0x01, 0x04, 0xff, 0x10, 0x10, 0x10, 0xff, 0x00, 0x01, 0x05, 0xff,
  153. 0x00, 0x01, 0x06, 0xff, 0x00, 0x01, 0x07, 0xff, 0x00, 0x01, 0x08, 0xff,
  154. },
  155. },
  156. [9]float64{
  157. -1, -1, -1,
  158. -1, 8, -1,
  159. -1, -1, -1,
  160. },
  161. nil,
  162. &image.NRGBA{
  163. Rect: image.Rect(0, 0, 3, 3),
  164. Stride: 3 * 4,
  165. Pix: []uint8{
  166. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
  167. 0x00, 0x00, 0x00, 0xff, 0x80, 0x78, 0x5c, 0xff, 0x00, 0x00, 0x00, 0xff,
  168. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
  169. },
  170. },
  171. },
  172. }
  173. for _, tc := range testCases {
  174. t.Run(tc.name, func(t *testing.T) {
  175. got := Convolve3x3(tc.src, tc.kernel, tc.options)
  176. if !compareNRGBA(got, tc.want, 0) {
  177. t.Fatalf("got result %#v want %#v", got, tc.want)
  178. }
  179. })
  180. }
  181. }
  182. func TestConvolve5x5(t *testing.T) {
  183. testCases := []struct {
  184. name string
  185. src image.Image
  186. kernel [25]float64
  187. options *ConvolveOptions
  188. want *image.NRGBA
  189. }{
  190. {
  191. "Convolve5x5 4x4 translate",
  192. &image.NRGBA{
  193. Rect: image.Rect(-1, -1, 3, 3),
  194. Stride: 4 * 4,
  195. Pix: []uint8{
  196. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  197. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  198. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  199. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  200. },
  201. },
  202. [25]float64{
  203. 0, 0, 0, 0, 0,
  204. 0, 0, 0, 0, 0,
  205. 0, 0, 0, 0, 0,
  206. 0, 0, 0, 0, 0,
  207. 0, 0, 0, 0, 1,
  208. },
  209. nil,
  210. &image.NRGBA{
  211. Rect: image.Rect(0, 0, 4, 4),
  212. Stride: 4 * 4,
  213. Pix: []uint8{
  214. 0x28, 0x29, 0x2a, 0x03, 0x2c, 0x2d, 0x2e, 0x07, 0x2c, 0x2d, 0x2e, 0x0b, 0x2c, 0x2d, 0x2e, 0x0f,
  215. 0x38, 0x39, 0x3a, 0x13, 0x3c, 0x3d, 0x3e, 0x17, 0x3c, 0x3d, 0x3e, 0x1b, 0x3c, 0x3d, 0x3e, 0x1f,
  216. 0x38, 0x39, 0x3a, 0x23, 0x3c, 0x3d, 0x3e, 0x27, 0x3c, 0x3d, 0x3e, 0x2b, 0x3c, 0x3d, 0x3e, 0x2f,
  217. 0x38, 0x39, 0x3a, 0x33, 0x3c, 0x3d, 0x3e, 0x37, 0x3c, 0x3d, 0x3e, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  218. },
  219. },
  220. },
  221. }
  222. for _, tc := range testCases {
  223. t.Run(tc.name, func(t *testing.T) {
  224. got := Convolve5x5(tc.src, tc.kernel, tc.options)
  225. if !compareNRGBA(got, tc.want, 0) {
  226. t.Fatalf("got result %#v want %#v", got, tc.want)
  227. }
  228. })
  229. }
  230. }
  231. func TestNormalizeKernel(t *testing.T) {
  232. testCases := []struct {
  233. name string
  234. kernel []float64
  235. want []float64
  236. }{
  237. {
  238. name: "positive sum",
  239. kernel: []float64{
  240. 2, 0, 2,
  241. 0, 2, 0,
  242. 2, 0, 2,
  243. },
  244. want: []float64{
  245. 0.2, 0, 0.2,
  246. 0, 0.2, 0,
  247. 0.2, 0, 0.2,
  248. },
  249. },
  250. {
  251. name: "negative sum",
  252. kernel: []float64{
  253. -2, 0, -2,
  254. 2, 2, 2,
  255. -2, 0, -2,
  256. },
  257. want: []float64{
  258. 1, 0, 1,
  259. -1, -1, -1,
  260. 1, 0, 1,
  261. },
  262. },
  263. {
  264. name: "zero sum",
  265. kernel: []float64{
  266. 0, 2, 0,
  267. 2, 0, -2,
  268. 0, -2, 0,
  269. },
  270. want: []float64{
  271. 0, 0.5, 0,
  272. 0.5, 0, -0.5,
  273. 0, -0.5, 0,
  274. },
  275. },
  276. {
  277. name: "all zero",
  278. kernel: []float64{
  279. 0, 0, 0,
  280. 0, 0, 0,
  281. 0, 0, 0,
  282. },
  283. want: []float64{
  284. 0, 0, 0,
  285. 0, 0, 0,
  286. 0, 0, 0,
  287. },
  288. },
  289. }
  290. for _, tc := range testCases {
  291. t.Run(tc.name, func(t *testing.T) {
  292. normalizeKernel(tc.kernel)
  293. for i := range tc.kernel {
  294. if tc.kernel[i] != tc.want[i] {
  295. t.Fatalf("got kernel %v want %v", tc.kernel, tc.want)
  296. }
  297. }
  298. })
  299. }
  300. }
  301. func BenchmarkConvolve3x3(b *testing.B) {
  302. b.ReportAllocs()
  303. for i := 0; i < b.N; i++ {
  304. Convolve3x3(
  305. testdataBranchesJPG,
  306. [9]float64{
  307. -1, -1, 0,
  308. -1, 0, 1,
  309. 0, 1, 1,
  310. },
  311. nil,
  312. )
  313. }
  314. }
  315. func BenchmarkConvolve5x5(b *testing.B) {
  316. b.ReportAllocs()
  317. for i := 0; i < b.N; i++ {
  318. Convolve5x5(
  319. testdataBranchesJPG,
  320. [25]float64{
  321. -1, -1, -1, -1, 0,
  322. -1, -1, -1, 0, 1,
  323. -1, -1, 0, 1, 1,
  324. -1, 0, 1, 1, 1,
  325. 0, 1, 1, 1, 1,
  326. },
  327. nil,
  328. )
  329. }
  330. }