Skip to content

Commit 229cc69

Browse files
committed
ready to merge into firmware
1 parent 032815e commit 229cc69

File tree

6 files changed

+64
-87
lines changed

6 files changed

+64
-87
lines changed

Diff for: RoadTracer/LineHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public List<LineSegment2D> ExtractContour(Image<Bgr, byte> img,out Image<Bgr, b
6565
img.ROI = new Rectangle(new Point(0, Img_Height - Roi_Height), new Size(Img_Width, Roi_Height));
6666
gray.ROI = new Rectangle(new Point(0, Img_Height - Roi_Height), new Size(Img_Width, Roi_Height));
6767
Image<Gray, byte> canny = gray.Canny(150, 50);
68-
canny.Save("D:\\temp\\canny.jpg");
68+
//canny.Save("D:\\temp\\canny.jpg");
6969
Image<Bgr, byte> lineimage = img;
7070
lines = canny.HoughLines(1, 2, 3, Math.PI / 180, 150, 80, 400);
7171
foreach (LineSegment2D line in lines[0])
@@ -79,7 +79,7 @@ public List<LineSegment2D> ExtractContour(Image<Bgr, byte> img,out Image<Bgr, b
7979
}
8080
}
8181
temp = lineimage;
82-
lineimage.Save("D:\\temp\\HoughLines.jpg");
82+
//lineimage.Save("D:\\temp\\HoughLines.jpg");
8383
return ListOfLines;
8484

8585
}

Diff for: RoadTracer/Operator.cs

+57-53
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,59 @@ public Operator(int i_w, int i_h, int r_h)
1818
Img_Height = i_h;
1919
Roi_Height = r_h;
2020
}
21-
public void FindMiddleByPath(string url)
22-
{
23-
Image<Bgr, byte> img = new Image<Bgr, byte>(url);
24-
List<LineSegment2D> positiveLines = new List<LineSegment2D>();
25-
List<LineSegment2D> negativeLines = new List<LineSegment2D>();
26-
LineHelper helper = new LineHelper(Img_Height,Img_Width,Roi_Height);
21+
//public void FindMiddleByPath(string url)
22+
//{
23+
// Image<Bgr, byte> img = new Image<Bgr, byte>(url);
24+
// List<LineSegment2D> positiveLines = new List<LineSegment2D>();
25+
// List<LineSegment2D> negativeLines = new List<LineSegment2D>();
26+
// LineHelper helper = new LineHelper(Img_Height,Img_Width,Roi_Height);
2727

28-
//store all the possible lines
29-
List<LineSegment2D> lines = helper.ExtractContour(img,out temp);
28+
// //store all the possible lines
29+
// List<LineSegment2D> lines = helper.ExtractContour(img,out temp);
3030

31-
//divide the lines into two parts.
32-
foreach (LineSegment2D line in lines)
33-
{
34-
float theta = line.Direction.Y / line.Direction.X;
35-
if (theta > 0) { positiveLines.Add(line); }
36-
else if (theta < 0) { negativeLines.Add(line); }
37-
}
31+
// //divide the lines into two parts.
32+
// foreach (LineSegment2D line in lines)
33+
// {
34+
// float theta = line.Direction.Y / line.Direction.X;
35+
// if (theta > 0) { positiveLines.Add(line); }
36+
// else if (theta < 0) { negativeLines.Add(line); }
37+
// }
3838

39-
//find the average line of two groups above
40-
if (positiveLines.Count == 0) throw new TextException("No negative Line, Try look right!");
41-
if (negativeLines.Count==0) throw new TextException("No positive Line,Try look left!");
42-
LineSegment2DF posline = helper.AvrageLine(positiveLines);
43-
LineSegment2DF negline = helper.AvrageLine(negativeLines);
39+
// //find the average line of two groups above
40+
// if (positiveLines.Count == 0) throw new TextException("No negative Line, Try look right!");
41+
// if (negativeLines.Count==0) throw new TextException("No positive Line,Try look left!");
42+
// LineSegment2DF posline = helper.AvrageLine(positiveLines);
43+
// LineSegment2DF negline = helper.AvrageLine(negativeLines);
4444

45-
//draw two average lines.
46-
temp.Draw(posline, new Bgr(0, 255, 0), 5);
47-
temp.Draw(negline, new Bgr(0, 255, 0), 5);
45+
// //draw two average lines.
46+
// temp.Draw(posline, new Bgr(0, 255, 0), 5);
47+
// temp.Draw(negline, new Bgr(0, 255, 0), 5);
4848

49-
//Find and draw the vanishing point
50-
PointF crosspt = helper.GetIntersection(ref posline, ref negline);
51-
if (crosspt.Y > 140) throw new TextException("Near Corner");
52-
temp.Draw(new CircleF(crosspt, 3), new Bgr(0, 0, 255), 5);
49+
// //Find and draw the vanishing point
50+
// PointF crosspt = helper.GetIntersection(ref posline, ref negline);
51+
// if (crosspt.Y > 140) throw new TextException("Near Corner");
52+
// temp.Draw(new CircleF(crosspt, 3), new Bgr(0, 0, 255), 5);
5353

54-
//Find the bisector
55-
float i = 0;
56-
for (i = 0; i < 640; i++)
57-
{
58-
PointF pt = new PointF(i, Roi_Height);
59-
if (Math.Abs(helper.GetDist(pt, posline) - helper.GetDist(pt, negline)) < 1)
60-
break;
61-
}
62-
LineSegment2DF middleline = new LineSegment2DF(crosspt, new PointF(i, 280));
54+
// //Find the bisector
55+
// float i = 0;
56+
// for (i = 0; i < 640; i++)
57+
// {
58+
// PointF pt = new PointF(i, Roi_Height);
59+
// if (Math.Abs(helper.GetDist(pt, posline) - helper.GetDist(pt, negline)) < 1)
60+
// break;
61+
// }
62+
// LineSegment2DF middleline = new LineSegment2DF(crosspt, new PointF(i, 280));
6363

64-
#region test bisector
65-
//double angle1, angle2;
66-
//angle1 = middleline.GetExteriorAngleDegree(posline);
67-
//angle2 = middleline.GetExteriorAngleDegree(negline);
68-
//double angle3 = posline.GetExteriorAngleDegree(negline);
69-
#endregion
70-
temp.Draw(middleline, new Bgr(0, 0, 255), 5);
71-
temp.Save("D:\\temp\\lines.jpg");
72-
}
73-
public void FindMiddleByImg(Image<Bgr, byte> img)
64+
// #region test bisector
65+
// //double angle1, angle2;
66+
// //angle1 = middleline.GetExteriorAngleDegree(posline);
67+
// //angle2 = middleline.GetExteriorAngleDegree(negline);
68+
// //double angle3 = posline.GetExteriorAngleDegree(negline);
69+
// #endregion
70+
// temp.Draw(middleline, new Bgr(0, 0, 255), 5);
71+
// temp.Save("D:\\temp\\lines.jpg");
72+
//}
73+
public string FindMiddleByImg(Image<Bgr, byte> img)
7474
{
7575
List<LineSegment2D> positiveLines = new List<LineSegment2D>();
7676
List<LineSegment2D> negativeLines = new List<LineSegment2D>();
@@ -88,25 +88,25 @@ public void FindMiddleByImg(Image<Bgr, byte> img)
8888
}
8989

9090
//find the average line of two groups above
91-
if (positiveLines.Count == 0) throw new TextException("No negative Line, Try look right!");
92-
if (negativeLines.Count == 0) throw new TextException("No positive Line,Try look left!");
91+
if (positiveLines.Count == 0) return "No negative Line, Try look right!";
92+
if (negativeLines.Count == 0) return "No positive Line,Try look left!";
9393
LineSegment2DF posline = helper.AvrageLine(positiveLines);
9494
LineSegment2DF negline = helper.AvrageLine(negativeLines);
9595

9696
//draw two average lines.
97-
//temp.Draw(posline, new Bgr(0, 255, 0), 5);
98-
//temp.Draw(negline, new Bgr(0, 255, 0), 5);
97+
temp.Draw(posline, new Bgr(0, 255, 0), 5);
98+
temp.Draw(negline, new Bgr(0, 255, 0), 5);
9999

100100
//Find and draw the vanishing point
101101
PointF crosspt = helper.GetIntersection(ref posline, ref negline);
102-
if (crosspt.Y > 140) throw new TextException("Near Corner");
102+
if (crosspt.Y > 140) return "Near Corner";
103103
//temp.Draw(new CircleF(crosspt, 3), new Bgr(0, 0, 255), 5);
104-
104+
PointF pt=new PointF(0,Roi_Height);
105105
//Find the bisector
106106
float i = 0;
107107
for (i = 0; i < 640; i++)
108108
{
109-
PointF pt = new PointF(i, Roi_Height);
109+
pt = new PointF(i, Roi_Height);
110110
if (Math.Abs(helper.GetDist(pt, posline) - helper.GetDist(pt, negline)) < 1)
111111
break;
112112
}
@@ -119,6 +119,10 @@ public void FindMiddleByImg(Image<Bgr, byte> img)
119119
//double angle3 = posline.GetExteriorAngleDegree(negline);
120120
#endregion
121121
temp.Draw(middleline, new Bgr(0, 0, 255), 5);
122+
if (pt.X < 220 && pt.X > 0) return "Turn Left";
123+
else if (pt.X >= 220 && pt.X < 420) return "Go Straight";
124+
else if (pt.X >= 420 && pt.X <= 640) return "Turn Right";
125+
else return "No Sense";
122126
//temp.Save("D:\\temp\\lines.jpg");
123127
}
124128
public Image<Bgr, byte> GetRoadImg()

Diff for: RoadTracer/RoadTracer.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
<Compile Include="LineHelper.cs" />
7474
<Compile Include="Operator.cs" />
7575
<Compile Include="Properties\AssemblyInfo.cs" />
76-
<Compile Include="TextException.cs" />
7776
</ItemGroup>
7877
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7978
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Diff for: RoadTracer/TextException.cs

-10
This file was deleted.

Diff for: TestWpf/MainWindow.xaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<Image x:Name="cam" HorizontalAlignment="Left" Height="300" Margin="10,30,0,0" VerticalAlignment="Top" Width="400" />
1313
<Image x:Name="cam_right" HorizontalAlignment="Left" Height="300" Margin="415,30,-55,0" VerticalAlignment="Top"
1414
Width="400" />
15-
</Grid>
15+
<Label x:Name="status" HorizontalAlignment="Left" VerticalAlignment="Bottom"></Label>
16+
</Grid>
1617
</Window>

Diff for: TestWpf/MainWindow.xaml.cs

+3-20
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ await Application.Current.Dispatcher.BeginInvoke(
112112
{
113113
try
114114
{
115-
if (!interrupt)
116-
{
115+
117116
_bitmapImage = new BitmapImage();
118117
_bitmapImage.BeginInit();
119118
_bitmapImage.StreamSource = _stream; // Copy stream to local
@@ -122,25 +121,9 @@ await Application.Current.Dispatcher.BeginInvoke(
122121

123122
observed = new Image<Bgr, byte>(UiHandler.Bmimg2Bitmap(_bitmapImage));
124123
Operator op = new Operator(640, 480, 280);
125-
try
126-
{
127-
op.FindMiddleByImg(observed);
124+
125+
status.Content= op.FindMiddleByImg(observed);
128126
UiHandler.show_Image(cam_right, op.GetRoadImg());
129-
}
130-
catch (TextException ex)
131-
{
132-
interrupt = true;
133-
MessageBox.Show(ex.Message);
134-
interrupt = false;
135-
}
136-
}
137-
else {
138-
_bitmapImage = new BitmapImage();
139-
_bitmapImage.BeginInit();
140-
_bitmapImage.StreamSource = _stream; // Copy stream to local
141-
_bitmapImage.EndInit();
142-
cam.Source = _bitmapImage;
143-
}
144127
}
145128

146129
catch (Exception)

0 commit comments

Comments
 (0)