From: Greg Burri Date: Wed, 6 Jan 2016 18:04:25 +0000 (+0100) Subject: * Try another approach to remove false ellipses without success (commented). X-Git-Tag: 1.0.11~63 X-Git-Url: http://git.euphorik.ch/?a=commitdiff_plain;h=6b550c3faf4dea77738fa5c27cd9af277f45549c;p=master-thesis.git * Try another approach to remove false ellipses without success (commented). * Add otsu method in ImgTools. --- diff --git a/Parasitemia/Parasitemia/Classifier.fs b/Parasitemia/Parasitemia/Classifier.fs index f0c9d01..341f7c9 100644 --- a/Parasitemia/Parasitemia/Classifier.fs +++ b/Parasitemia/Parasitemia/Classifier.fs @@ -103,10 +103,8 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: if e.isOutside w_f h_f then e.Removed <- true // 3) Remove ellipses with a high standard deviation (high contrast). - - // CvInvoke.Normalize(img, img, 0.0, 255.0, CvEnum.NormType.MinMax) // Not needed. let imgData = img.Data - let globalStdDeviation = MathNet.Numerics.Statistics.Statistics.StandardDeviation(seq { + let globalStdDeviation = MathNet.Numerics.Statistics.Statistics.PopulationStandardDeviation(seq { for y in 0 .. h - 1 do for x in 0 .. w - 1 do yield float imgData.[y, x, 0] }) @@ -127,6 +125,40 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: if stdDeviation > globalStdDeviation * config.Parameters.standardDeviationMaxRatio then e.Removed <- true +(* + let imgData = img.Data + let stdDeviations = [ + for e in ellipses do + if not e.Removed + then + let shrinkedE = e.Scale 0.9f + let minX, minY, maxX, maxY = ellipseWindow shrinkedE + + let stdDeviation = float32 <| MathNet.Numerics.Statistics.Statistics.StandardDeviation (seq { + for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do + for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do + if shrinkedE.Contains (float32 x) (float32 y) + then + yield float imgData.[y, x, 0] }) + + e.StdDeviation <- stdDeviation + yield stdDeviation ] + + // We use Otsu and eliminate some cells only if the curve may be bimodal. + // See https://en.wikipedia.org/wiki/Multimodal_distribution#Bimodality_coefficient + let skewness, kurtosis = MathNet.Numerics.Statistics.Statistics.PopulationSkewnessKurtosis (stdDeviations |> List.map float) + let n = float stdDeviations.Length + let bimodalityCoefficient = (skewness ** 2. + 1.) / (kurtosis + 3. * (n - 1.) ** 2. / ((n - 2.) * (n - 3.))) + + if bimodalityCoefficient > 5. / 9. + then + let hist = ImgTools.histogram stdDeviations 200 + let thresh, _, _ = ImgTools.otsu hist + for e in ellipses do + if not e.Removed && e.StdDeviation > thresh + then e.Removed <- true +*) + // 4) Remove ellipses with little area. let minArea = config.RBCMinArea for e, neighbors in ellipsesWithNeigbors do diff --git a/Parasitemia/Parasitemia/Config.fs b/Parasitemia/Parasitemia/Config.fs index aa8f0e5..701a3fd 100644 --- a/Parasitemia/Parasitemia/Config.fs +++ b/Parasitemia/Parasitemia/Config.fs @@ -9,8 +9,6 @@ type Debug = | DebugOn of string // Output directory. type Parameters = { - scale: float - initialAreaOpen: int minRbcRadius: float32 diff --git a/Parasitemia/Parasitemia/Ellipse.fs b/Parasitemia/Parasitemia/Ellipse.fs index ef167b2..c8d44b7 100644 --- a/Parasitemia/Parasitemia/Ellipse.fs +++ b/Parasitemia/Parasitemia/Ellipse.fs @@ -14,8 +14,8 @@ open Const type private SearchExtremum = Minimum | Maximum -let private goldenSectionSearch (f: float32 -> float32) (nbIter: int) (xmin: float32) (xmax: float32) (searchExtremum: SearchExtremum) : (float32 * float32) = - let gr = 1.f / 1.6180339887498948482f +let private goldenSectionSearch (f: float -> float) (nbIter: int) (xmin: float) (xmax: float) (searchExtremum: SearchExtremum) : (float * float) = + let gr = 1. / 1.6180339887498948482 let mutable a = xmin let mutable b = xmax let mutable c = b - gr * (b - a) @@ -41,14 +41,14 @@ let private goldenSectionSearch (f: float32 -> float32) (nbIter: int) (xmin: flo c <- d d <- a + gr * (b - a) - let x = (b + a) / 2.f + let x = (b + a) / 2. x, f x // Ellipse.A is always equal or greater than Ellipse.B. // Ellipse.Alpha is between 0 and Pi. -let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: float32) (m2: float32) (p3x: float32) (p3y: float32) : Types.Ellipse option = - let accuracy_extremum_search_1 = 8 // 3 - let accuracy_extremum_search_2 = 8 // 4 +let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2: float) (p3x: float) (p3y: float) : Types.Ellipse option = + let accuracy_extremum_search_1 = 10 // 3 + let accuracy_extremum_search_2 = 10 // 4 // p3 as the referencial. let p1x = p1x - p3x @@ -61,27 +61,27 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let alpha1 = atan m1 let alpha2 = atan m2 - let r1 = sqrt (p1x ** 2.f + p1y ** 2.f) + let r1 = sqrt (p1x ** 2. + p1y ** 2.) let theta1 = atan2 p1y p1x - let r2 = sqrt (p2x ** 2.f + p2y ** 2.f) + let r2 = sqrt (p2x ** 2. + p2y ** 2.) let theta2 = atan2 p2y p2x let valid = - 4.f * sin (alpha1 - theta1) * (-r1 * sin (alpha1 - theta1) + r2 * sin (alpha1 - theta2)) * + 4. * sin (alpha1 - theta1) * (-r1 * sin (alpha1 - theta1) + r2 * sin (alpha1 - theta2)) * sin (alpha2 - theta2) * (-r1 * sin (alpha2 - theta1) + r2 * sin (alpha2 - theta2)) + - r1 * r2 * sin (alpha1 - alpha2) ** 2.f * sin (theta1 - theta2) ** 2.f < 0.f + r1 * r2 * sin (alpha1 - alpha2) ** 2. * sin (theta1 - theta2) ** 2. < 0. if valid then let r theta = (r1 * r2 * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) * sin (theta1 - theta2)) / - (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2.f - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.f) + (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2. - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.) let rabs = r >> abs // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval. - let thetaTan, _ = goldenSectionSearch rabs accuracy_extremum_search_1 0.f PI Maximum + let thetaTan, _ = goldenSectionSearch rabs accuracy_extremum_search_1 0. Math.PI Maximum let rTan = r thetaTan let PTanx = rTan * cos thetaTan @@ -93,7 +93,7 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let d2a = tan alpha2 let d2b = -d2a * p2x + p2y - let d3a = -1.f / tan thetaTan + let d3a = -1. / tan thetaTan let d3b = -d3a * PTanx + PTany let Ux = -(d1b - d2b) / (d1a - d2a) @@ -102,11 +102,11 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let Vx = -(d1b - d3b) / (d1a - d3a) let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a) - let Wx = p1x + (p2x - p1x) / 2.f - let Wy = p1y + (p2y - p1y) / 2.f + let Wx = p1x + (p2x - p1x) / 2. + let Wy = p1y + (p2y - p1y) / 2. - let Zx = p1x + (PTanx - p1x) / 2.f - let Zy = p1y + (PTany - p1y) / 2.f + let Zx = p1x + (PTanx - p1x) / 2. + let Zy = p1y + (PTany - p1y) / 2. let va = -(-Vy + Zy) / (Vx - Zx) let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx) @@ -117,33 +117,33 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let cx = -(vb - ub) / (va - ua) let cy = -(ua * vb - va * ub) / (va - ua) - let rc = sqrt (cx ** 2.f + cy ** 2.f) + let rc = sqrt (cx ** 2. + cy ** 2.) let psi = atan2 cy cx let rellipse theta = sqrt ( - rc ** 2.f + (r1 ** 2.f * r2 ** 2.f * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) ** 2.f * sin (theta1 - theta2) ** 2.f) / - (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2.f - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.f) ** 2.f - - (2.f * r1 * r2 * rc * cos (theta - psi) * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) * sin (theta1 - theta2)) / - (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2.f - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.f)) + rc ** 2. + (r1 ** 2. * r2 ** 2. * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) ** 2. * sin (theta1 - theta2) ** 2.) / + (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2. - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.) ** 2. - + (2. * r1 * r2 * rc * cos (theta - psi) * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) * sin (theta1 - theta2)) / + (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2. - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.)) // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval. - let r1eTheta, r1e = goldenSectionSearch rellipse accuracy_extremum_search_2 0.f (PI / 2.f) Maximum // Pi/2 and not pi because the period is Pi. - let r2eTheta, r2e = goldenSectionSearch rellipse accuracy_extremum_search_2 0.f (PI / 2.f) Minimum + let r1eTheta, r1e = goldenSectionSearch rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Maximum // Pi/2 and not pi because the period is Pi. + let r2eTheta, r2e = goldenSectionSearch rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Minimum let rr1e = r r1eTheta let r1ex = rr1e * cos r1eTheta let r1ey = rr1e * sin r1eTheta let mutable alpha = atan ((r1ey - cy) / (r1ex - cx)) - if alpha < 0.f + if alpha < 0. then - alpha <- alpha + PI + alpha <- alpha + Math.PI // Ride off the p3 referential. let cx = cx + p3x let cy = cy + p3y - Some (Types.Ellipse(cx, cy, r1e, r2e, alpha)) + Some (Types.Ellipse(float32 cx, float32 cy, float32 r1e, float32 r2e, float32 alpha)) else None @@ -220,8 +220,8 @@ let find (edges: Matrix) let radiusTolerance = (r2 - r1) * 0.2f - let squaredMinimumDistance = (r2 / 1.5f) ** 2.f - let squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2.f + (y1 - y2) ** 2.f + let squaredMinimumDistance = (float r2 / 1.5) ** 2. + let inline squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2. + (y1 - y2) ** 2. let h = edges.Height let w = edges.Width @@ -271,16 +271,16 @@ let find (edges: Matrix) if p1 <> p2 && p1 <> p3 && p2 <> p3 then nbOfPicks <- nbOfPicks - 1 - let p1yf, p1xf = float32 p1.Y, float32 p1.X - let p2yf, p2xf = float32 p2.Y, float32 p2.X - let p3yf, p3xf = float32 p3.Y, float32 p3.X + let p1yf, p1xf = float p1.Y, float p1.X + let p2yf, p2xf = float p2.Y, float p2.X + let p3yf, p3xf = float p3.Y, float p3.X if squaredDistance p1xf p1yf p2xf p2yf >= squaredMinimumDistance && squaredDistance p1xf p1yf p3xf p3yf >= squaredMinimumDistance && squaredDistance p2xf p2yf p3xf p3yf >= squaredMinimumDistance then - match areVectorsValid p1xf p1yf p2xf p2yf -xDirData.[p1.Y, p1.X, 0] -yDirData.[p1.Y, p1.X, 0] -xDirData.[p2.Y, p2.X, 0] -yDirData.[p2.Y, p2.X, 0] with + match areVectorsValid (float32 p1xf) (float32 p1yf) (float32 p2xf) (float32 p2yf) -xDirData.[p1.Y, p1.X, 0] -yDirData.[p1.Y, p1.X, 0] -xDirData.[p2.Y, p2.X, 0] -yDirData.[p2.Y, p2.X, 0] with | Some (m1, m2) -> - match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf p3yf with + match ellipse p1xf p1yf (float m1) p2xf p2yf (float m2) p3xf p3yf with | Some e when e.Cx > 0.f && e.Cx < w_f - 1.f && e.Cy > 0.f && e.Cy < h_f - 1.f && e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance -> ellipses.Add e diff --git a/Parasitemia/Parasitemia/ImgTools.fs b/Parasitemia/Parasitemia/ImgTools.fs index f64b1c3..3cfdc89 100644 --- a/Parasitemia/Parasitemia/ImgTools.fs +++ b/Parasitemia/Parasitemia/ImgTools.fs @@ -32,6 +32,123 @@ let saveMat (mat: Matrix<'TDepth>) (filepath: string) = saveImg img filepath +type Histogram = { data: int[]; total: int; sum: int; min: float32; max: float32 } + +let histogramImg (img: Image) (nbSamples: int) : Histogram = + let imgData = img.Data + + let min, max = + let min = ref [| 0.0 |] + let minLocation = ref <| [| Point() |] + let max = ref [| 0.0 |] + let maxLocation = ref <| [| Point() |] + img.MinMax(min, max, minLocation, maxLocation) + float32 (!min).[0], float32 (!max).[0] + + let bin (x: float32) : int = + let p = int ((x - min) / (max - min) * float32 nbSamples) + if p >= nbSamples then nbSamples - 1 else p + + let data = Array.zeroCreate nbSamples + + for i in 0 .. img.Height - 1 do + for j in 0 .. img.Width - 1 do + let p = bin imgData.[i, j, 0] + data.[p] <- data.[p] + 1 + + { data = data; total = img.Height * img.Width; sum = Array.sum data; min = min; max = max } + +let histogramMat (mat: Matrix) (nbSamples: int) : Histogram = + let matData = mat.Data + + let min, max = + let min = ref 0.0 + let minLocation = ref <| Point() + let max = ref 0.0 + let maxLocation = ref <| Point() + mat.MinMax(min, max, minLocation, maxLocation) + float32 !min, float32 !max + + let bin (x: float32) : int = + let p = int ((x - min) / (max - min) * float32 nbSamples) + if p >= nbSamples then nbSamples - 1 else p + + let data = Array.zeroCreate nbSamples + + for i in 0 .. mat.Height - 1 do + for j in 0 .. mat.Width - 1 do + let p = bin matData.[i, j] + data.[p] <- data.[p] + 1 + + { data = data; total = mat.Height * mat.Width; sum = Array.sum data; min = min; max = max } + +let histogram (values: float32 seq) (nbSamples: int) : Histogram = + let mutable min = Single.MaxValue + let mutable max = Single.MinValue + let mutable n = 0 + + for v in values do + n <- n + 1 + if v < min then min <- v + if v > max then max <- v + + let bin (x: float32) : int = + let p = int ((x - min) / (max - min) * float32 nbSamples) + if p >= nbSamples then nbSamples - 1 else p + + let data = Array.zeroCreate nbSamples + + for v in values do + let p = bin v + data.[p] <- data.[p] + 1 + + { data = data; total = n; sum = Array.sum data; min = min; max = max } + +let otsu (hist: Histogram) : float32 * float32 * float32 = + let mutable sumB = 0 + let mutable wB = 0 + let mutable maximum = 0.0 + let mutable level = 0 + let sum = hist.data |> Array.mapi (fun i v -> i * v) |> Array.sum |> float + + for i in 0 .. hist.data.Length - 1 do + wB <- wB + hist.data.[i] + if wB <> 0 + then + let wF = hist.total - wB + if wF <> 0 + then + sumB <- sumB + i * hist.data.[i] + let mB = (float sumB) / (float wB) + let mF = (sum - float sumB) / (float wF) + let between = (float wB) * (float wF) * (mB - mF) ** 2.; + if between >= maximum + then + level <- i + maximum <- between + + let mean1 = + let mutable sum = 0 + let mutable nb = 0 + for i in 0 .. level - 1 do + sum <- sum + i * hist.data.[i] + nb <- nb + hist.data.[i] + (sum + level * hist.data.[level] / 2) / (nb + hist.data.[level] / 2) + + let mean2 = + let mutable sum = 0 + let mutable nb = 0 + for i in level + 1 .. hist.data.Length - 1 do + sum <- sum + i * hist.data.[i] + nb <- nb + hist.data.[i] + (sum + level * hist.data.[level] / 2) / (nb + hist.data.[level] / 2) + + let toFloat l = + float32 l / float32 hist.data.Length * (hist.max - hist.min) + hist.min + + toFloat level, toFloat mean1, toFloat mean2 + + let suppressMConnections (img: Matrix) = let w = img.Width let h = img.Height @@ -79,9 +196,11 @@ let findEdges (img: Image) : Matrix * Image let thresholdHigh, thresholdLow = let sensibilityHigh = 0.1f - let sensibilityLow = 0.1f + let sensibilityLow = 0.0f use magnitudesByte = magnitudes.Convert() let threshold = float32 <| CvInvoke.Threshold(magnitudesByte, magnitudesByte, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) + let threshold, _, _ = otsu (histogramMat magnitudes 300) + threshold + (sensibilityHigh * threshold), threshold - (sensibilityLow * threshold) // Non-maximum suppression. diff --git a/Parasitemia/Parasitemia/KMeans.fs b/Parasitemia/Parasitemia/KMeans.fs index 15651ae..55b899d 100644 --- a/Parasitemia/Parasitemia/KMeans.fs +++ b/Parasitemia/Parasitemia/KMeans.fs @@ -6,6 +6,7 @@ open System.Drawing open Emgu.CV open Emgu.CV.Structure + type Result = { fg: Image mean_bg: float32 @@ -13,7 +14,7 @@ type Result = { d_fg: Image } // Euclidean distances of the foreground to mean_fg. let kmeans (img: Image) : Result = - let nbIteration = 3 + let nbIteration = 4 let w = img.Width let h = img.Height diff --git a/Parasitemia/Parasitemia/KMedians.fs b/Parasitemia/Parasitemia/KMedians.fs index f7f2e54..d005651 100644 --- a/Parasitemia/Parasitemia/KMedians.fs +++ b/Parasitemia/Parasitemia/KMedians.fs @@ -13,7 +13,7 @@ type Result = { d_fg: Image } // Euclidean distances of the foreground to median_fg. let kmedians (img: Image) : Result = - let nbIteration = 3 + let nbIteration = 4 let w = img.Width let h = img.Height diff --git a/Parasitemia/Parasitemia/MainAnalysis.fs b/Parasitemia/Parasitemia/MainAnalysis.fs index ec2aabb..9f91a6d 100644 --- a/Parasitemia/Parasitemia/MainAnalysis.fs +++ b/Parasitemia/Parasitemia/MainAnalysis.fs @@ -13,9 +13,7 @@ open Types let doAnalysis (img: Image) (name: string) (config: Config) : Cell list = - let scaledImg = if config.Parameters.scale = 1.0 then img else img.Resize(config.Parameters.scale, CvEnum.Inter.Area) - - use green = scaledImg.Item(1) + use green = img.Item(1) let greenFloat = green.Convert() let filteredGreen = gaussianFilter greenFloat (float config.Parameters.preFilterSigma) @@ -58,7 +56,7 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.05 saveImg imgAllEllipses (buildFileName " - ellipses - all.png") - let imgEllipses = img.Copy() + let imgEllipses = filteredGreenWhitoutStain.Convert() drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0 saveImg imgEllipses (buildFileName " - ellipses.png") @@ -81,10 +79,10 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li saveImg green (buildFileName " - green.png") - use blue = scaledImg.Item(0) + use blue = img.Item(0) saveImg blue (buildFileName " - blue.png") - use red = scaledImg.Item(2) + use red = img.Item(2) saveImg red (buildFileName " - red.png") | _ -> () diff --git a/Parasitemia/Parasitemia/ParasitesMarker.fs b/Parasitemia/Parasitemia/ParasitesMarker.fs index f1a94e1..720488d 100644 --- a/Parasitemia/Parasitemia/ParasitesMarker.fs +++ b/Parasitemia/Parasitemia/ParasitesMarker.fs @@ -18,7 +18,6 @@ type Result = { // * 'Stain' corresponds to the stain around the parasites. // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes. let findMa (green: Image) (filteredGreen: Image) (config: Config.Config) : Result * Image * Image = - // We use the filtered image to find the dark stain. let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians filteredGreen) let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults @@ -51,7 +50,6 @@ let findMa (green: Image) (filteredGreen: Image) ( // * 'Stain' corresponds to the stain around the parasites. // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes. let find (filteredGreen: Image) (config: Config.Config) : Result * Image = - use filteredGreenWithoutInfection = filteredGreen.Copy() ImgTools.areaCloseF filteredGreenWithoutInfection (int config.InfectionArea) @@ -59,20 +57,14 @@ let find (filteredGreen: Image) (config: Config.Config) : Result ImgTools.areaCloseF filteredGreenWithoutStain (int config.StainArea) // We use the filtered image to find the dark stain. + let _, mean_fg, mean_bg = + let hist = ImgTools.histogramImg filteredGreenWithoutInfection 300 + ImgTools.otsu hist - // With K-Means. - let kmeansResults = logTime "Finding fg/bg (k-means)" (fun () -> KMeans.kmeans (filteredGreenWithoutInfection)) - let { KMeans.mean_bg = value_bg; KMeans.mean_fg = value_fg; KMeans.d_fg = d_fg } = kmeansResults - - // With K-Medians. - (* let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians (filteredGreenWithoutInfection)) // FIXME: avoid converting this again in MainAnalysis - let { KMedians.median_bg = value_bg; KMedians.median_fg = value_fg; KMedians.d_fg = d_fg } = kmediansResults *) - - let darkStain = d_fg.Cmp((float value_bg) * config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan) - darkStain._And(filteredGreenWithoutInfection.Cmp(float value_fg, CvEnum.CmpType.LessThan)) + let darkStain = filteredGreenWithoutInfection.Cmp(-(float mean_bg) * config.Parameters.darkStainLevel + (float mean_fg), CvEnum.CmpType.LessThan) let marker (img: Image) (closed: Image) (level: float) : Image = - let diff = img.Copy() // closed - (img * level) + let diff = img.Copy() diff._Mul(level) CvInvoke.Subtract(closed, diff, diff) diff._ThresholdBinary(Gray(0.0), Gray(255.)) diff --git a/Parasitemia/Parasitemia/Program.fs b/Parasitemia/Parasitemia/Program.fs index 09f4312..f477cfe 100644 --- a/Parasitemia/Parasitemia/Program.fs +++ b/Parasitemia/Parasitemia/Program.fs @@ -62,8 +62,7 @@ let main args = | mode, debug -> let config = Config( - { scale = 1. - + { initialAreaOpen = 2000 minRbcRadius = -0.32f @@ -73,7 +72,7 @@ let main args = factorNbPick = 1.0 - darkStainLevel = 0.22 // Lower -> more sensitive. 0.3. Careful about illumination on the borders. + darkStainLevel = 0.25 // Lower -> more sensitive. 0.3. Careful about illumination on the borders. maxDarkStainRatio = 0.1 // 10 % infectionArea = 0.012f // 1.2 %