/// <summary> /// 繪製黑白交錯的曲線(縱軸是Intensity,橫軸是排序的線段從最前面的線段到最下面的線段的座標) /// </summary> /// <param name="drawImg"></param> /// <param name="current">現在的座標像素</param> /// <param name="previous">前一個點的座標像素</param> /// <param name="x">x軸步近的值</param> private static void DrawBlackWhiteCurve(Image <Bgr, byte> drawImg, IntensityPoint current, IntensityPoint previous, int x) { //繪製呈現用,斑馬線黑白像素經過的圖形 int projectY = Math.Abs((int)current.GetIntensity() - 300); if (!current.IsEmpty() && !previous.IsEmpty()) { float prevPorjectY = Math.Abs((float)previous.GetIntensity() - 300); drawImg.Draw(new LineSegment2DF(new PointF(x - 2, projectY), new PointF(x, prevPorjectY)), new Bgr(Color.Green), 1); } else { drawImg.Draw(new LineSegment2DF(new PointF(0, 300), new PointF(x, projectY)), new Bgr(Color.Red), 1); } drawImg.Draw(new CircleF(new PointF(x, projectY), 1), new Bgr(Color.Blue), 2); }
private static bool DoBlackWhiteStatisticsByScanLine(List <LineSegment2DF> lines, Image <Gray, byte> source, Image <Bgr, byte> stasticDst) { string checkBlackWhiteCrossingPoint = ""; Image <Bgr, byte> blackWhiteCurveImg = new Image <Bgr, byte>(480, 300, new Bgr(Color.White)); int x = 0; // 要尋訪的起點 IntensityPoint current, previous; current = new IntensityPoint(); previous = new IntensityPoint(); //記錄每一條線段的像素統計用的索引 int pixelSum = 0; int previousPixelValue = -1; int previousCheckIntentisty = -1; //計算線段通過pixel foreach (LineSegment2DF line in lines) { float nextX; float nextY = line.P1.Y; //如果尋訪小於線段結束點的y軸,則不斷尋訪 while (nextY < line.P2.Y) { nextX = GetXPositionFromLineEquations(line.P1, line.P2, nextY); //抓灰階 or 二值化做測試 Gray pixel = source[Convert.ToInt32(nextY), Convert.ToInt32(nextX)]; CheckBlackWhiteTexture(checkBlackWhiteCrossingPoint, pixel.Intensity, ref pixelSum, ref previousPixelValue, ref previousCheckIntentisty, ref checkBlackWhiteCrossingPoint); //取得目前掃描線步進的素值 current.SetData(new PointF(nextX, nextY), pixel.Intensity); if (stasticDst != null) { DrawBlackWhiteCurve(stasticDst, current, previous, x); } //設定前一筆 previous.SetData(current.GetLocation(), current.GetIntensity()); //Console.WriteLine("x:" + nextX + ",y:" + nextY + ",Intensity:" + pixel.Intensity); //步進Y nextY++; //繪製用的步進值 x += 5; } } ////顯示所有check的狀況 Console.WriteLine(checkBlackWhiteCrossingPoint); if (checkBlackWhiteCrossingPoint.Contains("010101") || checkBlackWhiteCrossingPoint.Contains("101010")) { Console.WriteLine("有交錯"); return(true); } else { return(false); } }
public static bool AnalyzeZebraCrossingTexture(int mainDirectionLineGroupId, Dictionary <LineQuantification, LinkedList <LineEquation> > linesHistogram, Image <Gray, byte> processingImg, Image <Bgr, byte> oriImg, Image <Bgr, byte> stasticDst, Image <Bgr, byte> drawScanLineImg) { //紀錄斑馬線之間白色連結起來的線段 List <LineSegment2DF> crossingConnectionlines = new List <LineSegment2DF>(); Point prePoint = new Point(); Point currentPoint = new Point(); Image <Bgr, byte> scanLineImg = oriImg.Clone(); IntensityPoint current, previous; current = new IntensityPoint(); previous = new IntensityPoint(); int index = 0; List <LineEquation> orderedLines = new List <LineEquation>(); //角度幾乎呈垂直,所以排序用x軸 if ((17 <= mainDirectionLineGroupId && mainDirectionLineGroupId <= 18) || (7 <= mainDirectionLineGroupId && mainDirectionLineGroupId <= 9)) { var orderedMainLines = from line in linesHistogram[(LineQuantification)mainDirectionLineGroupId] orderby(line.Line.P1.X + line.Line.P2.X) / 2 select line; foreach (LineEquation line in orderedMainLines) { orderedLines.Add(line); } } else { var orderedMainLines = from line in linesHistogram[(LineQuantification)mainDirectionLineGroupId] orderby(line.Line.P1.Y + line.Line.P2.Y) / 2 select line; foreach (LineEquation line in orderedMainLines) { orderedLines.Add(line); } } foreach (LineEquation line in orderedLines) { int lineCenterY = (line.Line.P1.Y + line.Line.P2.Y) / 2; int lineCenterX = (line.Line.P1.X + line.Line.P2.X) / 2; if (!currentPoint.IsEmpty) { prePoint = currentPoint; } currentPoint = new Point(lineCenterX, lineCenterY); //兩點 =>存放線條,並繪製 if (!currentPoint.IsEmpty && !prePoint.IsEmpty) { LineSegment2DF scanline = new LineSegment2DF(prePoint, currentPoint); if (drawScanLineImg != null) { drawScanLineImg.Draw(scanline, Utilities.LineColors[index % Utilities.LineColors.Length], 2); } //記錄每一條線段 crossingConnectionlines.Add(scanline); //Console.WriteLine("draw Line:direction ,x = " + scanline.Direction.X + "y =" + scanline.Direction.Y + ",point p1.x =" + prePoint.X + ",p1.y = " + prePoint.Y + ", p2.x =" + currentPoint.X + ",p2.y = " + currentPoint.Y); } //Console.WriteLine("-------------------------------------"); index++; } //統計黑白像素與判斷是否每條線段為白黑白的特徵 bool isBlackWhiteCrossing = DoBlackWhiteStatisticsByScanLine(crossingConnectionlines, processingImg, stasticDst); if (isBlackWhiteCrossing && linesHistogram[(LineQuantification)mainDirectionLineGroupId].Count > 3) { Console.WriteLine("共有" + linesHistogram[(LineQuantification)mainDirectionLineGroupId].Count + "相似斜率線段"); return(true); } else { return(false); } }