9662049ebc6c793f2c9f39b4a5173a1416fc6fc4
[master-thesis.git] / Parasitemia / ParasitemiaCore / ImgTools / Drawing.fs
1 module ParasitemiaCore.Drawing
2
3 open System
4 open System.Drawing
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 open Const
10 open Types
11
12 let drawPoints (img : Image<Gray, 'TDepth>) (points : Points) (intensity : 'TDepth) =
13 for p in points do
14 img.Data.[p.Y, p.X, 0] <- intensity
15
16 let drawLine (img : Image<'TColor, 'TDepth>) (color : 'TColor) (x0 : int) (y0 : int) (x1 : int) (y1 : int) (thickness : int) =
17 img.Draw(LineSegment2D(Point(x0, y0), Point(x1, y1)), color, thickness);
18
19 let drawLineF (img : Image<'TColor, 'TDepth>) (color : 'TColor) (x0 : float) (y0 : float) (x1 : float) (y1 : float) (thickness : int) =
20 img.Draw(LineSegment2DF(PointF(float32 x0, float32 y0), PointF(float32 x1, float32 y1)), color, thickness, CvEnum.LineType.AntiAlias);
21
22 let drawEllipse (img : Image<'TColor, 'TDepth>) (e : Ellipse) (color : 'TColor) (alpha : float) =
23 if alpha >= 1.0 then
24 img.Draw(Emgu.CV.Structure.Ellipse(PointF(e.Cx, e.Cy), SizeF(2.f * e.B, 2.f * e.A), e.Alpha / PI * 180.f), color, 1, CvEnum.LineType.AntiAlias)
25 else
26 let windowPosX = e.Cx - e.A - 5.f
27 let gapX = windowPosX - (float32 (int windowPosX))
28
29 let windowPosY = e.Cy - e.A - 5.f
30 let gapY = windowPosY - (float32 (int windowPosY))
31
32 let roi = Rectangle(int windowPosX, int windowPosY, 2.f * (e.A + 5.f) |> int, 2.f * (e.A + 5.f) |> int)
33
34 img.ROI <- roi
35 if roi = img.ROI then // We do not display ellipses touching the edges (FIXME)
36 use i = new Image<'TColor, 'TDepth>(img.ROI.Size)
37 i.Draw(Emgu.CV.Structure.Ellipse(PointF(e.A + 5.f + gapX, e.A + 5.f + gapY), SizeF(2.f * e.B, 2.f * e.A), e.Alpha / PI * 180.f), color, 1, CvEnum.LineType.AntiAlias)
38 CvInvoke.AddWeighted(img, 1.0, i, alpha, 0.0, img)
39 img.ROI <- Rectangle.Empty
40
41 let drawEllipses (img : Image<'TColor, 'TDepth>) (ellipses : Ellipse list) (color : 'TColor) (alpha : float) =
42 List.iter (fun e -> drawEllipse img e color alpha) ellipses
43
44 let rngCell = System.Random()
45 let drawCell (img : Image<Bgr, byte>) (drawCellContent : bool) (c : Cell) =
46 if drawCellContent then
47 let colorB = rngCell.Next(20, 70)
48 let colorG = rngCell.Next(20, 70)
49 let colorR = rngCell.Next(20, 70)
50
51 for y = 0 to c.elements.Height - 1 do
52 for x = 0 to c.elements.Width - 1 do
53 if c.elements.[y, x] > 0uy then
54 let dx, dy = c.center.X - c.elements.Width / 2, c.center.Y - c.elements.Height / 2
55 let b = img.Data.[y + dy, x + dx, 0] |> int
56 let g = img.Data.[y + dy, x + dx, 1] |> int
57 let r = img.Data.[y + dy, x + dx, 2] |> int
58 img.Data.[y + dy, x + dx, 0] <- if b + colorB > 255 then 255uy else byte (b + colorB)
59 img.Data.[y + dy, x + dx, 1] <- if g + colorG > 255 then 255uy else byte (g + colorG)
60 img.Data.[y + dy, x + dx, 2] <- if r + colorR > 255 then 255uy else byte (r + colorR)
61
62 let crossColor, crossColor2 =
63 match c.cellClass with
64 | HealthyRBC -> Bgr(255., 0., 0.), Bgr(255., 255., 255.)
65 | InfectedRBC -> Bgr(0., 0., 255.), Bgr(120., 120., 255.)
66 | Peculiar -> Bgr(0., 0., 0.), Bgr(80., 80., 80.)
67
68 drawLine img crossColor2 (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 2
69 drawLine img crossColor2 c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 2
70
71 drawLine img crossColor (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 1
72 drawLine img crossColor c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 1
73
74 let drawCells (img : Image<Bgr, byte>) (drawCellContent : bool) (cells : Cell list) =
75 List.iter (fun c -> drawCell img drawCellContent c) cells