duanqinjiao5244 2018-09-09 19:27
浏览 94
已采纳

ioutil ReadFile增加了额外的字节

I've been working on trying to draw a number of the MNIST database to an image file, in my first attempts, the image generated seemed shifted, as follows:

enter image description here

I know that the training file consists of 60,000 images, each one is 28 x 28 pixels big, which in the file is represented as an array of 28 x 28 x 60,000 uint8, which should give 47040000 as it's length.

However, when printing the length of the file, it gives 47040016 as it's length, that extra 16 numbers are what causes the image to shift.

The code used is the following, the const imgNum is defined by the image I want to print and the length of the image. I don't really see anything weird going on when reading the images file.

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)
    }
}

Knowing that those 16 pixels are the ones that causes the image to shift, I decided to modify imgNum:

imgNum = 499 * imgSize + 16

With that change, the image draws fine.

enter image description here

But I would still like to know why are there an extra 16 numbers where there shouldn't be?

  • 写回答

1条回答 默认 最新

  • duanqin4238 2018-09-09 19:33
    关注

    Looking at their web site you can see that the data format for the file is:

    [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
    

    which means the first 16 bytes are 4 32 bit integers, hence 16 byte, of header information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号