/
main.go
187 lines (154 loc) · 4.95 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"log"
"math"
"os"
"path/filepath"
"strconv"
fatihColor "github.com/fatih/color"
"golang.org/x/image/draw"
)
func main() {
fmt.Print("CHALKBOARDIMAGE: by Dave Duke ")
fatihColor.New(fatihColor.FgHiGreen).Print("dave@daveduke.co.uk")
fmt.Println()
// Check if an image file path is provided as a command-line argument
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <input_image_path> [threshold] [thickness] [-invert]")
return
}
// Get the input image file path from the command-line argument
inputFileName := os.Args[1]
// Process the filename for spaces (if any) by using filepath.Glob
files, err := filepath.Glob(inputFileName)
if err != nil {
log.Fatalf("Error processing file path: %s", err)
}
if len(files) < 1 {
fmt.Println("No image files found.")
return
}
inputFileName = files[0]
// Set default threshold and thickness values
threshold := 20
thickness := 0
// Check if threshold and thickness are provided as command-line arguments
if len(os.Args) >= 4 {
// Parse the threshold value
threshold, err = strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Invalid threshold value. Using default (50).")
threshold = 50
}
// Parse the thickness value
thickness, err = strconv.Atoi(os.Args[3])
if err != nil {
fmt.Println("Invalid thickness value. Using default (1).")
thickness = 1
}
}
// Check if the -invert flag is provided
invert := false
for _, arg := range os.Args[1:] {
if arg == "-invert" {
invert = true
break
}
}
// Replace "output.png" with the path where you want to save the output mask.
outputFileName := "output.png"
// Read the input image file
inputImage, err := readImage(inputFileName)
if err != nil {
log.Fatalf("Error reading image: %s", err)
}
// Create a grayscale version of the input image (if needed)
grayImage := image.NewGray(inputImage.Bounds())
draw.Draw(grayImage, grayImage.Bounds(), inputImage, image.Point{}, draw.Src)
// Apply edge detection or contour finding algorithm here to create the mask
mask := createMask(grayImage, threshold, thickness)
// Invert the mask if the -invert flag is provided
if invert {
invertImage(mask)
}
// Save the mask to an output file
if err := saveMask(outputFileName, mask); err != nil {
log.Fatalf("Error saving mask: %s", err)
}
fmt.Println("Mask created and saved successfully.")
}
// readImage reads an image file and returns the decoded image.
func readImage(fileName string) (image.Image, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return img, nil
}
// createMask applies an edge detection or contour finding algorithm to create a mask.
// Replace this function with your own image processing algorithm to generate the mask.
func createMask(img *image.Gray, threshold, thickness int) *image.Gray {
bounds := img.Bounds()
mask := image.NewGray(bounds)
for y := bounds.Min.Y + 1; y < bounds.Max.Y-1; y++ {
for x := bounds.Min.X + 1; x < bounds.Max.X-1; x++ {
// Calculate the gradient in the x and y directions
dx := int(img.GrayAt(x+1, y).Y) - int(img.GrayAt(x-1, y).Y)
dy := int(img.GrayAt(x, y+1).Y) - int(img.GrayAt(x, y-1).Y)
// Calculate the gradient magnitude
gradient := uint8(math.Sqrt(float64(dx*dx + dy*dy)))
// Check if the gradient is greater than the threshold or is larger than its neighbors
if int(gradient) > threshold ||
int(gradient) > int(img.GrayAt(x-1, y-1).Y)+threshold ||
int(gradient) > int(img.GrayAt(x, y-1).Y)+threshold ||
int(gradient) > int(img.GrayAt(x+1, y-1).Y)+threshold ||
int(gradient) > int(img.GrayAt(x-1, y).Y)+threshold ||
int(gradient) > int(img.GrayAt(x+1, y).Y)+threshold ||
int(gradient) > int(img.GrayAt(x-1, y+1).Y)+threshold ||
int(gradient) > int(img.GrayAt(x, y+1).Y)+threshold ||
int(gradient) > int(img.GrayAt(x+1, y+1).Y)+threshold {
for i := y - thickness; i <= y+thickness; i++ {
for j := x - thickness; j <= x+thickness; j++ {
if i >= bounds.Min.Y && i < bounds.Max.Y && j >= bounds.Min.X && j < bounds.Max.X {
// Increase the pixel intensity to make the lines brighter
mask.SetGray(j, i, color.Gray{Y: 255})
}
}
}
}
}
}
return mask
}
// saveMask saves the mask image to the specified file.
func saveMask(fileName string, mask *image.Gray) error {
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
if err := png.Encode(file, mask); err != nil {
return err
}
return nil
}
// invertImage inverts the pixel intensities of the given grayscale image.
func invertImage(img *image.Gray) {
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
oldGray := img.GrayAt(x, y)
newGray := color.Gray{Y: 255 - oldGray.Y} // Invert the pixel intensity
img.SetGray(x, y, newGray)
}
}
}