added squash

This commit is contained in:
Lorenz Hohermuth 2024-02-19 19:39:35 +01:00
parent 0579adeb80
commit 612e4a29be
4 changed files with 96 additions and 19 deletions

View File

@ -1,12 +0,0 @@
package image
//func getBighnessMap(map [][]Pixel) [][]float64{}
func (pix Pixel) Brightness() float64 {
R := float64(pix.R)
G := float64(pix.G)
B := float64(pix.B)
A := float64(pix.A)
brightness := (0.2126*R + 0.7152*G + 0.0722*B) /255
return brightness * (A / 255)
}

View File

@ -7,18 +7,13 @@ import (
"os" "os"
) )
type Pixel struct {
R int
G int
B int
A int
}
func ImageToPixles(path string) [][]Pixel { func ImageToPixles(path string) [][]Pixel {
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig) image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
file, err := os.Open(path) file, err := os.Open(path)
check(err) check(err)
defer file.Close() defer file.Close()
return getPixles(file) mat := getPixles(file)
return squashMatrix(mat)
} }
func getPixles(f io.Reader) [][]Pixel { func getPixles(f io.Reader) [][]Pixel {

32
pkg/image/pixel.go Normal file
View File

@ -0,0 +1,32 @@
package image
//func getBighnessMap(map [][]Pixel) [][]float64{}
func (pix Pixel) Brightness() float64 {
R := float64(pix.R)
G := float64(pix.G)
B := float64(pix.B)
A := float64(pix.A)
brightness := (0.2126*R + 0.7152*G + 0.0722*B) /255
return brightness * (A / 255)
}
type Pixel struct {
R int
G int
B int
A int
}
func averagePixel(arr []Pixel) Pixel {
l := len(arr)
var sumR, sumG, sumB, sumA int
for _, v := range arr {
sumR += v.R
sumG += v.G
sumB += v.B
sumA += v.A
}
return Pixel{sumR / l, sumG / l, sumB / l, sumA / l}
}

62
pkg/image/split.go Normal file
View File

@ -0,0 +1,62 @@
package image
var heigthCell int = 2
var widthCell int = 1
func reFormatVertical(matrix [][]Pixel ) [][]Pixel {
heightMatrix := len(matrix)
widthMatrix := len(matrix[0])
newMatrix := makePixleMatrix(widthMatrix, heightMatrix)
for y := range matrix {
for x := range matrix[y] {
newMatrix[x][y] = matrix[y][x]
}
}
return newMatrix
}
func splitArray(arr []Pixel, size int) [][]Pixel {
var out [][]Pixel
var j int
for i := 0; i < len(arr); i += size {
j += size
if j > len(arr) {
j = len(arr)
}
out = append(out, arr[i:j])
}
return out
}
func squashMatrix(pix [][]Pixel) [][]Pixel{
pix = reFormatVertical(pix)
var tensor [][][]Pixel
for _, arr := range pix {
tensor = append(tensor, splitArray(arr, heigthCell))
}
out := makePixleMatrix(len(tensor[0]), len(tensor))
for x := range tensor {
for y := range tensor[x] {
p := averagePixel(tensor[x][y])
out[y][x] = p
}
}
return out
}
type cord struct {
x int
y int
}
func makePixleMatrix(height int, width int) [][]Pixel {
arr := make([][]Pixel, height)
for i := range arr {
arr[i] = make([]Pixel, width)
}
return arr
}