|
|
@@ -42,7 +42,18 @@ namespace ZIM
|
|
|
}
|
|
|
|
|
|
readonly float[] spotBrightness = new float[] { 0.93f, 0.5f }; // 亮点阈值
|
|
|
- int samplingScale = 1;
|
|
|
+ int _samplingScale = 1;
|
|
|
+ public int SamplingScale
|
|
|
+ {
|
|
|
+ get => _samplingScale;
|
|
|
+ set
|
|
|
+ {
|
|
|
+ if (value <= 0)
|
|
|
+ throw new InvalidOperationException();
|
|
|
+ _samplingScale = value;
|
|
|
+ PixelSpotArea.UpdateGridLength(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
//const float circleVariance = 30f;
|
|
|
//const int brightAreaRadius = 30;
|
|
|
//const int LeastBrightPoint = 10000;
|
|
|
@@ -120,8 +131,8 @@ namespace ZIM
|
|
|
//watch.Start();
|
|
|
//var times = new List<double>() { 0.0 };
|
|
|
|
|
|
- (int x, int y) rectMin = ((int)rect.min.x / samplingScale, (int)rect.min.y / samplingScale);
|
|
|
- (int x, int y) rectMax = ((int)rect.max.x / samplingScale, (int)rect.max.y / samplingScale);
|
|
|
+ (int x, int y) rectMin = ((int)rect.min.x / SamplingScale, (int)rect.min.y / SamplingScale);
|
|
|
+ (int x, int y) rectMax = ((int)rect.max.x / SamplingScale, (int)rect.max.y / SamplingScale);
|
|
|
|
|
|
var spotPoint = new List<Vector2>(200); // 预估的初始容量
|
|
|
var brightPoint = new List<Vector2>(1000);
|
|
|
@@ -132,8 +143,8 @@ namespace ZIM
|
|
|
var points1 = new List<Vector2>(rectMax.y - rectMin.y);
|
|
|
for (int j = rectMin.y; j < rectMax.y; j++)
|
|
|
{
|
|
|
- int ip = i * samplingScale;
|
|
|
- int jp = j * samplingScale;
|
|
|
+ int ip = i * SamplingScale;
|
|
|
+ int jp = j * SamplingScale;
|
|
|
|
|
|
if (!screenIdentification.Screen.Active && ScreenPixelCheaker.OutArea2D(new Vector2(ip, jp), screenIdentification.Size))
|
|
|
continue;
|
|
|
@@ -160,18 +171,18 @@ namespace ZIM
|
|
|
});
|
|
|
|
|
|
//if (ScreenLocate.Main.DebugOnZIMDemo)
|
|
|
- //{
|
|
|
- // if (spotPoint.Count > 400) // 如果亮点太多,控制亮点数量在200左右
|
|
|
- // {
|
|
|
- // samplingScale = (int)Math.Ceiling(samplingScale * Math.Sqrt(spotPoint.Count / 400.0));
|
|
|
- // return new List<ISpotArea>();
|
|
|
- // }
|
|
|
- // else if (samplingScale > 1 && spotPoint.Count < 100)
|
|
|
- // {
|
|
|
- // samplingScale = Math.Max((int)Math.Ceiling(samplingScale * Math.Sqrt(spotPoint.Count / 200.0)), 1);
|
|
|
- // return new List<ISpotArea>();
|
|
|
- // }
|
|
|
- //}
|
|
|
+ {
|
|
|
+ if (spotPoint.Count > 500) // 如果亮点太多,控制亮点数量在500左右
|
|
|
+ {
|
|
|
+ SamplingScale = (int)Math.Ceiling(SamplingScale * Math.Sqrt(spotPoint.Count / 500.0));
|
|
|
+ return new List<ISpotArea>();
|
|
|
+ }
|
|
|
+ else if (SamplingScale > 1 && spotPoint.Count < 100)
|
|
|
+ {
|
|
|
+ SamplingScale = Math.Max((int)Math.Ceiling(SamplingScale * Math.Sqrt(spotPoint.Count / 200.0)), 1);
|
|
|
+ return new List<ISpotArea>();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
//times.Add(watch.ElapsedMilliseconds);
|
|
|
//UnityEngine.Debug.Log("time1: " + (times[times.Count - 1] - times[times.Count - 2]));
|
|
|
@@ -201,9 +212,9 @@ namespace ZIM
|
|
|
//times.Add(watch.ElapsedMilliseconds);
|
|
|
//UnityEngine.Debug.Log("time2: " + (times[times.Count - 1] - times[times.Count - 2]));
|
|
|
|
|
|
- //var db = Dbscan.Run(spotPoint, ZIM.Unity.ZIMMath.LengthManhattan, samplingScale * 2, 3);
|
|
|
- //var spotArea = DbscanToSpotAreas(db, brightPoint);
|
|
|
- var spotArea = PixelSpotArea.Cluster(spotPoint, brightPoint, screenIdentification.Screen.UVInScreen);
|
|
|
+ var db = Dbscan.Run(spotPoint, ZIM.Unity.ZIMMath.LengthManhattan, SamplingScale, 4);
|
|
|
+ var spotArea = DbscanToSpotAreas(db, brightPoint);
|
|
|
+ //var spotArea = PixelSpotArea.Cluster(spotPoint, brightPoint, screenIdentification.Screen.UVInScreen);
|
|
|
|
|
|
if (ScreenLocate.Main.DebugOnZIMDemo)
|
|
|
DebugAreas(spotArea);
|
|
|
@@ -331,8 +342,8 @@ namespace ZIM
|
|
|
// 将泛光点添加到最近的area
|
|
|
foreach (var i in brightPoint)
|
|
|
{
|
|
|
- var index = FindNearestAreaIndex(i, areaIncludeRadius);
|
|
|
- if (index < clusters.Count)
|
|
|
+ var index = FindNearestAreaIndex(i, areaIncludeRadius,out float nearestDistance);
|
|
|
+ if (index >= 0 && index < clusters.Count && nearestDistance < PixelSpotArea.gridLength1)
|
|
|
clusters[index].Pixels1.Add(i);
|
|
|
}
|
|
|
// 添加了泛光点后,重新计算半径
|
|
|
@@ -342,13 +353,14 @@ namespace ZIM
|
|
|
return clusters;
|
|
|
}
|
|
|
|
|
|
- private int FindNearestAreaIndex(Vector2 a, List<(Vector2, float)> areas)
|
|
|
+ private int FindNearestAreaIndex(Vector2 a, List<(Vector2, float)> areas, out float nearestDistance)
|
|
|
{
|
|
|
+ nearestDistance = float.MaxValue;
|
|
|
if (areas == null || areas.Count == 0)
|
|
|
return -1;
|
|
|
|
|
|
int nearestIndex = 0;
|
|
|
- float nearestDistance = Vector2.Distance(a, areas[0].Item1) - areas[0].Item2;
|
|
|
+ nearestDistance = Vector2.Distance(a, areas[0].Item1) - areas[0].Item2;
|
|
|
|
|
|
for (int i = 1; i < areas.Count; i++)
|
|
|
{
|
|
|
@@ -620,12 +632,12 @@ namespace ZIM
|
|
|
{
|
|
|
var brightPixelDic = new Dictionary<float, List<Vector2>>();
|
|
|
(int x, int y) origin = ((int)rect.min.x + 1, (int)rect.min.y + 1);
|
|
|
- Parallel.For(origin.x / samplingScale, (origin.x + (int)rect.width) / samplingScale, (i) =>
|
|
|
+ Parallel.For(origin.x / SamplingScale, (origin.x + (int)rect.width) / SamplingScale, (i) =>
|
|
|
{
|
|
|
- for (int j = origin.y / samplingScale; j < (origin.y + rect.height) / samplingScale; j++)
|
|
|
+ for (int j = origin.y / SamplingScale; j < (origin.y + rect.height) / SamplingScale; j++)
|
|
|
{
|
|
|
- int ip = i * samplingScale;
|
|
|
- int jp = j * samplingScale;
|
|
|
+ int ip = i * SamplingScale;
|
|
|
+ int jp = j * SamplingScale;
|
|
|
var index = mCameraInfo.CoordToIndex(ip, jp);
|
|
|
//var b = brightness[index];
|
|
|
//var b = (int)Math.Round(ConvBrightness(brightness, (ip, jp)));
|