dongzuo9096 2016-06-05 15:06
浏览 288


I'm new at Go and trying to improve my skills. Currently I'm working with images and I need to have all pixels' red value of an image. I know I can use the code below to achieve this but it seemed slow to me(~485 msecs),

pixList := make([]uint8, width*height)

for y := 0; y < height; y++ {
    for x := 0; x < width; x++ {
        r, _, _, _ := img.At(x, y).RGBA()
        var rNew uint8 = uint8(float32(r)*(255.0/65535.0))
        pixList[(x*height)+y] = rNew

Is there any faster way to do this? Any built-in functions to get all pixel values at once?

Edit: I'm now using the Pix to get all pixel data but still my Pix list is not giving what I'm looking for.

new code:

pixList := img.(*image.Paletted).Pix
newPixList := make([]uint8, width*height)

fmt.Println(len(pixList))//gives width*height, shouldn't it be width*height*4?
for index := 0; index < width*height; index++ {
    newPixList[index] = pixList[index*4]//this part gives index out of range error, because the pixList is length of width*height, i dunno why


I think it's not behaving my image as it's an rgba image, maybe a conversion could work. Any ideas?


  • 写回答

1条回答 默认 最新

  • douna1895 2016-06-05 18:01

    You can't make this pattern performant, because this requires an interface method call for every pixel. For fast access to the image data, you access the image's data directly. Take the image.RGBA type for example:

    type RGBA struct {
            // Pix holds the image's pixels, in R, G, B, A order. The pixel at
            // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4].
            Pix []uint8
            // Stride is the Pix stride (in bytes) between vertically adjacent pixels.
            Stride int
            // Rect is the image's bounds.
            Rect Rectangle

    The docs for each image type include the data layout and indexing formula. For this type you could extract all red pixels from the Pix slice with:

    w, h := img.Rect.Dx(), img.Rect.Dy()
    pixList := make([]uint8, w*h)
    for i := 0; i < w*h; i++ {
        pixList[i] = img.Pix[i*4]

    If you need to convert other image types, you can use the existing methods to do the color conversion, but first assert the correct image type and use the native *At method to avoid the interface call. Extracting the approximate red values from a YCbCr image :

    w, h := img.Rect.Dx(), img.Rect.Dy()
    pixList := make([]uint8, w*h)
    for x := 0; x < w; x++ {
        for y := 0; y < h; y++ {
            r, _, _, _ := img.YCbCrAt(x, y).RGBA()
            pixList[(x*h)+y] = uint8(r >> 8)
    return pixList

    Similar to how the YCbCr image above has no "red" pixels (the value needs to be computed for each individual pixel), a paletted image has no individual RGBA values for the pixels, and needs to be looked up in the image's palette. You could take this one step further and predetermine the color model of the palette colors to remove the Color.RGBA() interface call to speed this up even more like so:

    palette := make([]*color.RGBA, len(img.Palette))
    for i, c := range img.Palette {
        palette[i] = c.(*color.RGBA)
    pixList := make([]uint8, len(img.Pix))
    for i, p := range img.Pix {
        pixList[i] = palette[p].R
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 python使用pulp线性优化时报错
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题