AS3:如何有效访问像素数据?

发布于 2024-09-03 19:30:58 字数 371 浏览 7 评论 0原文

我正在做一个游戏。

游戏需要实体分析图像并转向具有特定属性(高红色通道等)的像素。

我研究过 Pixel Bender,但这似乎只对向图像写入新颜色有用。目前,即使在低分辨率 (200x200) 下,仅一个实体扫描图像也会减慢至 1-2 帧/秒。

我正在嵌入图像并将其实例化为位图作为舞台的子级。 1-2 FPS 的情况是使用 BitmapData.getPixel() (在每个像素上)并预先进行距离计算。

我想知道是否有任何方法可以更有效地做到这一点...我的第一个想法是某种空间分区以及将图像分割成许多较小的部分。

我也觉得 Pixel Bender 应该能够以某种方式提供帮助,但我对此缺乏经验。

为任何帮助干杯。 乔纳森

I'm working a game.

The game requires entities to analyse an image and head towards pixels with specific properties (high red channel, etc.)

I've looked into Pixel Bender, but this only seems useful for writing new colors to the image. At the moment, even at a low resolution (200x200) just one entity scanning the image slows to 1-2 Frames/second.

I'm embedding the image and instance it as a Bitmap as a child of the stage. The 1-2 FPS situation is using BitmapData.getPixel() (on each pixel) with a distance calculation beforehand.

I'm wondering if there's any way I can do this more efficiently... My first thought was some sort of spatial partioning coupled with splitting the image up into many smaller pieces.

I also feel like Pixel Bender should be able to help somehow, however I've had little experience with it.

Cheers for any help.
Jonathan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2024-09-10 19:30:58

让我们将实体朝向“吸引子”的像素称为“吸引子”,因为它们吸引实体。

您描述了由于扫描吸引子而导致的低帧速率。这表明您可能会在每一帧扫描图像。您无需指定扫描的图像是静态的还是像视频输入那样频繁变化。如果图像每帧都在变化,因此您必须以某种方式重新计算吸引子,那么您正在尝试使用 ABC 虚拟机进行实时计算机视觉,请参见下文。

如果您有一张不变的图像,那么您可以做的最重要的优化就是仅扫描图像一次,然后保存吸引子位置的摘要(或“记忆”)。在每个渲染帧,您可以搜索已知吸引子的列表或数组,而不是扫描整个图像。当用户导致图像发生变化时,您可以从头开始重新计算,或者增量更新您的计算 - 只要您认为合适。

如果您尝试使用 ActionScript 3 进行实时计算机视觉,我建议您查看 Flash 10.1 的新 vector 类型,并考虑使用 abcsx 编写 ABC 汇编代码,或使用 Adobe 的 Alchemy 将 C 编译到 Flash 运行时。 ABC是Flash的字节码。换句话说,重新考虑使用 AS3 进行实时计算机视觉。

Let us call the pixels which entities head towards "attractors" because they attract the entities.

You describe a low frame rate due to scanning for attractors. This indicates that you may possibly be scanning an image at every frame. You don't specify whether the image scanned is static or changes as frequently as, e.g., a video input. If the image is changing with every frame, so that you must re-calculate attractors somehow, then what you are attempting is real-time computer vision with the ABC Virtual Machine, please see below.

If you have an unchanging image, then the most important optimization you can make is to scan the image one time only, then save a summary (or "memoization") of the locations of the attractors. At each rendering frame, rather than scan the entire image, you can search the list or array of known attractors. When the user causes the image to change, you can recalculate from scratch, or update your calculations incrementally -- as you see fit.

If you are attempting to do real-time computer vision with ActionScript 3, I suggest you look at the new vector types of Flash 10.1 and also that you look into using either abcsx to write ABC assembly code, or use Adobe's Alchemy to compile C onto the Flash runtime. ABC is the byte code of Flash. In other words, reconsider the use of AS3 for real-time computer vision.

浊酒尽余欢 2024-09-10 19:30:58

BitmapData 有一个 getPixels 方法(注意它是复数)。它返回所有像素 的字节数组,该数组的迭代速度比 for 循环快得多,其中调用 getPixel ,嵌套在另一个 for 循环 中。不幸的是,顾名思义,字节数组是一维字节数组,因此迭代每个像素(4 个字节)需要使用 for 循环,而不是 foreach 循环。默认情况下,您可以单独访问每个像素的颜色通道,但这听起来像您想要的(查找具有“高红色通道”的像素),因此您不必按位和每个像素值来隔离特定通道。

我在某处读到 getPixel 非常慢,所以我认为这就是你可以节省最多的地方。我可能是错的,所以值得计时。

BitmapData has a getPixels method (notice it's plural). It returns a byte array of all the pixels which can be iterated much faster than a for loop with a call to getPixel inside, nested inside another for loop . Unfortunately, bytearrays are, as their name implies, 1 dimensional arrays of bytes, so iterating each pixel(4 bytes) requires using a for loop, not a foreach loop. You can access each pixel's color channel individually by default, but this sounds like what you want (find pixels with a "high red channel"), so you won't have to bitwise-and each pixel value to isolate a particular channel.

I read somewhere that getPixel is very slow, so that's where I figured you'd save the most. I could be wrong, so it'd be worth timing it.

神爱温柔 2024-09-10 19:30:58

我想说 Heath Hunnicutt 的答案是一个很好的答案。如果图像没有改变,只需将所有颜色值存储在向量中。或任何 byteArray 并将其用作查找表,这样您就不需要每帧调用 getPixel() 。

I would say Heath Hunnicutt's anwser is a good one. If the image doesnt change just store all the color values in a vector. or byteArray of whatever and use it as a lookup table so you don't need to call getPixel() every frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文