-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.go
95 lines (74 loc) · 2.53 KB
/
preprocess.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package yolotriton
import (
"image"
"image/color"
"image/draw"
"math"
"github.com/nfnt/resize"
)
func resizeImage(img image.Image, width, heigth uint) image.Image {
return resize.Resize(width, heigth, img, resize.Lanczos3)
}
func pixelRGBA(c color.Color) (r, g, b, a uint32) {
r, g, b, a = c.RGBA()
return r >> 8, g >> 8, b >> 8, a >> 8
}
func imageToFloat32Slice(img image.Image) []float32 {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
inputContents := make([]float32, width*height*3)
idx := 0
offset := (height * width)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
pixel := img.At(x, y)
r, g, b, _ := pixelRGBA(pixel)
// Normalize the color values to the range [0, 1]
floatR := float32(r) / 255
floatG := float32(g) / 255
floatB := float32(b) / 255
inputContents[idx] = floatR
inputContents[offset+idx] = floatG
inputContents[2*offset+idx] = floatB
idx++
}
}
return inputContents
}
func imageToUint32Slice(img image.Image) []uint32 {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
inputContents := make([]uint32, width*height*3)
idx := 0
offset := (height * width)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
pixel := img.At(x, y)
r, g, b, _ := pixelRGBA(pixel)
inputContents[idx] = r
inputContents[offset+idx] = g
inputContents[2*offset+idx] = b
idx++
}
}
return inputContents
}
func padImageToCenterWithGray(originalImage image.Image, targetWidth, targetHeight int, grayValue uint8) (image.Image, int, int) {
// Calculate the dimensions of the original image
originalWidth := originalImage.Bounds().Dx()
originalHeight := originalImage.Bounds().Dy()
// Calculate the padding dimensions
padWidth := targetWidth - originalWidth
padHeight := targetHeight - originalHeight
// Create a new RGBA image with the desired dimensions and fill it with gray
paddedImage := image.NewRGBA(image.Rect(0, 0, targetWidth, targetHeight))
grayColor := color.RGBA{grayValue, grayValue, grayValue, 255}
draw.Draw(paddedImage, paddedImage.Bounds(), &image.Uniform{grayColor}, image.Point{}, draw.Src)
// Calculate the position to paste the original image in the center
xOffset := int(math.Floor(float64(padWidth) / 2))
yOffset := int(math.Floor(float64(padHeight) / 2))
// Paste the original image onto the padded image
pasteRect := image.Rect(xOffset, yOffset, xOffset+originalWidth, yOffset+originalHeight)
draw.Draw(paddedImage, pasteRect, originalImage, image.Point{}, draw.Over)
return paddedImage, xOffset, yOffset
}