dousong1926 2019-03-16 01:34
浏览 47
已采纳

Image.At返回nil

I'm having an issue. Here is my code:

package main

import (
    "math/rand"
    "image/draw"
    "image/png"
    "image/color"
    "strconv"
    "os"
    "time"
    "fmt"
)

func genSites(width, height int) ([][]int) {
    rand.Seed(time.Now().Unix())
    l, err := strconv.Atoi(os.Args[len(os.Args)-2])
    if err != nil {
        panic(err)
    }
    sites := make([][]int, l)
    for i := range sites {
        sites[i] = make([]int, 2)
        sites[i][0] = rand.Intn(width)
        sites[i][1] = rand.Intn(height)
    }
    return sites
}

func main() {
    inputF, err := os.Open(os.Args[len(os.Args)-3])
    if err != nil {
        panic(err)
    }
    defer inputF.Close()

    inputR, err := png.Decode(inputF)
    if err != nil {
        panic(err)
    }
    input := inputR.(draw.Image)
    minx, miny := input.Bounds().Min.X, input.Bounds().Min.Y
    maxx, maxy := input.Bounds().Max.X-1, input.Bounds().Max.Y-1
    sites := genSites(maxx-minx, maxy-miny)
    siteColors := make([][]color.Color, len(sites))
    //todo: figure out something better than this
    for i := range siteColors {
        siteColors[i] = make([]color.Color, (maxx-minx)*(maxy-miny))
    }
    siteBelongs := make([][]int, maxx - minx)
    for x := range siteBelongs {
        siteBelongs[x] = make([]int, maxy - miny)
        for y := range siteBelongs[x] {
            dmin := (maxx-minx)*(maxx-minx) + (maxy-miny)*(maxy-miny)
            var smin int
            for i, s := range sites {
                d := (s[0]-x)*(s[0]-x) + (s[1]-y)*(s[1]-y)
                if d > dmin {
                    smin = i
                    dmin = d
                }
            }
            siteBelongs[x][y] = smin
            siteColors[smin] = append(siteColors[smin], input.At(x+minx, y+miny))
        }
    }
    siteAvgColors := make([]color.Color, len(sites))
    for i := range siteAvgColors {
        var sR, sG, sB, sA int
        for _, val := range siteColors[i] {
            fmt.Println(val)
            r, g, b, a := val.RGBA()
            sR += int(r)
            sG += int(g)
            sB += int(b)
            sA += int(a)
        }
        siteAvgColors[i] = color.RGBA{
            uint8(sR/len(siteColors[i])),
            uint8(sG/len(siteColors[i])),
            uint8(sB/len(siteColors[i])),
            uint8(sA/len(siteColors[i]))}
    }
    for x := range siteBelongs {
        for y := range siteBelongs[x] {
            input.Set(minx + x, miny + y, siteAvgColors[siteBelongs[x][y]])
        }
    }
    output, err := os.Create(os.Args[len(os.Args)-1])
    if err != nil {
        panic(err)
    }
    defer output.Close()
    err = png.Encode(output, input)
    if err != nil {
        panic(err)
    }
}

The error is this:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4b46e1]

goroutine 1 [running]:
main.main()
    /home/victor/programs/goprograms/src/v/imagerandvornoi/main.go:71 +0x7a1

Line 71 is the one that says r, g, b, a := val.RGBA(). That val is inserted at line 63, or siteColors[smin] = append(siteColors[smin], input.At(x+minx, y+miny)), which means that input.At is returning nil. Why? How can I fix this?

  • 写回答

1条回答 默认 最新

  • drh37116 2019-03-16 01:51
    关注

    This has to do with the semantics of the make() builtin within Go. make is special in that it can take two or three arguments:

    make(Type, length) or make(Type, length, capacity). For the former, the length and capacity are set to the same value. If you're going to be assigning values to the slice using append, you're going to want to use the latter form. This is because the two argument form (make(Type, 10)) builds a slice with 10 elements in it already with the zero value, when you use append() it becomes the 11th item.

    You're hitting a nil derefernece here because you're using the two-argument version of make with append, and so the first item in the slice is nil.

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

报告相同问题?

悬赏问题

  • ¥15 爬取豆瓣电影相关处理
  • ¥15 手机淘宝抓清除消息接口
  • ¥15 C#无selenium
  • ¥15 LD衰减计算的结果过大
  • ¥15 用机器学习方法帮助保险公司预测哪些是欺诈行为
  • ¥15 计算300m以内的LD衰减
  • ¥15 数据爬取,python
  • ¥15 怎么看 cst中一个面的功率分布图,请说明详细步骤。类似下图
  • ¥15 为什么我的pycharm无法用pyqt6的QtWebEngine
  • ¥15 FOR循环语句显示查询超过300S错误怎么办