| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- //using OpenCVForUnity.ImgprocModule;
- //using OpenCVForUnity.CoreModule;
- //using OpenCVForUnity.UnityUtils;
- //using OpenCVForUnity.ImgcodecsModule;
- //using OpenCVForUnity.FaceModule;
- using System;
- using System.Linq;
- using o0.Geometry2D;
- using System.IO;
- using System.Threading.Tasks;
- using ZIM.Image;
- using o0;
- using o0.Project;
- using Color = UnityEngine.Color;
- //public class FindSquaresTest : MonoBehaviour
- //{
- // public List<RawImage> Images;
- // // Start is called before the first frame update
- // void Awake()
- // {
- // /*
- // var v0 = new Vector<float>(5, 0);
- // var v1 = new Vector<float>(0, 5);
- // var dir = v1 - v0;
- // var edgeP = new Vector<float>(dir.y, -dir.x);
- // Debug.Log(edgeP);
- // var p = new Vector<float>(1, 0.5f);
- // var ep = p;
- // var dot = ep.x * edgeP.y - ep.y * edgeP.x;
- // Debug.Log(dot);
- // var line = new Line<float>(v0, v1);
- // Debug.Log("distance: " + new Vector<float>(0, 0).DistanceTo(line));
- // //Debug.Log(new Vector<float>(0, 0).VerticalLineIntersect(line));
- // var line2 = new Line<float>(new Vector<float>(0,0), new Vector<float>(1,1));
- // var point = line.Intersect(line2, out float ta, out float tb, false);
- // {
- // Point p1 = new Point(0, 0);
- // Point p2 = new Point(3, 0);
- // Point p3 = new Point(3.1, 4);
- // Point p4 = new Point(3, 5);
- // Point p5 = new Point(0, 5);
- // Point p6 = new Point(0.1, 4);
- // var array = new Point[] { p1, p2, p3, p4, p5, p6 };
- // var mat = new MatOfPoint2f(array);
- // mat = PointReduce(mat, 4);
- // foreach (var i in mat.toArray())
- // Debug.Log(i);
- // }
- // /**/
-
- // GetComponent<Button>().onClick.AddListener(onClick);
- // }
- // // Update is called once per frame
- // void Update()
- // {
- // if (Input.GetKeyUp(KeyCode.Q))
- // {
- // onClick();
- // }
- // }
- // public void onClick()
- // {
- // var watch = new System.Diagnostics.Stopwatch();
- // watch.Start();
- // var times = new List<double>() { 0.0 };
- // foreach (var i in Images)
- // {
- // if (!i || !i.gameObject.activeSelf)
- // continue;
- // //cvFindSquare(i);
- // //cvHoughLines(i);
- // zimFindLines(i);
- // }
- // times.Add(watch.ElapsedMilliseconds);
- // UnityEngine.Debug.Log("time: " + (times[times.Count - 1] - times[times.Count - 2]));
- // }
- // string SavePath = "output/";
- // void cvFindSquare(RawImage rawImage, double threshold = 30, double maxCosineOfSquare = 0.75)
- // {
- // if (rawImage.texture == null)
- // return;
- // //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
- // Utils.setDebugMode(true);
- // Mat src = new Mat(rawImage.texture.height, rawImage.texture.width, CvType.CV_8UC4);
- // Utils.texture2DToMat((Texture2D)rawImage.texture, src);
- // Imgcodecs.imwrite(SavePath + rawImage.name + "_opencv0.jpg", src);
- // // 图像二值化处理
- // Imgproc.GaussianBlur(src, src, new Size(3, 3), 0);
- // Mat gray = new Mat(src.size(), src.type());
- // Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
- // Mat bin = new Mat(src.size(), src.type());
- // Imgproc.threshold(src, bin, threshold, 255, Imgproc.THRESH_BINARY);
- // Imgproc.cvtColor(bin, bin, Imgproc.COLOR_BGR2GRAY);
- // Imgcodecs.imwrite(SavePath + rawImage.name + "_opencv1.jpg", bin);
- // List<MatOfPoint> contours = new List<MatOfPoint>();
- // Mat hierarchy = new Mat(src.size(), src.type());
- // Imgproc.findContours(bin, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
- // // 查找四边形, 修改自opencv官方的find_squares例子
- // var squares = new SortedList<double, MatOfPoint>();
- // foreach (var cnt in contours)
- // {
- // var cntf = new MatOfPoint2f(cnt.toArray());
- // var cnt_len = Imgproc.arcLength(cntf, true);
- // Imgproc.approxPolyDP(cntf, cntf, 0.025 * cnt_len, true);
- // //Imgproc.drawContours(src, new List<MatOfPoint> { new MatOfPoint(cntf.toArray()) }, -1, new Scalar(255, 0, 0, 255), 4);
- // var area = Math.Abs(Imgproc.contourArea(cntf));
- // if (Math.Abs(cntf.rows() - 6) <= 2 && area > 10000) // && Imgproc.isContourConvex(cntf))
- // {
- // cntf = PointReduce(cntf, 4); // 删减顶点数至4个
- // double maxCosine = 0;
- // var cntfArray = cntf.toArray();
- // for (int j = 2; j < 5; j++)
- // {
- // // find the maximum cosine of the angle between joint edges
- // double cosine = Math.Abs(cvAngle2D(cntfArray[j % 4], cntfArray[j - 2], cntfArray[j - 1]));
- // maxCosine = Math.Max(maxCosine, cosine);
- // }
- // // if cosines of all angles are small
- // // (all angles are ~90 degree) then write quandrange
- // // vertices to resultant sequence
- // if (maxCosine < maxCosineOfSquare)
- // {
- // squares[area] = new MatOfPoint(cntf.toArray());
- // //Imgproc.drawContours(src, squares, -1, new Scalar(255, 0, 0, 255), 1);
- // foreach (var i in cntfArray)
- // Imgproc.circle(src, i, 5, new Scalar(255, 255, 255, 255), -1);
- // }
- // }
- // }
- // if (squares.Count != 0)
- // {
- // Debug.Log("查找到四边形: " + rawImage.name);
- // Imgproc.drawContours(src, new List<MatOfPoint> { squares.Last().Value }, -1, new Scalar(255, 0, 0, 255), 1);
- // Imgcodecs.imwrite(SavePath + rawImage.name + "_opencv2.jpg", src);
- // }
- // else
- // {
- // Debug.Log("不能查找到四边形: " + rawImage.name);
- // }
- // //Mat dst = new Mat(src.size(), src.type(), new Scalar(0, 0, 0, 255, 255));
- // Texture2D texture2 = new Texture2D(src.cols(), src.rows(), TextureFormat.RGBA32, false);
- // Utils.matToTexture2D(src, texture2);
- // rawImage.texture = texture2;
- // Utils.setDebugMode(false);
- // }
- // void cvHoughLines(RawImage rawImage)
- // {
- // //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
- // Utils.setDebugMode(true);
- // Mat src = new Mat(rawImage.texture.height, rawImage.texture.width, CvType.CV_8UC4);
- // Utils.texture2DToMat((Texture2D)rawImage.texture, src);
- // Imgcodecs.imwrite(SavePath + rawImage.name + "_opencv0.jpg", src);
- // // 保留红通道
- // Mat red = new Mat();
- // Core.extractChannel(src, red, 0);
- // for (int i = 0; i < 5; i++)
- // {
- // Imgproc.GaussianBlur(red, red, new Size(5, 5), 3);
- // //Sobel算子
- // Mat x = new Mat(), y = new Mat();
- // Imgproc.Sobel(red, x, CvType.CV_16S, 1, 0);
- // Imgproc.Sobel(red, y, CvType.CV_16S, 0, 1);
- // Core.convertScaleAbs(x, x);
- // Core.convertScaleAbs(y, y);
- // Core.addWeighted(x, 0.5, y, 0.5, 0, red);
- // Imgcodecs.imwrite(SavePath + rawImage.name + $"_opencv1_{i}.jpg", red);
- // }
- // //// 计算Laplacian卷积结果
- // //Mat laplacian = new Mat();
- // //Imgproc.Laplacian(gray, laplacian, CvType.CV_16S);
- // ////数据进行转换成uint8形式
- // //Core.convertScaleAbs(laplacian, laplacian);
- // //Imgcodecs.imwrite(SavePath + rawImage.name + "_opencv1l.jpg", laplacian);
- // //// canny
- // //Imgproc.Canny(gray, gray, 0, 2);
- // //Imgcodecs.imwrite(SavePath + rawImage.name + "_opencv1c.jpg", gray);
- // Mat lines = new Mat();
- // Imgproc.HoughLinesP(red, lines, 3, Math.PI / 180, 30, 100, 10);
- // for (int i = 0; i < lines.rows(); i++)
- // {
- // int[] line = new int[4];
- // lines.get(i, 0, line);//将线对应的极点坐标存到line数组中
- // Imgproc.line(src, new Point(line[0], line[1]), new Point(line[2], line[3]), new Scalar(255, 0, 0, 255), 4, Imgproc.LINE_AA);
- // Debug.Log("线段" + i + ": " + new Point(line[0], line[1]) + ", " + new Point(line[2], line[3]));
- // }
- // Imgcodecs.imwrite(SavePath + rawImage.name + "_opencv2.jpg", src);
- // Texture2D texture2 = new Texture2D(src.cols(), src.rows(), TextureFormat.RGBA32, false);
- // Utils.matToTexture2D(src, texture2);
- // rawImage.texture = texture2;
- // Utils.setDebugMode(false);
- // }
- // void zimFindLines(RawImage rawImage)
- // {
- // //MatrixF2D mat = new MatrixF2D(result.w, result.p);
- // //var t = mat.IdentifyEdgeGradientFull().ToTex();
- // var pixels = ((Texture2D)rawImage.texture).GetPixels();
- // var brightness = new float[pixels.Length];
- // Parallel.For(0, pixels.Length, (i) =>
- // {
- // brightness[i] = getBrightness(pixels[i]);
- // });
- // ImgProcessGray imageGray = new ImgProcessGray(brightness, rawImage.texture.width, rawImage.texture.height);
- // var result = imageGray.FindLines();
- // var t = ArrayToTexture(result.p, result.w, result.h);
- // var bytes = t.EncodeToPNG();
- // File.WriteAllBytes($"output/Variance2.png", bytes);
- // rawImage.texture = t;
- // }
- // Texture2D ArrayToTexture(float[] pixels,int width, int height)
- // {
- // var color = new Color[pixels.Length];
- // Parallel.For(0, pixels.Length, (i) =>
- // {
- // color[i] = new Color(pixels[i], 0, 0);
- // });
- // var texture = new Texture2D(width, height);
- // texture.SetPixels(color);
- // texture.Apply();
- // return texture;
- // }
- // // finds a cosine of angle between vectors
- // // from pt0->pt1 and from pt0->pt2
- // double cvAngle2D(Point pt1, Point pt2, Point pt0)
- // {
- // double dx1 = pt1.x - pt0.x;
- // double dy1 = pt1.y - pt0.y;
- // double dx2 = pt2.x - pt0.x;
- // double dy2 = pt2.y - pt0.y;
- // return (dx1 * dx2 + dy1 * dy2) / Math.Sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
- // }
- // double cvArea(Point p1, Point p2, Point p3)
- // {
- // double _distance(Point p1, Point p2)
- // {
- // return Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
- // }
- // //计算三边长
- // double a = _distance(p1, p2);
- // double b = _distance(p2, p3);
- // double c = _distance(p3, p1);
- // //计算半周长
- // double s = (a + b + c) / 2;
- // //计算面积,注意处理退化情况
- // if (s == 0 || s * (s - a) * (s - b) * (s - c) < 0)
- // return 0;
- // else
- // return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
- // }
- // public float getBrightness(Color c)
- // {
- // return 0.59f * c.r + 0.3f * c.g + 0.11f * c.b; // 红色为主
- // }
- // MatOfPoint2f PointReduce(MatOfPoint2f cnt, int dstCount) // 随手写的,还能优化
- // {
- // var array = cnt.toList();
- // if (array.Count <= dstCount)
- // return cnt;
- // while (array.Count > dstCount)
- // {
- // var areas = new SortedList<double, int>();
- // for (int j = 1; j < array.Count + 1; j++)
- // areas[cvArea(array[j - 1], array[j % array.Count], array[(j + 1) % array.Count])] = j % array.Count;
- // array.RemoveAt(areas.First().Value);
- // }
- // return new MatOfPoint2f(array.ToArray());
- // }
- //}
|