go 语言实现图像归一化

package main

import (
	"fmt"
	"image"

	//"image/color"
	"image/png"
	"log"
	"os"
	"reflect" //反射
)

func main() {
    
    
	file, err := os.Open("C:\\Users\\wg\\go\\src\\学习go语言\\灰度.png")
	if err != nil {
    
    
		log.Fatal(err)
	}

	// decode jpeg into image.Image
	img, err := png.Decode(file)//jpg和png的格式不一样
	if err != nil {
    
    
		log.Fatal(err)
	}
	file.Close()
	fmt.Println(img.Bounds().Max)//打印最大的边框
	fmt.Println(img.Bounds().Min)//打印最小的边框
	fmt.Println(reflect.TypeOf(img))//反射
	//把RGBA转换为255形势的。
	dx := img.Bounds().Dx()//x轴的值
	dy := img.Bounds().Dy()//y轴的值
	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 G B A
			r_uint8 := uint8(r >> 8)	//转换为 255 值
			//g_uint8 := uint8(g >> 8)
			//b_uint8 := uint8(b >> 8)
			//a_uint8 := uint8(a >> 8)
			if temp<r_uint8{
    
    
				temp=r_uint8
			}else{
    
    
				continue;
			}

		}

	}
	//fmt.Println("最大值为",temp)
	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 G B A
			r_uint8 := uint8(r >> 8)	//转换为 255 值
			//g_uint8 := uint8(g >> 8)
			//b_uint8 := uint8(b >> 8)
			//a_uint8 := uint8(a >> 8)
			if tempmin>r_uint8{
    
    
				tempmin=r_uint8
			}else{
    
    
				continue;
			}

		}


	}
	var nn [777][776] float64 /* 数组里面的数必须是定值*/  //所以这里要根据图像的大小更改。
	//fmt.Println("最小值为",tempmin)
	for c := 0; c < dx; c++ {
    
    
		for d := 0; d < dy; d++ {
    
    
			colorRgb := img.At(c, d)
			r, _, _, _ := colorRgb.RGBA()//转化为R G B A
			r_uint8 := uint8(r >> 8)	//转换为 255 值
			//g_uint8 := uint8(g >> 8)
			//b_uint8 := uint8(b >> 8)
			//a_uint8 := uint8(a >> 8)
			newA:=float64((r_uint8-tempmin))/float64((temp-tempmin))//这里有问题,需要改。
			nn[c][d]=newA




		}

	}
	fmt.Println(nn)
	fmt.Println("像素归一化后的结果为",newRgba)//输出归一化的结果。

/*
	dx:=img.Bounds().Dx()
	dy:=img.Bounds().Dy()
	for i:=0;i<dx;i++ {
		temp:=0
		for j:=0;j<dy;j++{
			if img.Bounds().At(i,j)>=temp
			{
				temp=img.At(i,j)
			}
			else:
				continue

		}
		fmt.Println("hello world")
	}
*/
	fmt.Println(img.At(0,0))//输出{223 226 231 255} R G B A
	fmt.Println(img.Bounds())//这个返回范围,后面的那个,At方法返回(x, y)位置的色彩
	fmt.Println(img.Bounds().Dx())//输出x轴的长度
	fmt.Println(img.Bounds().Dy())//输出y轴的长度
	fmt.Println("像素归一化成功了")
}

猜你喜欢

转载自blog.csdn.net/zhuiyunzhugang/article/details/121257605