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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格