Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Bitmap ExtractLaneImage(Image source, DetectorLane lane) {
- // Find the bounding recatangle and offset to the start
- Point topLeft = new Point(Math.Min(lane.start.X, lane.end.X), Math.Min(lane.start.Y, lane.end.Y));
- Point bottomRight = new Point(Math.Max(lane.start.X, lane.end.X), Math.Max(lane.start.Y, lane.end.Y));
- Size size = new Size(bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y);
- Rectangle area = new Rectangle(topLeft, size);
- Point offset = new Point(topLeft.X - lane.start.X, topLeft.Y - lane.start.Y);
- // Find the lane angle (used for the rotation)
- Point delta = new Point(lane.end.X - lane.start.X, lane.end.Y - lane.start.Y);
- double angle = Math.Atan2(delta.Y, delta.X) * 180 / Math.PI;
- // Find the length of the line
- double length = Math.Sqrt(Math.Pow(size.Width, 2) + Math.Pow(size.Height, 2));
- // Create the image for this lane
- Bitmap image = new Bitmap((int)length, 20);
- Graphics g = Graphics.FromImage(image);
- // Centre point/drawing position
- Point position = new Point(0, image.Height / 2);
- // Default white fill
- g.FillRectangle(Brushes.White, new Rectangle(new Point(0, 0), image.Size));
- // Offset to get start point at the desired position
- g.TranslateTransform(offset.X, offset.Y, MatrixOrder.Append);
- // Rotate (adjusting to to origin and back)
- g.TranslateTransform(-position.X, -position.Y, MatrixOrder.Append);
- g.RotateTransform(-(float)angle, MatrixOrder.Append);
- g.TranslateTransform(position.X, position.Y, MatrixOrder.Append);
- // Draw the final image
- g.DrawImage(source, position.X, position.Y, area, GraphicsUnit.Pixel);
- g.Dispose();
- return image;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement