gpt4 book ai didi

go - 从 Golang image.Image 检索像素数据时,img.At() 或 rgba.Pix() 更快/更好吗?

转载 作者:行者123 更新时间:2023-12-03 08:11:44 25 4
gpt4 key购买 nike

阅读 Get a pixel array from from golang image.Image 的答案,我看到有两种像素 RGBA 检索方法,通过 img.At()rgba.Pix()

哪个更好用?是否应该始终使用其中一种,或者是否存在应使用其中一种而不是另一种的情况,反之亦然?

最佳答案

如果您的程序将进行需要大部分(如果不是全部)像素数据的计算,则 rgba.Pix()显着优于img.At() 。如果您只需要图像中单个或几个像素的像素数据,请使用 img.At() (在这种情况下,计算 rgba.Pix() 先决条件的开销太高)。

以下是各种测试负载的结果,每个测试负载的持续时间是 10 个样本的平均值。

<表类=“s-表”><标题>方法1x11000x6673840x21601000x667 + 计算1000x667 仅 5x5 访问 <正文> img.At() 195ns30.211071ms294.885396ms853.345043ms42.431 μs rgba.Pix() 719ns7.786029ms77.700552ms836.480063ms6.791461 毫秒

我们可以看到如何对于微小的 1x1 图像以及我们将 for 循环限制为上限 5 的图像,使用 img.At()导致更快的执行时间。但是,对于获取每个像素的用例,rgba.Pix()带来更好的性能。随着总时间的增加以及 img.At() 之间的差异,我们对每个像素进行的计算越多,这种性能改进就越不明显。和rgba.Pix()变得不那么明显,如上表中的“1000x667 + 计算”所示。

这里是使用的测试代码:

func main() {
resp, err := http.Get("IMAGE URL GOES HERE")
if err != nil {
panic(err)
}
defer resp.Body.Close()

img, _, err := image.Decode(resp.Body)
if err != nil {
panic(err)
}

var start time.Time
var duration time.Duration
samples := 10
var sum time.Duration

fmt.Println("Samples: ", samples)
sum = time.Duration(0)
for i := 0; i < samples; i++ {
start = time.Now()
usingAt(img)
duration = time.Since(start)
sum += duration
}
fmt.Println("*** At avg: ", sum/time.Duration(samples))

sum = time.Duration(0)
for i := 0; i < samples; i++ {
start = time.Now()
usingPix(img)
duration = time.Since(start)
sum += duration
}
fmt.Println("*** Pix avg: ", sum/time.Duration(samples))
}

func usingAt(img image.Image) {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y

for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, _ := img.At(x, y).RGBA()
_ = uint8(r >> 8)
_ = uint8(g >> 8)
_ = uint8(b >> 8)
}
}
}

func usingPix(img image.Image, targetColor colorful.Color) {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y

rgba := image.NewRGBA(bounds)
draw.Draw(rgba, bounds, img, bounds.Min, draw.Src)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
index := (y*width + x) * 4
pix := rgba.Pix[index : index+4]
_ = pix[0]
_ = pix[1]
_ = pix[2]
}
}
}

1000x667 仅访问 5x5 替换了 heightwidth在for循环中使用5和5,限制访问的像素数量。

1000x667 + 计算实际上通过将每个像素与目标颜色的颜色距离与go-colorful进行比较来使用RGB值。的DE2000计算。

关于go - 从 Golang image.Image 检索像素数据时,img.At() 或 rgba.Pix() 更快/更好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70616183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com