-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.go
More file actions
143 lines (130 loc) · 3.38 KB
/
histogram.go
File metadata and controls
143 lines (130 loc) · 3.38 KB
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
143
package main
import (
"fmt"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"image"
"image/color"
"image/png"
"os"
"strconv"
)
// generate random data for bar chart
func generateBarItems2() []opts.BarData {
items := make([]opts.BarData, 256)
for i := 0; i < 256; i++ {
items[i] = opts.BarData{Value: 0}
}
file, err := os.Open("./Output/output.png")
if err != nil {
panic(err)
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
panic(err.Error())
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
//Image := image.NewGray(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
r, g, b, _ := img.At(x, y).RGBA()
gray := 0.21*float64(r) + 0.72*float64(g) + 0.07*float64(b)
colour := color.Gray{Y: uint8(gray / 256)}
items[(colour.Y)].Value = items[colour.Y].Value.(int) + 1
//Image.Set(x, y, gray)
}
}
return items
}
// generate random data for bar chart
func generateBarItems() []opts.BarData {
items := make([]opts.BarData, 256)
for i := 0; i < 256; i++ {
items[i] = opts.BarData{Value: 0}
}
file, err := os.Open("./Input/img.png")
if err != nil {
panic(err)
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
panic(err.Error())
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
//Image := image.NewGray(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
r, g, b, _ := img.At(x, y).RGBA()
gray := 0.21*float64(r) + 0.72*float64(g) + 0.07*float64(b)
colour := color.Gray{Y: uint8(gray / 256)}
items[(colour.Y)].Value = items[colour.Y].Value.(int) + 1
//Image.Set(x, y, gray)
}
}
return items
}
func equalize() []opts.BarData {
file, err := os.Open("./Input/img.png")
if err != nil {
panic(err)
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
panic(err.Error())
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
items := generateBarItems()
for i := 1; i < 256; i++ {
items[i] = opts.BarData{Value: items[i].Value.(int) + items[i-1].Value.(int)}
}
for i := 0; i < 256; i++ {
items[i] = opts.BarData{Value: uint8(255 * (float64(items[i].Value.(int)) / float64(width*height)))}
fmt.Println(items[i].Value)
}
Image := image.NewGray(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
r, g, b, _ := img.At(x, y).RGBA()
gray := 0.21*float64(r) + 0.72*float64(g) + 0.07*float64(b)
colour := color.Gray{Y: uint8(gray / 256)}
colour.Y = items[colour.Y].Value.(uint8)
Image.Set(x, y, colour)
}
}
outFile, err := os.Create("./Output/output.png")
if err != nil {
panic(err.Error())
}
defer outFile.Close()
err = png.Encode(outFile, Image)
if err != nil {
panic(err.Error())
}
return items
}
func createBarChart() {
// create a new bar instance
bar := charts.NewBar()
// Set global options
bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
Title: "Histogram",
Subtitle: "Original Image VS Equalized Image",
}))
label := []string{}
for i := 0; i < 256; i++ {
label = append(label, strconv.Itoa(i))
}
equalize()
//fmt.Println(generateBarItems())
// Put data into instance
bar.SetXAxis(label).
AddSeries("Original", generateBarItems()).AddSeries("Equalized", generateBarItems2())
f, _ := os.Create("bar.html")
_ = bar.Render(f)
}