mirror of
https://github.com/esimov/pigo.git
synced 2025-10-05 16:16:55 +08:00

Note: this bug is likely to have adversely affected detection quality on any non-square image sources.
23 lines
473 B
Go
23 lines
473 B
Go
package pigo
|
|
|
|
import (
|
|
"image"
|
|
)
|
|
|
|
// RgbToGrayscale converts the image to grayscale mode.
|
|
func RgbToGrayscale(src *image.NRGBA) []uint8 {
|
|
cols, rows := src.Bounds().Dx(), src.Bounds().Dy()
|
|
gray := make([]uint8, rows*cols)
|
|
|
|
for r := 0; r < rows; r++ {
|
|
for c := 0; c < cols; c++ {
|
|
gray[r*cols+c] = uint8(
|
|
0.299*float64(src.Pix[r*4*cols+4*c+0]) +
|
|
0.587*float64(src.Pix[r*4*cols+4*c+1]) +
|
|
0.114*float64(src.Pix[r*4*cols+4*c+2]),
|
|
)
|
|
}
|
|
}
|
|
return gray
|
|
}
|