b18f53c2d59050d55bd9a59398ea286cb1dc9e69
[master-thesis.git] / Parasitemia / Parasitemia / Granulometry.fs
1 module Granulometry
2
3 open System
4 open System.Drawing
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 open Utils
10
11 // 'range': a minimum and maximum radius.
12 // 'scale': <= 1.0, to speed up the process.
13 let findRadius (img: Image<Gray, 'TDepth>) (range: int * int) (scale: float) : int =
14 use scaledImg = if scale = 1.0 then img else img.Resize(scale, CvEnum.Inter.Area)
15
16 let r1, r2 = range
17 let r1', r2' = roundInt (float r1 * scale), roundInt (float r2 * scale)
18
19 let patternSpectrum = Array.zeroCreate (r2' - r1')
20 let intensityImg = scaledImg.GetSum().Intensity
21
22 let mutable previous_n = Double.NaN
23 for r in r1' .. r2' do
24 let se = CvInvoke.GetStructuringElement(CvEnum.ElementShape.Ellipse, Size(2 * r, 2 * r), Point(-1, -1))
25 use closed = scaledImg.MorphologyEx(CvEnum.MorphOp.Close, se, Point(-1, -1), 1, CvEnum.BorderType.Replicate, MCvScalar(0.0))
26
27 let n = closed.GetSum().Intensity
28
29 if not (Double.IsNaN previous_n)
30 then
31 patternSpectrum.[r - r1' - 1] <- abs (n - previous_n)
32 previous_n <- n
33
34 let max, _ = patternSpectrum |> Array.indexed |> Array.fold (fun (iMax, sMax) (i, s) -> if s > sMax then (i, s) else (iMax, sMax)) (0, Double.MinValue)
35
36 float (max + r1') / scale |> roundInt
37
38