// Update is called once per frame void Update() { tex2 = MakeTexture2D(carm.targetTexture); Mat imgMat = new Mat(tex2.height, tex2.width, CvType.CV_8UC3); Utils.texture2DToMat(tex2, imgMat); //GrayScale 생성 Mat grayMat = new Mat(); Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGB2GRAY); //Canny적용 Mat cannyMat = new Mat(); Imgproc.Canny(grayMat, cannyMat, 70, 210); //ROI 설정 grayMat.adjustROI(cannyMat.height(), 0, cannyMat.width() * 4 / 10, cannyMat.width()); //HoughLine생성 Mat lines = new Mat(); Imgproc.HoughLinesP(cannyMat, lines, 1, Mathf.PI / 180, 30, 100, 20); int[] linesArray = new int[lines.cols() * lines.rows() * lines.channels()]; lines.get(0, 0, linesArray); for (int i = 0; i < linesArray.Length; i = i + 4) { //Debug.Log(Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg); if (0 <= Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { if (80 > Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { Imgproc.line(imgMat, new Point(linesArray[i + 0], linesArray[i + 1]), new Point(linesArray[i + 2], linesArray[i + 3]), new Scalar(255, 0, 0), 4); } } if (180 >= Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { if (100 < Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { Imgproc.line(imgMat, new Point(linesArray[i + 0], linesArray[i + 1]), new Point(linesArray[i + 2], linesArray[i + 3]), new Scalar(255, 0, 0), 4); } } } Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(imgMat, texture); GameObject.Find("test3").GetComponent <Renderer>().material.mainTexture = texture; }
private void Start() { // Imgproc으로 이미지를 수정할 때마다 이미지가 반전된다 // Core.flip(inputMat, outputMat, 0)으로 원래대로 회전 할 수 있음 originMat = new Mat(roadTexture.height, roadTexture.width, CvType.CV_8UC4); Utils.texture2DToMat(roadTexture, originMat); DrawMat(quad_1, originMat); Debug.Log("LOAD " + roadTexture.width.ToString() + "x" + roadTexture.height.ToString() + " :: roadTexture"); outputMat = originMat.clone(); inputMat = originMat.clone(); // 원본 - > 흑백 grayMat = new Mat(); Imgproc.cvtColor(inputMat, grayMat, Imgproc.COLOR_BGR2GRAY); DrawMat(quad_2, grayMat); // 흑백 - > 가우스 필터 흑백 gaussianMat = new Mat(); Imgproc.GaussianBlur(grayMat, gaussianMat, gaussianMat.size(), 2, 2); DrawMat(quad_3, gaussianMat); // 가우스 필터 흑백 - > 테두리 contourMat = new Mat(); Imgproc.Canny(gaussianMat, contourMat, 50, 200); DrawMat(quad_4, contourMat); // 테두리 - > 관심영역 씌운 테두리 regionMat = WriteRegionOfInterest(contourMat); DrawMat(quad_5, regionMat); // 관심영역 씌운 테두리 - > hough 알고리즘으로 추출한 선 좌표 Matrix houghMat = new Mat(); Imgproc.HoughLinesP(regionMat, houghMat, 2, Mathf.PI / 180, 90, 120, 150); Debug.Log(houghMat.dump()); // 선 좌표 Matrix - > 선만 그려진 Mat lineMat = Mat.zeros(outputMat.rows(), outputMat.cols(), outputMat.type()); for (int x = 0; x < houghMat.rows(); x++) { Point pt1 = new Point(houghMat.get(x, 0)[0], houghMat.get(x, 0)[1]); Point pt2 = new Point(houghMat.get(x, 0)[2], houghMat.get(x, 0)[3]); Debug.Log(pt1.ToString() + "/" + pt2.ToString()); Imgproc.line(lineMat, pt1, pt2, new Scalar(255, 0, 0), 4, Imgproc.LINE_AA, 0); } // 선만 그려진 Mat와 원본을 합침 Core.addWeighted(lineMat, 0.8, inputMat, 1, 0, outputMat); DrawMat(quad_6, outputMat); }
void UpdateLines() { // Detect lines. Imgproc.HoughLinesP(cannyMat, houghLines, 1.0, Mathf.PI / 180.0, 50, 50.0, 10.0); // // Calculate the lines' screen coordinates and // world coordinates. // // Clear the previous coordinates. lines.Clear(); // Iterate over the lines. int numHoughLines = houghLines.cols() * houghLines.rows() * houghLines.channels(); int[] houghLinesArray = new int[numHoughLines]; houghLines.get(0, 0, houghLinesArray); for (int i = 0; i < numHoughLines; i += 4) { // Convert lines' image coordinates to screen coordinates. Vector2 screenPoint0 = ConvertToScreenPosition( houghLinesArray[i], houghLinesArray[i + 1]); Vector2 screenPoint1 = ConvertToScreenPosition( houghLinesArray[i + 2], houghLinesArray[i + 3]); // Convert screen coordinates to world // coordinates based on raycasting. Vector3 worldPoint0 = ConvertToWorldPosition( screenPoint0); Vector3 worldPoint1 = ConvertToWorldPosition( screenPoint1); Line line = new Line( screenPoint0, screenPoint1, worldPoint0, worldPoint1); lines.Add(line); } }
//finds lines in image void FindLines() { Mat mainMat = new Mat(baseTexture.height, baseTexture.width, CvType.CV_8UC3); Mat grayMat = new Mat(); //Convert Texture2d to Matrix Utils.texture2DToMat(baseTexture, mainMat); //copy main matrix to grayMat mainMat.copyTo(grayMat); //Convert color to gray Imgproc.cvtColor(grayMat, grayMat, Imgproc.COLOR_BGR2GRAY); //Convert to canny edges Mat edges = new Mat(); Imgproc.Canny(grayMat, edges, 50, 200); //convert to lines Mat lines = new Mat(edges.rows(), edges.cols(), CvType.CV_32SC4); int minLineLength = 50; int maxLineGap = 150; Imgproc.HoughLinesP(edges, lines, 1, Mathf.PI / 180, 75, minLineLength, maxLineGap); Debug.Log("lines.toString() " + lines.ToString()); Debug.Log("lines.dump()" + lines.dump()); int[] linesArray = new int[lines.cols() * lines.rows() * lines.channels()]; lines.get(0, 0, linesArray); for (int i = 0; i < linesArray.Length; i = i + 4) { Imgproc.line(grayMat, new Point(linesArray[i + 0], linesArray[i + 1]), new Point(linesArray[i + 2], linesArray[i + 3]), new Scalar(255, 0, 0), 2); } Texture2D finalTexture = new Texture2D(grayMat.cols(), grayMat.rows(), TextureFormat.RGB24, false); Utils.matToTexture2D(grayMat, finalTexture); targetRawImage.texture = finalTexture; }
// Use this for initialization void Start() { Texture2D imgTexture = Resources.Load("chessboard") as Texture2D; Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Utils.texture2DToMat(imgTexture, imgMat); Debug.Log("imgMat dst ToString " + imgMat.ToString()); Mat grayMat = new Mat(); Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGB2GRAY); Imgproc.Canny(grayMat, grayMat, 50, 200); Mat lines = new Mat(); Imgproc.HoughLinesP(grayMat, lines, 1, Mathf.PI / 180, 50, 50, 10); // Debug.Log ("lines toStirng " + lines.ToString ()); // Debug.Log ("lines dump" + lines.dump ()); int[] linesArray = new int[lines.cols() * lines.rows() * lines.channels()]; lines.get(0, 0, linesArray); for (int i = 0; i < linesArray.Length; i = i + 4) { Core.line(imgMat, new Point(linesArray [i + 0], linesArray [i + 1]), new Point(linesArray [i + 2], linesArray [i + 3]), new Scalar(255, 0, 0), 2); } Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(imgMat, texture); gameObject.GetComponent <Renderer> ().material.mainTexture = texture; }
void UpdateLines() { // Detect lines. Imgproc.HoughLinesP(cannyMat, houghLines, 1.0, Mathf.PI / 180.0, 50, 50.0, 10.0); // // Calculate the lines' screen coordinates and // world coordinates. // // Clear the previous coordinates. lines.Clear(); // Count the elements in the matrix of Hough lines. // Each line should have 4 elements: // { x_start, y_start, x_end, y_end } int numHoughLineElems = houghLines.cols() * houghLines.rows() * houghLines.channels(); if (numHoughLineElems == 0) { return; } // Convert the matrix of Hough circles to a 1D array: // { x_start_0, y_start_0, x_end_0, y_end_0, ..., // x_start_n, y_start_n, x_end_n, y_end_n } int[] houghLinesArray = new int[numHoughLineElems]; houghLines.get(0, 0, houghLinesArray); // Iterate over the lines. for (int i = 0; i < numHoughLineElems; i += 4) { // Convert lines' image coordinates to // screen coordinates. Vector2 screenPoint0 = ConvertToScreenPosition( houghLinesArray[i], houghLinesArray[i + 1]); Vector2 screenPoint1 = ConvertToScreenPosition( houghLinesArray[i + 2], houghLinesArray[i + 3]); // Convert screen coordinates to world // coordinates based on raycasting. Vector3 worldPoint0 = ConvertToWorldPosition( screenPoint0); Vector3 worldPoint1 = ConvertToWorldPosition( screenPoint1); Line line = new Line( screenPoint0, screenPoint1, worldPoint0, worldPoint1); lines.Add(line); } }
void Run() { inputMat = new Mat(inputTexture.height, inputTexture.width, CvType.CV_8UC4); outputMat = new Mat(inputTexture.height, inputTexture.width, CvType.CV_8UC4); Utils.texture2DToMat(inputTexture, inputMat); Imgproc.cvtColor(inputMat, outputMat, Imgproc.COLOR_RGB2GRAY); Imgproc.Canny(outputMat, outputMat, 100, 150); Mat lines = new Mat(); //第五个参数是阈值,通过调整阈值大小可以过滤掉一些干扰线 Imgproc.HoughLinesP(outputMat, lines, 1, Mathf.PI / 180, 60, 50, 10); //计算霍夫曼线外围的交叉点 int[] linesArray = new int[lines.cols() * lines.rows() * lines.channels()]; lines.get(0, 0, linesArray); Debug.Log("length of lineArray " + linesArray.Length); List <int> a = new List <int>(); List <int> b = new List <int>(); for (int i = 0; i < linesArray.Length - 4; i = i + 4) { Imgproc.line(inputMat, new Point(linesArray[i + 0], linesArray[i + 1]), new Point(linesArray[i + 2], linesArray[i + 3]), new Scalar(255, 0, 0), 2); } for (int i = 0; i < linesArray.Length; i = i + 4) { a.Add(linesArray[i + 0]); a.Add(linesArray[i + 1]); a.Add(linesArray[i + 2]); a.Add(linesArray[i + 3]); for (int j = i + 4; j < linesArray.Length; j = j + 4) { b.Add(linesArray[j + 0]); b.Add(linesArray[j + 1]); b.Add(linesArray[j + 2]); b.Add(linesArray[j + 3]); Vector2 temp = ComputeIntersect(a, b); b.Clear(); if (temp.x > 0 && temp.y > 0 && temp.x < 1000 && temp.y < 1000) { corners.Add(temp); } } a.Clear(); } //剔除重合的点和不合理的点 CullIllegalPoint(ref corners, 20); if (corners.Count != 4) { Debug.Log("The object is not quadrilateral " + corners.Count); } Vector2 center = Vector2.zero; for (int i = 0; i < corners.Count; i++) { center += corners[i]; } center *= 0.25f; SortCorners(ref corners, center); //计算转换矩阵 Vector2 tl = corners[0]; Vector2 tr = corners[1]; Vector2 br = corners[2]; Vector2 bl = corners[3]; Mat srcRectMat = new Mat(4, 1, CvType.CV_32FC2); Mat dstRectMat = new Mat(4, 1, CvType.CV_32FC2); srcRectMat.put(0, 0, tl.x, tl.y, tr.x, tr.y, bl.x, bl.y, br.x, br.y); dstRectMat.put(0, 0, 0.0, inputMat.rows(), inputMat.cols(), inputMat.rows(), 0.0, 0.0, inputMat.rows(), 0); Mat perspectiveTransform = Imgproc.getPerspectiveTransform(srcRectMat, dstRectMat); Mat outputMat0 = inputMat.clone(); //圈出四个顶点 //Point t = new Point(tl.x,tl.y); //Imgproc.circle(outputMat0, t, 6, new Scalar(0, 0, 255, 255), 2); //t = new Point(tr.x, tr.y); //Imgproc.circle(outputMat0, t, 6, new Scalar(0, 0, 255, 255), 2); //t = new Point(bl.x, bl.y); //Imgproc.circle(outputMat0, t, 6, new Scalar(0, 0, 255, 255), 2); //t = new Point(br.x, br.y); //Imgproc.circle(outputMat0, t, 6, new Scalar(0, 0, 255, 255), 2); //进行透视转换 Imgproc.warpPerspective(inputMat, outputMat0, perspectiveTransform, new Size(inputMat.rows(), inputMat.cols())); Texture2D outputTexture = new Texture2D(outputMat0.cols(), outputMat0.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(outputMat0, outputTexture); output.texture = outputTexture; }
//사진에서 값 처리 void Update() { tex2 = MakeTexture2D(carm.targetTexture); Mat imgMat = new Mat(tex2.height, tex2.width, CvType.CV_8UC3); Utils.texture2DToMat(tex2, imgMat); //GrayScale 생성 Mat grayMat = new Mat(); Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGB2GRAY); //Canny적용 Mat cannyMat = new Mat(); Imgproc.Canny(grayMat, cannyMat, 60, 150); //ROI 설정 cannyMat.locateROI(new Size(200, 100), new Point(60, 60)); //HoughLine생성 Mat lines = new Mat(); Imgproc.HoughLinesP(cannyMat, lines, 1, Mathf.PI / 180, 30, 40, 40); int[] linesArray = new int[lines.cols() * lines.rows() * lines.channels()]; lines.get(0, 0, linesArray); line_count = 0; is_Left_Line = false; is_Right_Line = false; right_point.Initialize(); left_point.Initialize(); for (int i = 0; i < linesArray.Length; i = i + 4) { // Debug.Log(Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg); if (line_count >= 2) { break; } if (!is_Left_Line) { //각제한 if (5 < Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { if (85 > Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { Imgproc.line(imgMat, new Point(linesArray[i + 0], linesArray[i + 1]), new Point(linesArray[i + 2], linesArray[i + 3]), new Scalar(255, 0, 0), 4); line_count++; is_Left_Line = true; for (int j = 0; j < left_point.Length; j++) { left_point[j] = linesArray[i + j]; } } } } if (!is_Right_Line) { //각제한 if (125 < Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { if (175 > Mathf.Atan2((linesArray[i + 2] - linesArray[i + 0]), (linesArray[i + 3] - linesArray[i + 1])) * Mathf.Rad2Deg) { Imgproc.line(imgMat, new Point(linesArray[i + 0], linesArray[i + 1]), new Point(linesArray[i + 2], linesArray[i + 3]), new Scalar(255, 0, 0), 4); line_count++; is_Right_Line = true; for (int j = 0; j < right_point.Length; j++) { right_point[j] = linesArray[i + j]; } } } } } int[] van = Find_Vanishing_Point(left_point, right_point); Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false); Texture2D texture2 = new Texture2D(grayMat.cols(), grayMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(imgMat, texture); Utils.matToTexture2D(cannyMat, texture2); GameObject.FindGameObjectWithTag("test").GetComponent <Renderer>().material.mainTexture = texture; //GameObject.Find("test2").GetComponent<Renderer>().material.mainTexture = texture2; //자동차에 값 전달 Vector2 diretion = Get_Vector(van, imgMat); car_AI.Change_Value_Steering(diretion); //Debug.Log("van point : "+diretion.x +" : " + diretion.y); }