11 // 'range': a minimum and maximum radius.
12 // 'scale': <= 1.0, to speed up the process.
13 let findRadius (img
: Image<Gray, byte
>) (range
: int * int) (scale
: float) : int =
14 use scaledImg = if scale
= 1.0 then img
else img.Resize(scale
, CvEnum.Inter.Area)
17 let r1', r2' = roundInt
(float r1 * scale
), roundInt
(float r2 * scale
)
19 let patternSpectrum = Array.zeroCreate
(r2' - r1')
20 let intensityImg = scaledImg.GetSum().Intensity
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))
27 let n = 1.0 - closed.GetSum().Intensity / intensityImg
29 if not
(Double.IsNaN previous_n)
31 patternSpectrum.[r
- r1' - 1] <- abs (n - previous_n)
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)