bbb6d66fd6e88346060bf54638f7b9d458e50c00
1
module ParasitemiaCore.Granulometry
13 /// Granulometry by closing on the image 'img' by testing circle structrual elements with radius range in 'range'.
15 /// <param name="img"></param>
16 /// <param name="range">Minimum radius * maximum radius</param>
17 /// <param name="scale">le 1.0, to speed up the process.</param>
18 let findRadiusByClosing (img
: Image<Gray, 'TDepth>) (range : int * int) (scale : float) (useOctagon : bool) : int =
19 use scaledImg = if scale = 1. then img else img.Resize(scale, CvEnum.Inter.Area)
22 let r1', r2
' = roundInt (float r1 * scale), roundInt (float r2 * scale)
24 let patternSpectrum = Array.zeroCreate (r2' - r1')
25 let intensityImg = scaledImg.GetSum().Intensity
28 let octagon (s : int) : Matrix<byte> =
29 if s % 2 = 0 then failwith "s must be odd"
30 let m = new Matrix<byte>(Array2D.create s s 1uy)
31 let r = (float s) / (Math.Sqrt 2. + 2.) |> roundInt
36 m.[s - i - 1, j] <- 0uy
37 m.[i, s - j - 1] <- 0uy
38 m.[s - i - 1, s - j - 1] <- 0uy
41 let mutable previous_n = Double.NaN
45 (octagon (2 * r - 1)).Mat // It doesn't
speed up the
process.
47 CvInvoke.GetStructuringElement(CvEnum.ElementShape.Ellipse, Size(2 * r, 2 * r), Point(-1, -1))
49 use closed = scaledImg.MorphologyEx(CvEnum.MorphOp.Close, se, Point(-1, -1), 1, CvEnum.BorderType.Replicate, MCvScalar(0.0))
51 let n = closed.GetSum().Intensity
53 if not
(Double.IsNaN previous_n) then
54 patternSpectrum.[r - r1' - 1] <- abs (n - previous_n)
57 let max, _ = patternSpectrum |> Array.indexed |> Array.fold (fun (iMax, sMax) (i, s) -> if s > sMax then (i, s) else (iMax, sMax)) (0, Double.MinValue)
59 float (max + r1') / scale
|> roundInt
62 /// Granulometry by area closing on the image 'img' by testing the circle area into the given radius range.
64 let findRadiusByAreaClosing (img : Image<Gray, float32
>) (radiusRange
: int * int) : int =
65 let r1, r2 = radiusRange
68 failwithf
"'radiusRange' invalid : %O" radiusRange
70 use imgCopy = img.Copy()
72 let mutable maxDiff = 0.f
73 let mutable max_r = r1
75 Morpho.areaCloseFWithFun
imgCopy [ for r in r1 .. r2 -> Math.PI * float r ** 2. |> roundInt
, r ] (fun r diff
->
76 if r <> r1 && diff
> maxDiff then