-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
142 lines (114 loc) · 2.84 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"image"
"image/png" // register the PNG format with the image package
"os"
"image/color"
"time"
"fmt"
)
func makeGray(src image.Image) (*image.Gray) {
// Create a new grayscale image
bounds := src.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
rectangle := image.Rect(0, 0, w, h)
gray := image.NewGray(rectangle)
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
oldColor := src.At(x, y)
grayColor := color.GrayModel.Convert(oldColor)
gray.Set(x, y, grayColor)
}
}
return gray
}
func main() {
start := time.Now()
infile, err := os.Open("test1.png")
if err != nil {
// replace this with real error handling
panic(err)
}
defer infile.Close()
// Decode will figure out what type of image is in the file on its own.
// We just have to be sure all the image packages we want are imported.
src, _, err := image.Decode(infile)
if err != nil {
// replace this with real error handling
panic(err)
}
gray := makeGray(src)
// create integral image
integral := integralImage(gray)
min, max := -1, 2
kernel := [][]int {
{min, min, min},
{max, max, max},
{min, min, min},
}
bounds := gray.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
rectangle := image.Rect(0, 0, w, h)
final := image.NewGray(rectangle)
kernelLength := len(kernel)
for y := 0; y < h - kernelLength; y++ {
for x := 0; x < w - kernelLength; x++ {
val := convolutionAt(kernel, gray, x, y)
final.Set(x, y, color.Gray{uint8(val)})
}
}
// Encode the grayscale image to the output file
outfile, err := os.Create("output.png")
if err != nil {
// replace this with real error handling
panic(err)
}
defer outfile.Close()
png.Encode(outfile, final)
elapsed := time.Since(start)
fmt.Printf("shit took about: %s", elapsed)
}
func integralImage(img *image.Gray) [][]int {
bounds := img.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
integral := make([][]int, w)
for i := range a {
a[i] = make([]int, h)
}
integral[0][0] = int(img.GrayAt(0, 0))
// First pass
for x := 1; x < w; x++ {
integral[x][0] = integral[x-1][0] + int(img.GrayAt(x, 0))
}
// First pass
for y := 1; y < h; y++ {
integral[0][y] = integral[0][y-1] + int(img.GrayAt(y, 0))
}
for y := 1; y < h; y++ {
for x := 1; x < w; x++ {
diag := integral[x-1][y-1]
top := integral[x][y-1]
left := integral[x-1][y]
me := int(img.GrayAt(x, y))
integral[x][y] = top + left + me - diag
}
}
return integral
}
func convolutionAt(kernel [][]int, img *image.Gray, centerX int, centerY int) int {
sum := 0
kernelTotal := 0
kernelLength := len(kernel)
for y := 0; y < kernelLength; y++ {
for x := 0; x < kernelLength; x++ {
targetPixel := img.GrayAt(centerX + x, centerY + y)
sum += kernel[y][x] * int(targetPixel.Y)
kernelTotal += kernel[y][x]
}
}
if kernelTotal == 0 {
kernelTotal = 1
}
result := sum / kernelTotal
return result
}