images.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package imageproc
  2. import (
  3. "image"
  4. "image/color"
  5. "golang.org/x/image/draw"
  6. )
  7. var (
  8. ImageNetDefaultMean = [3]float32{0.485, 0.456, 0.406}
  9. ImageNetDefaultSTD = [3]float32{0.229, 0.224, 0.225}
  10. ImageNetStandardMean = [3]float32{0.5, 0.5, 0.5}
  11. ImageNetStandardSTD = [3]float32{0.5, 0.5, 0.5}
  12. ClipDefaultMean = [3]float32{0.48145466, 0.4578275, 0.40821073}
  13. ClipDefaultSTD = [3]float32{0.26862954, 0.26130258, 0.27577711}
  14. )
  15. const (
  16. ResizeBilinear = iota
  17. ResizeNearestNeighbor
  18. ResizeApproxBilinear
  19. ResizeCatmullrom
  20. )
  21. // Composite returns an image with the alpha channel removed by drawing over a white background.
  22. func Composite(img image.Image) image.Image {
  23. dst := image.NewRGBA(img.Bounds())
  24. white := color.RGBA{255, 255, 255, 255}
  25. draw.Draw(dst, dst.Bounds(), &image.Uniform{white}, image.Point{}, draw.Src)
  26. draw.Draw(dst, dst.Bounds(), img, img.Bounds().Min, draw.Over)
  27. return dst
  28. }
  29. // Resize returns an image which has been scaled to a new size.
  30. func Resize(img image.Image, newSize image.Point, method int) image.Image {
  31. dst := image.NewRGBA(image.Rect(0, 0, newSize.X, newSize.Y))
  32. kernels := map[int]draw.Interpolator{
  33. ResizeBilinear: draw.BiLinear,
  34. ResizeNearestNeighbor: draw.NearestNeighbor,
  35. ResizeApproxBilinear: draw.ApproxBiLinear,
  36. ResizeCatmullrom: draw.CatmullRom,
  37. }
  38. kernel, ok := kernels[method]
  39. if !ok {
  40. panic("no resizing method found")
  41. }
  42. kernel.Scale(dst, dst.Rect, img, img.Bounds(), draw.Over, nil)
  43. return dst
  44. }
  45. // Normalize returns a slice of float32 containing each of the r, g, b values for an image normalized around a value.
  46. func Normalize(img image.Image, mean, std [3]float32, rescale bool, channelFirst bool) []float32 {
  47. var pixelVals []float32
  48. bounds := img.Bounds()
  49. if channelFirst {
  50. var rVals, gVals, bVals []float32
  51. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  52. for x := bounds.Min.X; x < bounds.Max.X; x++ {
  53. c := img.At(x, y)
  54. r, g, b, _ := c.RGBA()
  55. var rVal, gVal, bVal float32
  56. if rescale {
  57. rVal = float32(r>>8) / 255.0
  58. gVal = float32(g>>8) / 255.0
  59. bVal = float32(b>>8) / 255.0
  60. }
  61. rVal = (rVal - mean[0]) / std[0]
  62. gVal = (gVal - mean[1]) / std[1]
  63. bVal = (bVal - mean[2]) / std[2]
  64. rVals = append(rVals, rVal)
  65. gVals = append(gVals, gVal)
  66. bVals = append(bVals, bVal)
  67. }
  68. }
  69. pixelVals = append(pixelVals, rVals...)
  70. pixelVals = append(pixelVals, gVals...)
  71. pixelVals = append(pixelVals, bVals...)
  72. } else {
  73. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  74. for x := bounds.Min.X; x < bounds.Max.X; x++ {
  75. c := img.At(x, y)
  76. r, g, b, _ := c.RGBA()
  77. var rVal, gVal, bVal float32
  78. if rescale {
  79. rVal = float32(r>>8) / 255.0
  80. gVal = float32(g>>8) / 255.0
  81. bVal = float32(b>>8) / 255.0
  82. }
  83. rVal = (rVal - mean[0]) / std[0]
  84. gVal = (gVal - mean[1]) / std[1]
  85. bVal = (bVal - mean[2]) / std[2]
  86. pixelVals = append(pixelVals, rVal, gVal, bVal)
  87. }
  88. }
  89. }
  90. return pixelVals
  91. }