dongzuo9096 2016-06-05 15:06
浏览 288
已采纳

获取所有像素值(RGBA)

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?

Thanks.

  • 写回答

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 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了