比较两个位图的匹配情况 as3
我试图根据较小图像的构成将一个图像放置在另一个图像的顶部。较小的图像是较大图像的剪裁,我需要将其精确定位在较大图像上,使其看起来像单个图像,但允许应用单独的滤镜和 Alpha。由于图像不是简单的矩形或圆形,而是复杂的卫星图像,我不能简单地用代码重新绘制它们。我有很多图像,因此不想手动查找每个图像的位置,也不想在动作脚本中手动设置它们。有没有办法让我针对较大的图像采样 5-10 平方像素的小区域,并在找到完美匹配时设置较小图像的 x 和 y 值?所有图像都在一个数组中,并且已经设置了对它们的迭代,我只需要一种采样和匹配像素的方法。我的第一个猜测是向右和向下逐像素循环图像,覆盖整个位图,并在找到匹配项后移动到数组中的下一个子项,将匹配的子项保留在找到完美匹配时的位置。
I'm trying to position an image on top of another image based upon the make-up of the smaller image. The smaller image is a cut-out of a larger image and I need it to be positioned exactly on the larger image to make it look like a single image, but allow for separate filters and alphas to be applied. As the images are not simple rectangles or circles, but complex satellite images, I cannot simply redraw them in code. I have quite a few images and therefore do not feel like manually finding the position of each image every and hard setting them manually in actionscript. Is there any way for me to sample a small 5-10 sq. pixel area against the larger image and set the x and y values of the smaller image if a perfect match is found? All the images are in an array and iterating through them has already been set, I just need a way to sample and match pixels. My first guess was to loop the images pixel by pixel right and down, covering the whole bitmap and moving to the next child in the array once a match was found, leaving the matched child where it was when the perfect match was found.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望我正确理解你的问题。
可能有一个选项使用 copypixels 来实现您想要的。您可以使用 bitmapdata.rect 值来确定所需样本的大小,并使用矩形和移动点循环遍历较大的位图。让我们看看我是否可以将其编码出来......
沿着这些思路的东西应该可以工作 - 我确信代码优雅和可能的优化还有空间。然而,这种方法似乎会非常慢,因为您必须检查每个像素是否匹配。
有一个更好的方法。将大图像拆分为多个图层,并使用位块传输技术在运行时合成它们。在您的情况下,您可以创建一个没有卫星的地面纹理,然后单独创建卫星,并使用 copyPixels 方法将它们放置在您想要的任何位置。谷歌“blitting in as3”可以找到一些不错的教程。我目前正在开发一个使用这种技术的游戏项目,这是一个非常好的方法。
祝你好运!
编辑:忘记在默认返回语句中编码。使用此方法,您必须返回一个无效点(如 (-1,-1))并在函数外部检查它。或者,您可以将小位图复制到函数内的大位图,这会更符合逻辑,但我不知道您的要求。
I hope I understood your question correctly.
There may be an option that uses copypixels to achieve what you want. You can use the bitmapdata.rect value to determine the size of the sample you want, and loop through the bigger bitmap using thet rectangle and a moving point. Let's see if I can code this out...
Something along those lines should work - I'm sure there's room for code elegance and possible optimization. However, it seems like something like this method would be very slow, since you'd have to check each pixel for a match.
There is a better way. Split your big image into layers, and use the blitting technique to composite them at runtime. In your case, you could create a ground texture without satellites, and then create the satellites separately, and use the copyPixels method to place them whereever you want. Google "blitting in as3" to find some good tutorials. I'm currently working on a game project that uses this technique and it's a very good method.
Good luck!
Edit: Forgot to code in a default return statement. Using this method, you'd have to return an invalid point (like (-1,-1)) and check for it outside the function. Alternatively, you could just copy your small bitmap to the big one within the function, which would be much more logical, but I don't know your requirements.
您需要找到大图像中的像素序列。
BitmapData.getPixel
为您提供像素值。因此,从小图像中获取第一个像素,在大图像中找到它,然后继续比较,直到找到完全匹配。如果您在编码时遇到困难,请随时询问。You need to find pixel sequence in the big image.
BitmapData.getPixel
gives you pixel value. So get first pixel from small image, find it in big image, then continue comparing until you find full match. If you have trouble to code that, feel free to ask.对于实际比较,有 BitmapData.compare 如果 BitmapData 对象相等,则返回数字 0。
For the actual comparison, there's BitmapData.compare which returns the number 0 if the BitmapData objects are equivalent.