Procházet zdrojové kódy

Merge branch 'ZIM-DEV' into DEV-旧版UI

ZIM před 11 měsíci
rodič
revize
7d9305553b

binární
Assets/InfraredProject/WebCamera/Image/Debug3.jpg


+ 123 - 0
Assets/InfraredProject/WebCamera/Image/Debug3.jpg.meta

@@ -0,0 +1,123 @@
+fileFormatVersion: 2
+guid: a36d3e6d746b56c4980638b4970dae88
+TextureImporter:
+  internalIDToNameTable: []
+  externalObjects: {}
+  serializedVersion: 12
+  mipmaps:
+    mipMapMode: 0
+    enableMipMap: 0
+    sRGBTexture: 1
+    linearTexture: 0
+    fadeOut: 0
+    borderMipMap: 0
+    mipMapsPreserveCoverage: 0
+    alphaTestReferenceValue: 0.5
+    mipMapFadeDistanceStart: 1
+    mipMapFadeDistanceEnd: 3
+  bumpmap:
+    convertToNormalMap: 0
+    externalNormalMap: 0
+    heightScale: 0.25
+    normalMapFilter: 0
+  isReadable: 1
+  streamingMipmaps: 0
+  streamingMipmapsPriority: 0
+  vTOnly: 0
+  ignoreMasterTextureLimit: 0
+  grayScaleToAlpha: 0
+  generateCubemap: 6
+  cubemapConvolution: 0
+  seamlessCubemap: 0
+  textureFormat: 1
+  maxTextureSize: 2048
+  textureSettings:
+    serializedVersion: 2
+    filterMode: 1
+    aniso: 1
+    mipBias: 0
+    wrapU: 1
+    wrapV: 1
+    wrapW: 0
+  nPOTScale: 0
+  lightmap: 0
+  compressionQuality: 50
+  spriteMode: 1
+  spriteExtrude: 1
+  spriteMeshType: 1
+  alignment: 0
+  spritePivot: {x: 0.5, y: 0.5}
+  spritePixelsToUnits: 100
+  spriteBorder: {x: 0, y: 0, z: 0, w: 0}
+  spriteGenerateFallbackPhysicsShape: 1
+  alphaUsage: 1
+  alphaIsTransparency: 1
+  spriteTessellationDetail: -1
+  textureType: 8
+  textureShape: 1
+  singleChannelComponent: 0
+  flipbookRows: 1
+  flipbookColumns: 1
+  maxTextureSizeSet: 0
+  compressionQualitySet: 0
+  textureFormatSet: 0
+  ignorePngGamma: 0
+  applyGammaDecoding: 0
+  cookieLightType: 0
+  platformSettings:
+  - serializedVersion: 3
+    buildTarget: DefaultTexturePlatform
+    maxTextureSize: 2048
+    resizeAlgorithm: 0
+    textureFormat: -1
+    textureCompression: 1
+    compressionQuality: 50
+    crunchedCompression: 0
+    allowsAlphaSplitting: 0
+    overridden: 0
+    androidETC2FallbackOverride: 0
+    forceMaximumCompressionQuality_BC6H_BC7: 0
+  - serializedVersion: 3
+    buildTarget: Standalone
+    maxTextureSize: 2048
+    resizeAlgorithm: 0
+    textureFormat: -1
+    textureCompression: 1
+    compressionQuality: 50
+    crunchedCompression: 0
+    allowsAlphaSplitting: 0
+    overridden: 0
+    androidETC2FallbackOverride: 0
+    forceMaximumCompressionQuality_BC6H_BC7: 0
+  - serializedVersion: 3
+    buildTarget: Android
+    maxTextureSize: 2048
+    resizeAlgorithm: 0
+    textureFormat: -1
+    textureCompression: 1
+    compressionQuality: 50
+    crunchedCompression: 0
+    allowsAlphaSplitting: 0
+    overridden: 0
+    androidETC2FallbackOverride: 0
+    forceMaximumCompressionQuality_BC6H_BC7: 0
+  spriteSheet:
+    serializedVersion: 2
+    sprites: []
+    outline: []
+    physicsShape: []
+    bones: []
+    spriteID: 5e97eb03825dee720800000000000000
+    internalID: 0
+    vertices: []
+    indices: 
+    edges: []
+    weights: []
+    secondaryTextures: []
+    nameFileIdTable: {}
+  spritePackingTag: 
+  pSDRemoveMatte: 0
+  pSDShowRemoveMatteOption: 0
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 38 - 21
Assets/InfraredProject/WebCamera/Script/ZIM/InfraredLocate/LineIdentify/LineGuess.cs

@@ -34,18 +34,18 @@ namespace o0.Project
             }
 
             // 得到差异的幅度
-            public float GetDiffExtent()
-            {
-                if (diffExtent == -1)
-                {
-                    var sum = 0.0f;
-                    foreach (var i in DiffList)
-                        sum += i * i;
-                    return diffExtent = sum / DiffList.Count;
-                }
-                else
-                    return diffExtent;
-            }
+            //public float GetDiffExtent()
+            //{
+            //    if (diffExtent == -1)
+            //    {
+            //        var sum = 0.0f;
+            //        foreach (var i in DiffList)
+            //            sum += i * i;
+            //        return diffExtent = sum / DiffList.Count;
+            //    }
+            //    else
+            //        return diffExtent;
+            //}
 
             // 得到数据的幅度(和0比)
             public float GetDataExtent()
@@ -82,8 +82,21 @@ namespace o0.Project
             var dir = (line.Line.B - line.Line.A).Normalized;
             var vertical = new Vector(-dir.y, dir.x) * (GradientLength / 2);
             var verticalNormal = vertical.Normalized;
-            int lineABStep = (int)(MinLineLength / 5);
-            var ll = line.Line.Length;
+            float lineABStep = MinLineLength / 15;
+
+            float lineGuessStart;
+            float lineGuessEnd;
+            if (line.Line.Length > 2 * MinLineLength)
+            {
+                lineGuessStart = 7.5f * lineABStep;
+                lineGuessEnd = line.Line.Length - 7.5f * lineABStep;        // 线的两端不计算
+            }
+            else
+            {
+                lineGuessStart = lineABStep;
+                lineGuessEnd = line.Line.Length - lineABStep;
+            }
+
             var gradData = new List<float>();      // 直线两侧梯度的平均值,垂直于直线变化
 
             var a0 = line.Line.A - vertical;
@@ -92,7 +105,7 @@ namespace o0.Project
             {
                 var aN = a0 + verticalNormal * n;
                 var gradThisLine = new List<float>();
-                for (int i = 0; i <= ll; i += lineABStep)
+                for (float i = lineGuessStart; i <= lineGuessEnd; i += lineABStep)
                 {
                     var point = aN + dir * i;
                     gradThisLine.Add(ScreenLocateMatList[line.Batch][(int)point.x, (int)point.y]);
@@ -120,24 +133,28 @@ namespace o0.Project
                     subList.Add(new SubLineData(gradChangeData[i], newDiff));
                 diff = newDiff;
             }
-            int maxDiffIndex = -1;
-            if (subList.Count >= 3) 
-                maxDiffIndex = subList.MaxIndex((a, b) => a.GetDiffExtent().CompareTo(b.GetDiffExtent()));      // 找到分段里和平均数差异最大的分段,计算数据幅度的时候不计入
+            int maxSubDataIndex = subList.MaxIndex((a, b) => a.GetDataExtent().CompareTo(b.GetDataExtent()));  // 找到分段里和平均数差异最大的分段
+
+            //TODO 这里要不要考虑加个阈值,过滤掉梯度太小的线条? -----------------------------------
+
+            if (subList.Count < 3)
+                maxSubDataIndex = -1;  // ≥3的时候把SubDataExtent最高的这段剔除掉再计算
 
             var lineGuessExtent = 0.0f;
             for (int i = 0; i < subList.Count; i++)
             {
-                if (i != maxDiffIndex)
+                if (i != maxSubDataIndex)
                     lineGuessExtent += subList[i].GetDataExtent();
             }
-            if (maxDiffIndex == -1)
+            if (maxSubDataIndex == -1)
                 lineGuessExtent /= subList.Count;
             else
                 lineGuessExtent /= subList.Count - 1;
 
             lineGuessExtent = (float)Math.Sqrt(lineGuessExtent);
             var countFactor = -0.002f * Math.Abs(subList.Count - 3) + 0.015f;  // 3时最大
-            //Debug.Log("maxDiffIndex: " + maxDiffIndex + ", sub count: " + subList.Count + ", lineGuess: " + (1 - lineGuessExtent + countFactor));
+            if (ScreenLocate.Main.DebugOnZIMDemo)
+                Debug.Log("line.Index" + line.ScreenLineIndex + ", maxDiffIndex: " + maxSubDataIndex + ", sub count: " + subList.Count + ", lineGuess: " + (1 - lineGuessExtent + countFactor));
             return 1 - lineGuessExtent + countFactor;
         }
     }

+ 18 - 19
Assets/InfraredProject/WebCamera/Script/ZIM/InfraredLocate/ScreenIdentification.cs

@@ -679,29 +679,22 @@ namespace o0.Project
             //sw.Start();
 
             //读取数据
+            int batchCount;
             if (debugImages != null && debugImages.Count != 0)
             {
+                batchCount = debugImages.Count;
                 var dSize = debugImages.First().Size();
                 foreach (var i in debugImages)
                 {
                     Debug.Log($"<color=aqua>Debug {i.name}</color>");
                     if (i.Size() != dSize)
                         throw new InvalidOperationException("Multiple Debug textures have different sizes");
-                    PixelsMultipleBatches.Add(i.GetPixels());
+                    //PixelsMultipleBatches.Add(i.GetPixels());
                 }
 
             }
             else    // 获得屏幕差值
             {
-                var maxWhite = 0f;
-                foreach (var i in ScreenWhiteTexture)
-                {
-                    var m = i.x > i.y ? (i.x > i.z ? i.x : i.z) : (i.y > i.z ? i.y : i.z);
-                    if (maxWhite < m)
-                        maxWhite = m;
-                }
-                var scale = 1.0f / maxWhite;        // 放大对比度
-
                 var differPixel = new UnityEngine.Color[Size.x * Size.y];
                 var whitePixel = new UnityEngine.Color[Size.x * Size.y];
                 Parallel.For(0, Size.x, x =>
@@ -709,28 +702,33 @@ namespace o0.Project
                     for (int y = 0; y < Size.y; y++)
                     {
                         var i = y * Size.x + x;
-                        var d = ScreenWhiteTexture[i] * scale - ScreenBlackTexture[i];
+                        var d = ScreenWhiteTexture[i] - ScreenBlackTexture[i];
                         differPixel[i] = new UnityEngine.Color(d.x, d.y, d.z);
-                        whitePixel[i] = new UnityEngine.Color(ScreenWhiteTexture[i].x, ScreenWhiteTexture[i].y, ScreenWhiteTexture[i].z) * scale;
+                        whitePixel[i] = new UnityEngine.Color(ScreenWhiteTexture[i].x, ScreenWhiteTexture[i].y, ScreenWhiteTexture[i].z);
                     }
                 });
                 PixelsMultipleBatches.Add(differPixel);     // 色差图
                 PixelsMultipleBatches.Add(whitePixel);      // 原图
+                batchCount = PixelsMultipleBatches.Count;
             }
 
             int conSize = (int)Math.Ceiling(0.007f * Size.y) * 2 + 1;
             conSize = Math.Max(conSize, 7);     // 设置最小为7
 
-            float minLength = conSize * 6f;
+            float minLength = conSize * 7.7f;
             minLength = locateIndex == -1 ? minLength : minLength * areaPercent;    // minLength需要按areaPercent比例缩小
             string log = $"[Log][ScreenLocate Auto] Size: ({Size.x},{Size.y}), 卷积核Size: {conSize}, 最小线段长度: {minLength}";
 
             var allLines = new List<LineIdentified>();
             List<Texture2D> LocateTexTemp = new List<Texture2D>();
             List<Matrix> ScreenLocateMatList = new List<Matrix>();
-            foreach (var batch in PixelsMultipleBatches.Index())
+            for (int batch = 0; batch < batchCount; batch++)
             {
-                var locateTex = ToLocateTex(PixelsMultipleBatches[batch]);
+                Texture2D locateTex;
+                if (debugImages != null && debugImages.Count != 0)
+                    locateTex = debugImages[batch];
+                else
+                    locateTex = ToLocateTex(PixelsMultipleBatches[batch]);
                 LocateTexTemp.Add(locateTex);
                 var ScreenLocateMat = locateTex.Too0Mat();        // 用于获取Lines的Matrix
                 var lineCount = ZIMIdentifyQuadLSD(
@@ -742,7 +740,7 @@ namespace o0.Project
                 log += $"\r\n识别图片{batch}, 识别到的线段数量为: {lineCount}";
                 ScreenLocateMatList.Add(ScreenLocateMat);
             }
-            Texture2D ScreenLocateTexture = LocateTexTemp[0];       // for output
+            Texture2D ScreenLocateTexture = LocateTexTemp[0];       // 色差图,for output
 
             // LSD计算得到的矩阵尺寸较小(因为卷积),这里必须进行位移
             // 新增:根据阈值筛去梯度太低的线段
@@ -1009,7 +1007,7 @@ namespace o0.Project
             {
                 var gM = ScreenGrad(line);
                 float e = (float)Math.Sqrt(Math.Ceiling(line.Line.Length / minLength));       // 长度系数,筛选时梯度更大、长度更长的线段更优
-                float d = (5 - distanceRatio) / 4;             // 距离系数,距离越近,系数越大
+                float d = (1.3f - distanceRatio) / 0.3f;             // 距离系数,距离越近,系数越大
 
                 line.ZIMGradient = e * d * gM;      // 记录一下综合梯度,全自动新增的功能会二次使用
                 return line.ZIMGradient;
@@ -1124,7 +1122,7 @@ namespace o0.Project
                     }
                 }
             }
-            var (interA, interB) = FindInterLinePair(new LineGuess(screenLocateMatList, gradientLength * 2, minLength), interSelectable, 12);
+            var (interA, interB) = FindInterLinePair(new LineGuess(screenLocateMatList, gradientLength * 2, minLength), interSelectable, 8);
             if (interA != null)   // 替换上一步筛选的结果中的部分边,得到最终的结果
                 resultAuto[interA.ScreenLineIndex] = interA;
             if (interB != null)
@@ -1143,7 +1141,8 @@ namespace o0.Project
             {
                 if (lineGuess.GuessIsLine(line))        // 评价是不是Line, 并且找到离中心点最近的
                 {
-                    Debug.Log("[ScreenIdentification] guess is line: (index)" + line.ScreenLineIndex);
+                    if (ScreenLocate.Main.DebugOnZIMDemo)
+                        Debug.Log($"[ScreenIdentification] {interSelectable.IndexOf(line)}, guess is line: (index)" + line.ScreenLineIndex);
                     if (selected[line.ScreenLineIndex] == null || selected[line.ScreenLineIndex].DistanceToMiddle > line.DistanceToMiddle)
                         selected[line.ScreenLineIndex] = line;
                 }