| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using o0;
- using o0.Num;
- using o0InfraredLocate.ZIM;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using UnityEngine;
- using ZIM.Unity;
- using static ScreenLocate;
- using Color = UnityEngine.Color;
- namespace ZIM
- {
- // 亮区的点用来定位(计算center),泛光区域的点用来计算radius
- public class PixelSpotAreaOld
- {
- // 中心
- public Vector2 Centroid { get; private set; }
- // 亮区的半径,计算含泛光点
- float radius = 0;
- public float Radius
- {
- set
- {
- radius = value;
- }
- get
- {
- if (radius != 0)
- return radius;
- //var radiusDic = new Dictionary<int, float>() { { 0, 0 }, { 45, 0 }, { 90, 0 }, { 135, 0 }, { 180, 0 }, { 225, 0 }, { 270, 0 }, { 315, 0 } };
- var radiusDic = new float[8];
- foreach (var p in Pixels0)
- {
- var dir = p - Centroid;
- var radius = dir.magnitude;
- var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
- if (degreeI > 7) degreeI = 0; // 防止正好360度
- if (radius > radiusDic[degreeI])
- radiusDic[degreeI] = radius;
- }
- foreach (var p in Pixels1)
- {
- var dir = p - Centroid;
- var radius = dir.magnitude;
- var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
- if (degreeI > 7) degreeI = 0;
- if (radius > radiusDic[degreeI])
- radiusDic[degreeI] = radius;
- }
- return radius = radiusDic.Mean();
- }
- }
- public List<Vector2> Pixels0 = new List<Vector2>();
- public List<Vector2> Pixels1 = new List<Vector2>();
- public PixelSpotAreaOld(Vector2 center) // kmeans中用随机点初始化,作为中心
- {
- Centroid = center;
- }
- public void Add0(Vector2 point)
- {
- Pixels0.Add(point);
- }
- public void Add1(Vector2 point)
- {
- Pixels1.Add(point);
- }
- // 计算中心点,用亮区计算, 如果中心改变则输出true,不变输出false
- public bool UpdateCentroid()
- {
- Vector2 sum = Vector2.zero;
- foreach (var p in Pixels0)
- sum += p;
- var newCentroid = sum / Pixels0.Count;
- if (Centroid == newCentroid)
- return false;
- Centroid = newCentroid;
- return true;
- }
- // 不影响center
- public void Clear()
- {
- Pixels0.Clear();
- Pixels1.Clear();
- }
- public float TotalBrightness(Color[] pixels, Func<int, int, int> Vector2ToIndex, int outer_size = 3)
- {
- (int x, int y) rectMin = ((int)(Centroid.x - Radius - outer_size), (int)(Centroid.y - Radius - outer_size));
- (int x, int y) rectMax = ((int)(Centroid.x + Radius + outer_size), (int)(Centroid.y + Radius + outer_size));
- var total = 0f;
- Parallel.For(rectMin.x, rectMax.x, (i) =>
- {
- var t = 0;
- for (int j = rectMin.y; j < rectMax.y; j++)
- {
- var index = Vector2ToIndex(i, j);
- var b = pixels[index].Brightness(64);
- t += b;
- }
- lock (InfraredLocate.Locker)
- {
- total += t;
- }
- });
- //total /= (rectMax.y - rectMin.y) * (rectMax.x - rectMin.x);
- return total;
- }
- }
- }
|