package main
import (
"fmt"
"image"
"image/png"
"log"
"os"
"reflect"
)
func main() {
file, err := os.Open("C:\\Users\\wg\\go\\src\\学习go语言\\灰度.png")
if err != nil {
log.Fatal(err)
}
img, err := png.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
fmt.Println(img.Bounds().Max)
fmt.Println(img.Bounds().Min)
fmt.Println(reflect.TypeOf(img))
dx := img.Bounds().Dx()
dy := img.Bounds().Dy()
newRgba := image.NewRGBA(img.Bounds())
var temp uint8=0
for i := 0; i < dx; i++ {
for j := 0; j < dy; j++ {
colorRgb := img.At(i, j)
r, _, _, _ := colorRgb.RGBA()
r_uint8 := uint8(r >> 8)
if temp<r_uint8{
temp=r_uint8
}else{
continue;
}
}
}
var tempmin uint8=255
for a := 0; a < dx; a++ {
for b := 0; b < dy; b++ {
colorRgb := img.At(a, b)
r, _, _, _ := colorRgb.RGBA()
r_uint8 := uint8(r >> 8)
if tempmin>r_uint8{
tempmin=r_uint8
}else{
continue;
}
}
}
var nn [777][776] float64
for c := 0; c < dx; c++ {
for d := 0; d < dy; d++ {
colorRgb := img.At(c, d)
r, _, _, _ := colorRgb.RGBA()
r_uint8 := uint8(r >> 8)
newA:=float64((r_uint8-tempmin))/float64((temp-tempmin))
nn[c][d]=newA
}
}
fmt.Println(nn)
fmt.Println("像素归一化后的结果为",newRgba)
fmt.Println(img.At(0,0))
fmt.Println(img.Bounds())
fmt.Println(img.Bounds().Dx())
fmt.Println(img.Bounds().Dy())
fmt.Println("像素归一化成功了")
}