-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.go
72 lines (63 loc) · 1.61 KB
/
tools.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
package main
import (
"fmt"
"image"
"image/draw"
"math"
"math/rand"
"os"
"time"
)
// 把样本图片变成image.Image
func readPic(path string) image.Image {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
img, fmtName, err := image.Decode(f)
if err != nil {
panic(err)
}
fmt.Printf("Name: %v, Bounds: %+v, Color: %+v", fmtName, img.Bounds(), img.ColorModel())
return img
}
func RandFromRangeInt64(min int64, max int64) int64 {
rand.Seed(time.Now().UnixNano())
return rand.Int63n(max-min) + min
}
// NormalFloat64 正态分布公式
func NormalFloat64(x int64, miu int64, sigma int64) float64 {
randomNormal := 1 / (math.Sqrt(2*math.Pi) * float64(sigma)) * math.Pow(math.E, -math.Pow(float64(x-miu), 2)/(2*math.Pow(float64(sigma), 2)))
//注意下是x-miu,我看网上好多写的是miu-miu是不对的
return randomNormal
}
// RandomNormalInt64 正态分布随机数生产器:min:最小值,max:最大值,miu:期望值(均值),sigma:方差
func RandomNormalInt64(min int64, max int64, miu int64, sigma int64) (bool, int64) {
if min >= max {
return false, 0
}
if miu < min {
miu = min
}
if miu > max {
miu = max
}
var x int64
var y, dScope float64
for {
x = RandFromRangeInt64(min, max)
y = NormalFloat64(x, miu, sigma) * 100000
dScope = float64(RandFromRangeInt64(0, int64(NormalFloat64(miu, miu, sigma)*100000)))
//注意下传的是两个miu
if dScope <= y {
break
}
}
return true, x
}
func jpg2RGBA(img image.Image) *image.RGBA {
tmp := image.NewRGBA(img.Bounds())
draw.Draw(tmp, img.Bounds(), img, img.Bounds().Min, draw.Src)
return tmp
}