Sfoglia il codice sorgente

Merge branch 'ZIM_Demo' into new-ui-infrared

ZIM 1 anno fa
parent
commit
1a207955dc

BIN
Assets/InfraredProject/WebCamera/Image/1-测试图片.png


+ 0 - 0
Assets/InfraredProject/WebCamera/Image/测试图片.png.meta → Assets/InfraredProject/WebCamera/Image/1-测试图片.png.meta


BIN
Assets/InfraredProject/WebCamera/Image/2-测试图片.png


BIN
Assets/InfraredProject/WebCamera/Image/3-测试图片.png


+ 123 - 0
Assets/InfraredProject/WebCamera/Image/3-测试图片.png.meta

@@ -0,0 +1,123 @@
+fileFormatVersion: 2
+guid: 38728ab62f34f7843be0a0ae3a7cc178
+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: 

BIN
Assets/InfraredProject/WebCamera/Image/测试图片.png


+ 57 - 12
Assets/InfraredProject/WebCamera/Script/ZIM/InfraredLocate/ScreenIdentification.cs

@@ -2,6 +2,7 @@
 
 using o0.Geometry2D.Float;
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Threading.Tasks;
@@ -618,7 +619,7 @@ namespace o0.Project
             //读取数据
             if (debugImage != null)
             {
-                Debug.Log($"Debug {debugImage.name}");
+                Debug.Log($"<color=aqua>Debug {debugImage.name}</color>");
                 differPixel = debugImage.GetPixels();
             }
             else    // 获得屏幕差值
@@ -666,7 +667,24 @@ namespace o0.Project
             Debug.Log($"[ScreenIdentification] Size: ({Size.x},{Size.y}), 卷积核Size: {conSize}, 最小线段长度: {minLength}");
             var (edge, edgeDir) = ScreenLocateTexLightedMat.zimIdentifyEdgeGradientAny(conSize);
 
-            var quadLines = ScreenLocateTexLightedMat.ZIMIdentifyQuadLSD(edge, edgeDir, out Line[] oldLines, out List<Line> lightLines, Screen, conSize, conSize, minLength);
+            var quadLines = ScreenLocateTexLightedMat.ZIMIdentifyQuadLSD(
+                edge, edgeDir, 
+                out Line[] oldLines, out List<Line> possibleLines,out List<(Line, float, float)> allLines, 
+                Screen, conSize, conSize, minLength);
+
+
+
+            // 将 allLines 输出一张图片
+            var allLinesMap = new Matrix(ScreenLocateTexLightedMat.Size, Tiling: true);
+            foreach (var l in allLines)
+            {
+                if (l.Item1 != null)
+                    o0Extension.DrawLine(allLinesMap, l.Item1, (x, y) => 3, new Geometry2D.Float.Vector(0, 2), true);
+            }
+            var allLinesTex = allLinesMap.ToTexRGBA(FloatValueToColor);
+            ScreenLocate.DebugTexture(1, allLinesTex);
+
+
 
             // 将识别到的边画出来,并判断能否拼成屏幕,能拼成则设置ScreenMap
             // 线段顺序: 下、右、上、左
@@ -706,9 +724,11 @@ namespace o0.Project
                 quadTemp.Add(screenQuad.Quad);
             }
 
+
+
             // 还需要输出一张识别结果图,包含干扰线段
             var LSDLineMap = new Matrix(ScreenLocateTexLightedMat.Size, Tiling: true);
-            foreach (var l in lightLines)
+            foreach (var l in possibleLines)
             {
                 if (l != null && !quadLines.Contains(l))
                     o0Extension.DrawLine(LSDLineMap, l, (x, y) => 3, new Geometry2D.Float.Vector(0, 2), true); // 其他的备选线段
@@ -725,18 +745,15 @@ namespace o0.Project
             }
             ChoosableLineTex = LSDLineMap.ToTexRGBA(FloatValueToColor);
 
+
+
             // 是否将图片保存到本地
             if (ScreenLocate.Main.SaveToggle.isOn)
             {
-                var time = DateTime.Now.ToString("yyyyMMdd_HHmmss");
-                var FileSavePath = $"{time}屏幕定位数据.bin";
-                FileSavePath.FileWriteByte(ScreenWhiteTexture);
-
-                var bytes = ScreenLocateTexLighted.EncodeToPNG();
-                File.WriteAllBytes($"{time}屏幕.png", bytes);
-                bytes = ScreenQuadTex.EncodeToPNG();
-                File.WriteAllBytes($"{time}屏幕边框识别.png", bytes);
-                Debug.Log("ScreenLocateTexture Saved To: " + FileSavePath);
+                var FileDirectory = $"Debug_屏幕定位图片/";
+                ScreenLocate.Main.StartCoroutine(SaveImagesIEnum(FileDirectory,
+                    $"屏幕识别Size: ({Size.x},{Size.y}), 卷积核Size: {conSize}, 最小线段长度: {minLength}",
+                    ScreenLocateTex, allLinesTex, ScreenQuadTex));
             }
 
             //times.Add(watch.ElapsedMilliseconds);
@@ -755,5 +772,33 @@ namespace o0.Project
             UnityEngine.Object.Destroy(ScreenLocateTex);
         }
 
+        IEnumerator SaveImagesIEnum(string FileDirectory, string log,  Texture2D ScreenLocateTex, Texture2D allLinesTex, Texture2D ScreenQuadTex)
+        {
+            if (!Directory.Exists(FileDirectory))
+                Directory.CreateDirectory(FileDirectory);
+            var time = DateTime.Now.ToString("yyyyMMdd_HHmmss");
+
+            yield return null;
+            var pngData = (ScreenLocate.Main.outputTexture2D[7] as Texture2D)?.EncodeToPNG();
+            if (pngData != null)
+                File.WriteAllBytes($"{FileDirectory}{time}A屏幕原图.png", pngData);
+            yield return null;
+            var pngData1 = ScreenLocateTex.EncodeToPNG();
+            if (pngData1 != null)
+                File.WriteAllBytes($"{FileDirectory}{time}B黑白色差.png", pngData1);
+            yield return null;
+            var pngData2 = allLinesTex.EncodeToPNG();
+            if (pngData2 != null)
+                File.WriteAllBytes($"{FileDirectory}{time}C全部识别线段.png", pngData2);
+            yield return null;
+            var pngData3 = ScreenQuadTex.EncodeToPNG();
+            if (pngData3 != null)
+                File.WriteAllBytes($"{FileDirectory}{time}D识别结果.png", pngData3);
+            yield return null;
+            Debug.Log($"<color=aqua>({time}) 屏幕识别图片保存至:程序根目录/{FileDirectory}</color>");
+            log += $"\r\n屏幕原图保存{pngData != null}, \r\n黑白色差保存{pngData1 != null}, \r\n全部识别线段保存{pngData2 != null}, \r\n识别结果保存{pngData3 != null}, ";
+            File.WriteAllText($"{FileDirectory}{time}_日志.log", log);
+        }
+
     }
 }

+ 2 - 2
Assets/InfraredProject/WebCamera/zimWebCamera.unity

@@ -1545,7 +1545,7 @@ MonoBehaviour:
   onValueChanged:
     m_PersistentCalls:
       m_Calls: []
-  m_IsOn: 0
+  m_IsOn: 1
 --- !u!1 &614532336
 GameObject:
   m_ObjectHideFlags: 0
@@ -4292,7 +4292,7 @@ MonoBehaviour:
       m_Calls: []
   m_FontData:
     m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
-    m_FontSize: 36
+    m_FontSize: 7
     m_FontStyle: 0
     m_BestFit: 0
     m_MinSize: 2

+ 3 - 3
Assets/InfraredProject/o0/o0InfraredIdentification.cs

@@ -132,7 +132,7 @@ namespace o0.Project
                     //o0WebCamera.DebugTexture(4, ScreenLocateTexLighted.Too0Mat().IdentifyEdgeGradient().ToTex());
                     o0WebCamera.DebugTexture(4, edge.ToTex());
 
-                    var quadLines = ScreenLocateTexLightedMat.ZIMIdentifyQuadLSD(edge, edgeDir, out _,  out List<Geometry2D.Float.Line> lightLines, null, 15, 100);
+                    var quadLines = ScreenLocateTexLightedMat.ZIMIdentifyQuadLSD(edge, edgeDir, out _,  out List<Geometry2D.Float.Line> possibleLines, out _, null, 15, 100);
 
                     var drawLineMap = new Matrix(edge.Size, Tiling: true);
                     //drawLineMap.DrawLine(quadLines[0], (x, y) => 1, new Geometry2D.Float.Vector(0, 10));
@@ -154,9 +154,9 @@ namespace o0.Project
                     o0WebCamera.DebugTexture(5, drawLineMap.ToTex());
 
                     drawLineMap = new Matrix(edge.Size, Tiling: true);
-                    for (int i = 0; i < lightLines.Count; i++)
+                    for (int i = 0; i < possibleLines.Count; i++)
                     {
-                        var l = lightLines[i];
+                        var l = possibleLines[i];
                         if (i < 4)
                             drawLineMap.DrawLine(l, (x, y) => 1, new Geometry2D.Float.Vector(0, 10));
 

+ 9 - 20
Assets/InfraredProject/o0/zimIdentifyLineLSD.cs

@@ -92,7 +92,7 @@ namespace o0.Project
 
         // 返回四边形的四条边,List长度一定是4 (如果没有识别到就是null),且线段顺序是: 下、右、上、左
         public static List<Line> ZIMIdentifyQuadLSD(this Matrix screenLocateMat, Matrix edgeMat, Matrix edgeDirMat,
-            out Line[] oldLines, out List<Line> lightLines, ScreenMap screen, 
+            out Line[] oldLines, out List<Line> possibleLines, out List<(Line, float, float)> allLines, ScreenMap screen, 
             float conSize, float gradientLength, float minLength = 100)
         {
             // 加权平均
@@ -117,24 +117,13 @@ namespace o0.Project
             }
             avgPoint /= valueSum;
 
-            var lines = edgeMat.IdentifyLineLSD(edgeDirMat, minLength, 20, LineCaptureSize: new Vector(0, 5));
-
-            {
-                // 将lines输出一张图片
-                var LSDLineMap = new Matrix(new Geometry2D.Vector<int>((int)(edgeMat.Size.x + conSize - 1), (int)(edgeMat.Size.y + conSize - 1)), Tiling: true);
-                foreach (var l in lines)
-                {
-                    if (l.Item1 != null)
-                        o0Extension.DrawLine(LSDLineMap, l.Item1, (x, y) => 3, new Geometry2D.Float.Vector(0, 2), true); // 其他的备选线段
-                }
-                ScreenLocate.DebugTexture(1, LSDLineMap.ToTexRGBA(ScreenIdentification.FloatValueToColor));
-            }
+            allLines = edgeMat.IdentifyLineLSD(edgeDirMat, minLength, 20, LineCaptureSize: new Vector(0, 5));
 
             //Debug.Log("[IdentifyLineLSD] lines.Count: " + lines.Count);
             // LSD计算得到的矩阵尺寸较小(因为卷积),这里必须进行位移
             var offset = new Vector((conSize - 1) / 2, (conSize - 1) / 2);
-            for (int i = 0; i < lines.Count; i++)
-                lines[i] = (lines[i].Item1 + offset, lines[i].Item2, lines[i].Item3);
+            for (int i = 0; i < allLines.Count; i++)
+                allLines[i] = (allLines[i].Item1 + offset, allLines[i].Item2, allLines[i].Item3);
 
             // 沿直线计算综合梯度(梯度乘以长度系数)
             float estimateGradient(Line line)
@@ -158,7 +147,7 @@ namespace o0.Project
 
             // 下、右、上、左
             var quadLines = new List<(float, Line)>[4] {new List<(float, Line)>(), new List<(float, Line)>(), new List<(float, Line)>(), new List<(float, Line)>() };
-            lightLines = new List<Line>();
+            possibleLines = new List<Line>();
             oldLines = null;
 
             // 如果已有定位数据,根据现有数据筛选线条
@@ -172,7 +161,7 @@ namespace o0.Project
 
                 var pedals = oldLines.Select((i) => o0Extension.PointPedal(i, avgPoint)).ToArray();     // 当前定位的垂足,下、右、上、左
 
-                foreach (var i in lines)
+                foreach (var i in allLines)
                 {
                     float minDistance = float.MaxValue;
                     int index = -1;
@@ -189,16 +178,16 @@ namespace o0.Project
                     if (minDistance < calibration)      // 垂足的距离足够近
                     {
                         quadLines[index].Add((estimateGradient(i.Item1), i.Item1));
-                        lightLines.Add(i.Item1);
+                        possibleLines.Add(i.Item1);
                     }
                 }
             }
             else
             {
                 var avaAngleHalf = 75f;
-                foreach (var (line, sum, gradient) in lines)
+                foreach (var (line, sum, gradient) in allLines)
                 {
-                    lightLines.Add(line);
+                    possibleLines.Add(line);
 
                     var a = (avgPoint - (line.A + line.B) / 2).DegreeToXAxis();
                     //Debug.Log(a + ", " + gradient + ", " + sum);