发现质量中心上的2D位图 [英] Finding the center of mass on a 2D bitmap

查看:130
本文介绍了发现质量中心上的2D位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码一个游戏,我希望能够找到一个任意形状的质量对一个黑白位图的中心,像这样的:

 012345678
0.XX ......
1..XXX ....
2 ... ... XXX
3..XXXXXXX
4 ... XXX ...

所有细胞,具有相同的重量。对角相邻单元不被认为被连接,并且所述形状将始终是单个之一,因为它是在此之前已经分裂另一个功能

它只会被用于合理低分辨率(也许50x50的最多)的图像和它并不需要是超级准确,速度是preferable。

我得到一个感觉,有一个适当的方式做到这一点,但我真的不知道谷歌的东西。

我这个编码在ActionScript 3中,但在任何语言的例子是AP preciated,moreso如果他们做出被人理解。

编辑:随意假设数据存储在任何数据结构中,你认为最方便的例子。我使用位图,而是二维数组,甚至一个阵列就好了呢!

编辑:这是code我结束了使用,它可以最有可能被更快地完成,但我觉得这是非常可读的:

  // _bmp是一家民营BitmapData实例
公共职能getCenterOfMass():点{
    VAR平均:点=新的点(0,0);
    变种要点:UINT = 0;

    对于(VAR九:UINT = 0;九< _bmp.width;九++){
    对于(VAR IY:UINT = 0; IY< _bmp.height; IY ++){
    如果(_bmp.getPixel(IX,IY)== ACTIVE_COLOR){
    avg.x + =九;
    avg.y + = IY;
    点++;
    }
    }
    }

    avg.x / =点;
    avg.y / =点;

    平均回报;
}
 

解决方案

这个怎么样算法(psuedo- code)基于布尔矩阵就像在你的例子:

  xSum = 0
ySum = 0
点= 0

对于点矩阵
    如果点标记
        xSum + = pointX
        ySum + =尖尖
        点++

返程(xSum /分,ySum /分)
 

没有什么太复杂,计算其中X是最present,同样为Y,除以你计算点数,和你有重心。您可以通过在平均给予一定的点不同的权重进一步复杂化,但是这应该是你的主要方向。


这个问题让我想起了一个扩展到这个问题,我无法找到一个很好的答案。我张贴的问题在这里:<一href="http://stackoverflow.com/questions/411837/finding-clusters-of-mass-in-a-matrix-bitmap">http://stackoverflow.com/questions/411837/finding-clusters-of-mass-in-a-matrix-bitmap

I'm coding a game, and I'd like to be able to find the center of mass of an arbitrary shape on a black and white bitmap such as this:

 012345678
0.XX......
1..XXX....
2...XXX...
3..XXXXXXX
4...XXX...

All "cells" have the same weight. Diagonally adjacent cells are not considered to be connected, and the shape will always be a single one since it's already split by another function before this.

It's only going to be used for reasonably low resolution (maybe 50x50 at most) images and it doesn't need to be super accurate, speed is preferable.

I get a feeling there's a proper way to do this, but I don't really know what to google for.

I'm coding this in Actionscript 3, but examples in any language are appreciated, moreso if they're made to be understood by humans.

EDIT: Feel free to assume that the data is stored in whatever data structure you think is most convenient for your example. I'm using bitmaps, but two-dimensional arrays or even a single array is just fine too!

EDIT: This is the code I ended up using, it can most likely be done faster, but I find this to be very readable:

// _bmp is a private BitmapData instance
public function getCenterOfMass():Point {
    var avg		:Point  = new Point(0, 0);
    var points	:uint 	= 0;

    for (var ix:uint = 0; ix < _bmp.width; ix++) {
    	for (var iy:uint = 0; iy < _bmp.height; iy++) {
    		if (_bmp.getPixel(ix, iy) == ACTIVE_COLOR) {
    			avg.x += ix;
    			avg.y += iy;
    			points++;
    		}
    	}
    }

    avg.x /= points;
    avg.y /= points;

    return avg;
}

解决方案

How about this algorithm (psuedo-code) based on a boolean matrix like in your example :

xSum = 0
ySum = 0
points = 0

for point in matrix
    if point is marked
        xSum += pointX
        ySum += pointY
        points++

return (xSum/points, ySum/points)

Nothing too complicated, calculate where X is the most present, same for Y, divide by number of points you counted, and you got the center of mass. You can further complicate this by giving certain points different weight in the averaging, but this should be your main direction.


This question got me thinking about an expansion to this question which I could not find a good answer to. I posted the question here: http://stackoverflow.com/questions/411837/finding-clusters-of-mass-in-a-matrix-bitmap

这篇关于发现质量中心上的2D位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆