如何从png图像获取不透明区域的中心 [英] How to get center of un-transparent region from png image

查看:171
本文介绍了如何从png图像获取不透明区域的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一个不透明对象的PNG图像,其他区域是透明的.现在,如何获得该对象的区域以及如何获得该区域的中心?如果有人有答案,请分享.如果有代码,那是最好的(请用c#编写).谢谢!

我的英语不好,因为我不是英国人.

I have an PNG image with one un-transparent object in it, other region else is transparent. Now, how I get region of that object and how I get center of that region? If anyone have answer, please share it. And if it have codes, that is best (please write it in c#). Thankyou!

My english is not good, because i''m not english man.

推荐答案

定义中心".

一种方法是获得质量中心"(伪代码):
Define ''centre''.

One approach is to get the ''centre of mass'' (pseudocode):
float xaccum = 0, yaccum = 0;
float divisor = 0;

for(x = 0 to width-1)
 for(y = 0 to height-1) {
  float thisweight = pixels[x,y].A / 255f
  xaccum += x * thisweight;
  yaccum += y * thisweight;

  divisor += thisweight;
 }
}

PointF centre = [xaccum / divisor, yaccum / divisor];



(是的,可以很容易地对其进行优化,尤其是除数,但是这种讨论清楚地表明了计算的加权平均值"性质.)

这需要读取整个像素阵列,因此速度会很慢(即使使用LockBits并扫描该阵列而不是使用GetPixel).我目前无法想到一种与此无关的方法.

如果您希望所有不透明的像素(即使在255中具有1的alpha值)都计为全部权重,则按透明度进行加权.请将此权重设置为1或0.



(Yes, that can easily be optimised, particularly the divisor, but this forumlation is clear about the ''weighted average'' nature of the calculation.)

This requires reading over the whole pixel array so it will be slow (even using LockBits and scanning the array instead of using GetPixel). I can''t currently think of a way that doesn''t involve that, though.

It weights by the transparency, if you want all non-transparent pixels (even with an alpha of 1 part in 255) to count as full weight set thisweight to 1 or 0.


我建​​议遍历图像数据.

确定包含该圆内或圆上所有不透明点的最小圆的公式.

该圆的中心将是不透明对象的中心.


找到相距最远的两个点.

距离= sqrt((x1-x2)^ 2 +(y1-y2)^ 2)

然后用这两个点画一个圆

(距离/2)^ 2 =(x-(x1 + x2)/2)^ 2 +(y-(y1 + y2)/2)^ 2

如果所有其他点都在该圆中,则您的中心为((x1 + x2)/2,(y1 + y2)/2)

如果不是,则取离圆最远的点,并与原来的3个点合并以形成一个圆

r ^ 2 =(x-h)^ 2 +(y-k)^ 2

3分:(a,b)(c,d)(e,f)

h =(1/2)(((a²+b²)(fd)+(c²+d²)(bf)+(e²+f²)(db))/(a(fd)+ c(bf)+ e(db) ))

k =(1/2)(((a²+b²)(ec)+(c²+d²)(ae)+(e²+f²)(ca))/(b(ec)+ d(ae)+ f(ca) ))

r²=(a-h)²+(b-k)²

(h,k)是中心点
I suggest iterating through the image data.

Determine the formula for the smallest circle which contains all the opaque points inside or on the circle.

The center of that circle would be the center of the opaque object.


Find the two points that are farthest apart.

distance = sqrt((x1 - x2)^2 + (y1 - y2)^2)

Then make a circle with those two points

(distance /2 )^2 = (x - (x1+x2)/2)^2 + (y - (y1+y2)/2)^2

if all other points are in this circle, then you have the center point of ((x1+x2)/2, (y1+y2)/2)

if not, take the point farthest from the circle and combine with your original 3 points to form a circle

r^2 = (x - h)^2 + (y - k)^2

3 points : (a,b) (c,d) (e,f)

h = (1/2)((a²+b²)(f-d) + (c²+d²)(b-f) + (e²+f²)(d-b)) / (a(f-d)+c(b-f)+e(d-b))

k = (1/2)((a²+b²)(e-c) + (c²+d²)(a-e) + (e²+f²)(c-a)) / (b(e-c)+d(a-e)+f(c-a))

r² = (a-h)² + (b-k)²

(h,k) is the center point


这篇关于如何从png图像获取不透明区域的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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