goimagehash/transforms/pixels.go

39 lines
888 B
Go
Raw Normal View History

2017-07-28 10:18:24 -07:00
// Copyright 2017 The goimagehash Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package transforms
import (
"image"
)
// Convert RGB to a gray scale flatten array.
func Rgb2Gray(colorImg image.Image) [][]float64 {
bounds := colorImg.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
pixels := make([][]float64, h)
for i := range pixels {
pixels[i] = make([]float64, w)
for j := range pixels[i] {
color := colorImg.At(j, i)
r, g, b, _ := color.RGBA()
lum := 0.299*float64(r/257) + 0.587*float64(g/257) + 0.114*float64(b/256)
pixels[i][j] = lum
}
}
return pixels
}
func FlattenPixels(pixels [][]float64, x int, y int) []float64 {
flattens := make([]float64, x*y)
for i := 0; i < y; i++ {
for j := 0; j < x; j++ {
flattens[y*i+j] = pixels[i][j]
}
}
return flattens
}