1
module Parasitemia.Main
6 open System.Windows.Media
7 open System.Windows.Markup
8 open System.Windows.Shapes
9 open System.Windows.Controls
11 open System.Diagnostics
14 open FSharp.Collections.ParallelSeq
17 open Emgu.CV.Structure
22 let display (window
: Views.MainWindow) (img
: IImage) =
23 let imgControl = window
.Root.FindName("img") :?> Controls.Image
24 imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img
)
26 let log (window
: Views.MainWindow) (mess
: string) =
27 let txtLog = window
.Root.FindName("txtLog") :?> Controls.TextBlock
28 txtLog.Text <- txtLog.Text + mess
+ "\n"
36 | CmdLine of Input * string
39 type Arguments = RunningMode * bool
41 let parseArgs (args
: string[]) : Arguments =
43 let output = Array.tryFindIndex
((=) "--output") args
46 match Array.tryFindIndex
((=) "--folder") args
, output with
47 | Some i
, Some i_output when i
< args
.Length - 2 && i_output < args
.Length - 2 ->
48 CmdLine ((Dir args
.[i
+1]), args
.[i_output + 1])
50 match Array.tryFindIndex
((=) "--file") args
, output with
51 | Some i
, Some i_output when i
< args
.Length - 2 && i_output < args
.Length - 2 ->
52 CmdLine ((File args.[i
+1]), args.[i_output + 1])
56 runningMode, Array.exists
((=) "--debug") args
61 match parseArgs args with
66 initialAreaOpen
= 2000
71 preFilterSigma
= 1.7 // 1.5
75 darkStainLevel
= 0.25 // Lower -> more sensitive. 0.3. Careful about illumination on the borders.
76 maxDarkStainRatio
= 0.1 // 10 %
78 infectionArea
= 0.012f // 1.2 %
79 infectionLevel
= 1.12 // Lower -> more sensitive.
81 stainArea
= 0.08f // 8 %
82 stainLevel
= 1.1 // Lower -> more sensitive.
83 maxStainRatio
= 0.12 // 12 %
85 standardDeviationMaxRatio
= 0.5 // 0.55
86 minimumCellArea
= 0.5f })
89 | CmdLine (input
, output) ->
92 config.Debug <- DebugOn output
94 Directory.CreateDirectory output |> ignore
96 use logFile = new StreamWriter(new FileStream(Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
97 Utils.log <- (fun m
-> logFile.WriteLine(m
))
98 Utils.log (sprintf
"=== New run : %A %A ===" DateTime.Now (if debug
then "[DEBUG]" else "[RELEASE]"))
100 let files = match input
with
101 | File file
-> [ file
]
102 | Dir dir
-> Directory.EnumerateFiles dir |> List.ofSeq
104 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
107 let images = seq
{ for file
in files -> Path.GetFileNameWithoutExtension(FileInfo(file
).Name), new Image<Bgr, byte
>(file
) }
109 let nbConcurrentTaskLimit = 4
110 let n = Environment.ProcessorCount
112 Utils.logTime
"Whole analyze" (fun () ->
115 |> PSeq.map
(fun (id
, img
) -> id
, ImageAnalysis.doAnalysis
img id
(config.Copy()))
116 |> PSeq.withDegreeOfParallelism
(if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n)
118 for id
, cells
in results do
119 let total, infected
= Utils.countCells
cells
120 fprintf
resultFile "File: %s %d %d %.2f\n" id
total infected (100. * (float infected) / (float total)))
122 //Utils.log (sprintf "== File: %A" file)
124 //| :? IOException as ex -> Utils.log (sprintf "Unable to open the image '%A': %A" file ex)
128 let app = new Application()
129 let mainWindow = Views.MainWindow()
133 config.Debug <- DebugOn "."
135 Utils.log <- (fun m
-> log mainWindow m
)
137 //display mainWindow img
138 mainWindow.Root.Show()