gpt4 book ai didi

file - ioutil ReadFile 添加额外的字节

转载 作者:IT王子 更新时间:2023-10-29 02:36:52 25 4
gpt4 key购买 nike

我一直在努力尝试将一些 MNIST 数据库绘制到一个图像文件中,在我第一次尝试时,生成的图像似乎发生了偏移,如下所示:

enter image description here

我知道训练文件包含 60,000 张图像,每张图像的大小为 28 x 28 像素,在文件中表示为 28 x 28 x 60,000 uint8 的数组,其长度应为 47040000。

但是,当打印文件的长度时,它给出 47040016 作为它的长度,额外的 16 个数字是导致图像移动的原因。

使用的代码如下,const imgNum 由我要打印的图像和图像的长度定义。在读取图像文件时,我真的没有看到任何奇怪的事情。

package main

import (
"image"
"image/color"
"image/png"

"io/ioutil"

"os"
)

const (
imgSideLength = 28
imgSize = imgSideLength * imgSideLength
imgNum = 499 * imgSize
)

var images []uint8

func main() {
images, err := ioutil.ReadFile("train-images")
check(err)

canvas := image.NewRGBA(image.Rect(0, 0, imgSideLength, imgSideLength))

pixelIndex := imgNum
for i := 0; i < imgSideLength; i++ {
for j := 0; j < imgSideLength; j++ {
currPixel := images[pixelIndex]
pixelIndex++

pixelColor := color.RGBA{currPixel, currPixel, currPixel, 255}
canvas.Set(j, i, pixelColor)
}
}

numFile, err := os.Create("number.png")
check(err)
defer numFile.Close()

png.Encode(numFile, canvas)
}

func check(e error) {
if e != nil {
panic(e)
}
}

知道那 16 个像素是导致图像移动的像素,我决定修改 imgNum:

imgNum = 499 * imgSize + 16

有了这个改变,图像绘制得很好。

enter image description here

但我还是想知道为什么不应该有的地方多了16个数字?

最佳答案

查看their web site可以看到文件的数据格式是:

[offset] [type]          [value]          [description]
0000 32 bit integer 0x00000803(2051) magic number
0004 32 bit integer 60000 number of images
0008 32 bit integer 28 number of rows
0012 32 bit integer 28 number of columns
0016 unsigned byte ?? pixel
0017 unsigned byte ?? pixel
........
xxxx unsigned byte ?? pixel

这意味着头信息的前 16 个字节是 4 个 32 位整数,因此是 16 个字节。

关于file - ioutil ReadFile 添加额外的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52247955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com