- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
阅读 Get a pixel array from from golang image.Image 的答案,我看到有两种像素 RGBA 检索方法,通过 img.At()
和 rgba.Pix()
。
哪个更好用?是否应该始终使用其中一种,或者是否存在应使用其中一种而不是另一种的情况,反之亦然?
最佳答案
如果您的程序将进行需要大部分(如果不是全部)像素数据的计算,则 rgba.Pix()
显着优于img.At()
。如果您只需要图像中单个或几个像素的像素数据,请使用 img.At()
(在这种情况下,计算 rgba.Pix()
先决条件的开销太高)。
以下是各种测试负载的结果,每个测试负载的持续时间是 10 个样本的平均值。
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 替换了 height
和width
在for循环中使用5和5,限制访问的像素数量。
1000x667 + 计算实际上通过将每个像素与目标颜色的颜色距离与go-colorful进行比较来使用RGB值。的DE2000计算。
关于go - 从 Golang image.Image 检索像素数据时,img.At() 或 rgba.Pix() 更快/更好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70616183/
当我调用 png.Decode(imageFile) 时,它返回类型 image.Image。但我找不到将其转换为 image.NRGBA 或 image.RGBA 的记录方式,我可以在其上调用 At
image/jpeg 和 image/png 包有 Decode 和 Encode 函数,可以读取和写入 jpeg 和 png 图像,但 image/gif 包没有 - 只有 Decode 和 Dec
我正在尝试从一系列任意的非调色板图像创建动画 GIF。为了创建调色板图像,我需要以某种方式想出一个调色板。 // RGBA, etc. images from somewhere else var f
我在今年夏天的空闲时间使用 Go 镜像包进行一些练习。 package main import ( "os" "image" "image/png" "image/co
关闭。这个问题需要debugging details .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 1年前关闭。 Improve this question 今天
我正在尝试在 TilePane 中列出图像。当我尝试创建图像 new ImageView("address"); 时出现错误,地址如下: "file:D:/Chrysanthemum.jpeg/" 以
我有一个用于为画廊选择图像的表单,我希望允许用户仅选择 jpg、gif 和 png 图像格式。 现在,为了测试,我将图像的扩展名更改为 .bmp,例如“image1.bmp”,当我在输入文件中单击以选
我有创建图像的代码:(m_img 是 javafx.scene.image.Image) Image m_img = new Image("file:" + p_Fil.getAbsoluteFile
假设我有一个这样的 8 位灰度图像: var pixels []byte = ... width := 100 height := 100 如何将其转换为实现 image.Image 的东西? 最佳答
这段代码是我在localhost:8088 URL上的索引/主页的一部分,如果我想将用户发送到url localhost:8088/image/1,我应该写href='image/{{$image->
我正在尝试对图像进行简单的裁剪。这是代码 from PIL.Image import Image def get_image_half(image, half="upper"): if hal
我在这个问题上花了一整天,但在堆栈溢出中没有看到答案! 我试过了但是没用: >> pil_image = Image.frombytes('RGBA', wand_image.size, wa
所以,我是那些以始终使用最新版本的浏览器而自豪的人之一(当然 Internet Explorer 除外 - 我说的不是那个浏览器)。 我遇到了 this awesome CSS3 website详细介
如果 image_tag 无法从 url 加载图像,我想呈现默认图像: 因此,如果 image_tag 无法从 url 加载图像: 然后呈现默认值: 这将生成结果 HTML: 关于image -
我正在创建一个类似横幅的组件,并将图像设置为组件的背景,但我无法让它工作。我尝试了网上发布的不同建议,但没有成功,目前我不确定我的错误是否在 react 代码中,或者是 webpack 没有正确加载文
如何解决 Dart 中的这种歧义错误。 import 'dart:io'; import 'package:flutter/material.dart'; import 'package:camera
Center( child: CachedNetworkImage( imageUrl: "http:/ sosme link he
设置 www.website.com/sds/(index.htm) 以便鼠标悬停在不同位置时显示图像。 出于某种原因,当您将鼠标悬停在蓝色气球上时,图像 2.jpg 和 3.jpg(在蓝色气球上来回
社交网络在共享 URL 时可以很好地从网站中提取标题和描述,但对于图像,仍然需要创建自定义元标记:property="og:image" name="twitter:image" itemprop="
我正在尝试写一个简短的,它将读取一个 PNG 文件,并将一个 channel 与另一个 channel (R,G,B) 交换作为可能的选择。 但是,我无法找到如何从 image.At(x,y) 返回的
我是一名优秀的程序员,十分优秀!