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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)