Skip to content

Commit d820d0a

Browse files
committed
Work on Gaussian blur
1 parent df791d3 commit d820d0a

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

in-progress/gaussian-blur-da.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func clampMax[T float64 | int | uint8](value, max T) T {
1515
return value
1616
}
1717

18-
// Gaussian blur: different approach (channels)
18+
// Gaussian blur: different approach (channels) - top 2
1919
func GaussianBlurDA(path string, sigma float64) {
2020
if sigma < 0 {
2121
sigma *= -1
@@ -97,6 +97,7 @@ func GaussianBlurDA(path string, sigma float64) {
9797
result := <-ch
9898
resV = append(resV, result)
9999
if len(resV) == threads {
100+
close(ch)
100101
break
101102
}
102103
}

in-progress/gaussian-blur-daf.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package progress
2+
3+
import (
4+
"math"
5+
"runtime"
6+
"time"
7+
8+
"go-image-processing/utilities"
9+
)
10+
11+
// Gaussian blur: different approach faster (channels) - worst performance
12+
func GaussianBlurDAF(path string, sigma float64) {
13+
if sigma < 0 {
14+
sigma *= -1
15+
}
16+
img, format, openMS, convertMS := open(path)
17+
now := math.Round(float64(time.Now().UnixNano()) / 1000000)
18+
19+
kernel := createKernel(sigma)
20+
kernelLen := len(kernel)
21+
pixLen := len(img.Pix)
22+
width, height := img.Rect.Max.X, img.Rect.Max.Y
23+
temp := make([]uint8, len(img.Pix))
24+
threads := runtime.NumCPU()
25+
26+
signal := make(chan struct{}, threads)
27+
step := 4
28+
29+
processing := func(i int, direction string) {
30+
x, y := getCoordinates(i/4, width)
31+
sumR := 0.0
32+
sumG := 0.0
33+
sumB := 0.0
34+
for k := 0; k < kernelLen; k += 1 {
35+
var px int
36+
if direction == "horizontal" {
37+
px = getPixel(
38+
utilities.MaxMin(x-(kernelLen/2-k), width-1, 0),
39+
y,
40+
width,
41+
)
42+
sumR += float64(img.Pix[px]) * kernel[k]
43+
sumG += float64(img.Pix[px+1]) * kernel[k]
44+
sumB += float64(img.Pix[px+2]) * kernel[k]
45+
} else {
46+
px = getPixel(
47+
x,
48+
utilities.MaxMin(y-(kernelLen/2-k), height-1, 0),
49+
width,
50+
)
51+
sumR += float64(temp[px]) * kernel[k]
52+
sumG += float64(temp[px+1]) * kernel[k]
53+
sumB += float64(temp[px+2]) * kernel[k]
54+
}
55+
}
56+
if direction == "horizontal" {
57+
temp[i] = uint8(utilities.MaxMin(sumR, 255, 0))
58+
temp[i+1] = uint8(utilities.MaxMin(sumG, 255, 0))
59+
temp[i+2] = uint8(utilities.MaxMin(sumB, 255, 0))
60+
} else {
61+
img.Pix[i] = uint8(utilities.MaxMin(sumR, 255, 0))
62+
img.Pix[i+1] = uint8(utilities.MaxMin(sumG, 255, 0))
63+
img.Pix[i+2] = uint8(utilities.MaxMin(sumB, 255, 0))
64+
}
65+
<-signal
66+
}
67+
68+
// horizontal
69+
i := 0
70+
for {
71+
if i >= pixLen {
72+
break
73+
}
74+
signal <- struct{}{}
75+
go processing(i, "horizontal")
76+
i += step
77+
}
78+
79+
// vertical
80+
i = 0
81+
for {
82+
if i >= pixLen {
83+
close(signal)
84+
break
85+
}
86+
signal <- struct{}{}
87+
go processing(i, "vertical")
88+
i += step
89+
}
90+
91+
processMS := int(math.Round(float64(time.Now().UnixNano())/1000000) - now)
92+
saveMS := save(img, format, 1)
93+
sum := openMS + convertMS + processMS + saveMS
94+
println("open", openMS, "convert", convertMS, "process", processMS, "save", saveMS, "sum", sum)
95+
println("threads", threads)
96+
}

in-progress/gaussian-blur-ef.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"go-image-processing/utilities"
1010
)
1111

12-
// Gaussian blur: even faster (sync.WaitGroup)
12+
// Gaussian blur: even faster (sync.WaitGroup) - top 3
1313
func GaussianBlurEF(path string, sigma float64) {
1414
if sigma < 0 {
1515
sigma *= -1

in-progress/gaussian-blur-emf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"go-image-processing/utilities"
1010
)
1111

12-
// Gaussian blur: even more faster (sync.WaitGroup)
12+
// Gaussian blur: even more faster (sync.WaitGroup) - top 1
1313
func GaussianBlurEMF(path string, sigma float64) {
1414
if sigma < 0 {
1515
sigma *= -1
@@ -30,7 +30,7 @@ func GaussianBlurEMF(path string, sigma float64) {
3030

3131
processing := func(start int, direction string) {
3232
defer wg.Done()
33-
end := utilities.MaxMin(start+pixPerThread, pixLen, 0)
33+
end := clampMax(start+pixPerThread, pixLen)
3434
for i := start; i < end; i += 4 {
3535
x, y := getCoordinates(i/4, width)
3636
sumR := 0.0

main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
var FORMAT string
88

99
func main() {
10-
path := "images/10.jpeg"
10+
path := "images/15.jpeg"
1111
// img, f, openMS, convertMS := utilities.OpenFile(path)
1212
// FORMAT = f
1313
// now := math.Round(float64(time.Now().UnixNano()) / 1000000)
@@ -22,11 +22,13 @@ func main() {
2222
// saveMS := utilities.SaveFile("gaussEF-"+name, FORMAT, gaussEF)
2323
// saveMS := utilities.SaveFile("bilateral-"+name, FORMAT, bilateral)
2424

25-
blur := 25.0
2625
// progress.BinaryEFSlices(path, 125)
26+
27+
blur := 10.0
2728
// progress.GaussianBlur(path, blur)
28-
progress.GaussianBlurDA(path, blur)
29-
// progress.GaussianBlurEF(path, 25)
29+
// progress.GaussianBlurDA(path, blur)
30+
progress.GaussianBlurDAF(path, blur)
31+
// progress.GaussianBlurEF(path, blur)
3032
// progress.GaussianBlurEMF(path, blur)
3133

3234
/* Optimized filters */

0 commit comments

Comments
 (0)