Advertisement
Guest User

Transformations to extract an image of an arbitrary line

a guest
Jan 31st, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. private Bitmap ExtractLaneImage(Image source, DetectorLane lane) {
  2.     // Find the bounding recatangle and offset to the start
  3.     Point topLeft = new Point(Math.Min(lane.start.X, lane.end.X), Math.Min(lane.start.Y, lane.end.Y));
  4.     Point bottomRight = new Point(Math.Max(lane.start.X, lane.end.X), Math.Max(lane.start.Y, lane.end.Y));
  5.     Size size = new Size(bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y);
  6.     Rectangle area = new Rectangle(topLeft, size);
  7.     Point offset = new Point(topLeft.X - lane.start.X, topLeft.Y - lane.start.Y);
  8.  
  9.     // Find the lane angle (used for the rotation)
  10.     Point delta = new Point(lane.end.X - lane.start.X, lane.end.Y - lane.start.Y);
  11.     double angle = Math.Atan2(delta.Y, delta.X) * 180 / Math.PI;
  12.  
  13.     // Find the length of the line
  14.     double length = Math.Sqrt(Math.Pow(size.Width, 2) + Math.Pow(size.Height, 2));
  15.  
  16.     // Create the image for this lane
  17.     Bitmap image = new Bitmap((int)length, 20);
  18.     Graphics g = Graphics.FromImage(image);
  19.  
  20.     // Centre point/drawing position
  21.     Point position = new Point(0, image.Height / 2);
  22.  
  23.     // Default white fill
  24.     g.FillRectangle(Brushes.White, new Rectangle(new Point(0, 0), image.Size));
  25.  
  26.     // Offset to get start point at the desired position
  27.     g.TranslateTransform(offset.X, offset.Y, MatrixOrder.Append);
  28.  
  29.     // Rotate (adjusting to to origin and back)
  30.     g.TranslateTransform(-position.X, -position.Y, MatrixOrder.Append);
  31.     g.RotateTransform(-(float)angle, MatrixOrder.Append);
  32.     g.TranslateTransform(position.X, position.Y, MatrixOrder.Append);
  33.  
  34.     // Draw the final image
  35.     g.DrawImage(source, position.X, position.Y, area, GraphicsUnit.Pixel);
  36.  
  37.     g.Dispose();
  38.     return image;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement