From: Greg Burri Date: Mon, 14 Dec 2015 18:13:10 +0000 (+0100) Subject: * Remove ellipses with too small area (improved). X-Git-Tag: 1.0.11~80 X-Git-Url: http://git.euphorik.ch/?a=commitdiff_plain;h=10afa9a402eb88c8e073fe8b0d607faa25230eef;p=master-thesis.git * Remove ellipses with too small area (improved). * Draw all the ellipses in debug mode. --- diff --git a/Parasitemia/Parasitemia/Classifier.fs b/Parasitemia/Parasitemia/Classifier.fs index 52e7dc6..e0bbfa9 100644 --- a/Parasitemia/Parasitemia/Classifier.fs +++ b/Parasitemia/Parasitemia/Classifier.fs @@ -59,28 +59,47 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (fg: let ellipsesWithNeigbors = ellipsesInside |> List.choose (fun e -> if e.Removed then None else Some (e, neighbors e)) - // 3) Remove ellipse with a lower percentage of foreground. + // 3) Remove ellipses with a lower percentage of foreground. for e, neighbors in ellipsesWithNeigbors do let minX, minY, maxX, maxY = ellipseWindow e let mutable totalElement = 0 let mutable fgElement = 0 - for y in minY .. maxY do for x in minX .. maxX do let yf, xf = float y, float x if e.Contains xf yf && neighbors |> List.forall (fun (otherE, _, _) -> not <| otherE.Contains xf yf) - then - totalElement <- totalElement + 1 - if fg.Data.[y, x, 0] > 0uy then - fgElement <- fgElement + 1 + totalElement <- totalElement + 1 + if fg.Data.[y, x, 0] > 0uy + then + fgElement <- fgElement + 1 - if totalElement < config.minimumCellArea || (float fgElement) / (float totalElement) < config.percentageOfFgValidCell + if (float fgElement) / (float totalElement) < config.percentageOfFgValidCell then e.Removed <- true - // 3) Define pixels associated to each ellipse and create the cells. + // 4) Remove ellipses with little area. + for e, neighbors in ellipsesWithNeigbors do + if not e.Removed + then + let minX, minY, maxX, maxY = ellipseWindow e + + let mutable area = 0 + for y in minY .. maxY do + for x in minX .. maxX do + let yf, xf = float y, float x + if fg.Data.[y, x, 0] > 0uy && + e.Contains xf yf && + neighbors |> List.forall (fun (otherE, _, _) -> otherE.Removed || not <| otherE.Contains xf yf) + then + area <- area + 1 + + if area < config.minimumCellArea + then + e.Removed <- true + + // 5) Define pixels associated to each ellipse and create the cells. // Return 'true' if the point 'p' is owned by e. // The lines represents all intersections with other ellipses. diff --git a/Parasitemia/Parasitemia/Config.fs b/Parasitemia/Parasitemia/Config.fs index 8a172b8..464fd7e 100644 --- a/Parasitemia/Parasitemia/Config.fs +++ b/Parasitemia/Parasitemia/Config.fs @@ -16,6 +16,10 @@ type Config = { doGSigma2: float doGLowFreqPercentageReduction: float + // Ellipse. + factorNbPick: float + factorWindowSize: float // factor of 'maxRBCSize'. + // Parasites detection. darkStainLevel: float diff --git a/Parasitemia/Parasitemia/Ellipse.fs b/Parasitemia/Parasitemia/Ellipse.fs index 62c0936..128c823 100644 --- a/Parasitemia/Parasitemia/Ellipse.fs +++ b/Parasitemia/Parasitemia/Ellipse.fs @@ -7,28 +7,29 @@ open Emgu.CV open Emgu.CV.Structure open Utils +open Config open MatchingEllipses type private SearchExtremum = Minimum | Maximum - + let private goldenSectionSearch (f: float -> float) (nbIter: int) (xmin: float) (xmax: float) (searchExtremum: SearchExtremum) : (float * float) = let gr = 1.0 / 1.6180339887498948482 let mutable a = xmin let mutable b = xmax let mutable c = b - gr * (b - a) let mutable d = a + gr * (b - a) - + for i in 1 .. nbIter do let mutable fc = f c let mutable fd = f d - + if searchExtremum = Maximum then let tmp = fc fc <- fd fd <- tmp - + if fc < fd then b <- d @@ -38,7 +39,7 @@ let private goldenSectionSearch (f: float -> float) (nbIter: int) (xmin: float) a <- c c <- d d <- a + gr * (b - a) - + let x = (b + a) / 2.0 x, f x @@ -51,92 +52,92 @@ let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2: // p3 as the referencial. let p1x = p1x - p3x let p1y = p1y - p3y - + let p2x = p2x - p3x let p2y = p2y - p3y - + // Convert to polar coordinates. let alpha1 = atan m1 let alpha2 = atan m2 - + let r1 = sqrt (p1x ** 2.0 + p1y ** 2.0) let theta1 = atan2 p1y p1x let r2 = sqrt (p2x**2.0 + p2y**2.0) let theta2 = atan2 p2y p2x - let valid = - 4.0 * sin (alpha1 - theta1) * (-r1 * sin (alpha1 - theta1) + r2 * sin (alpha1 - theta2)) * + let valid = + 4.0 * 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.0 * sin (theta1 - theta2) ** 2.0 < 0.0 - + if valid then - let r theta = + 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.0 - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.0) - + 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.0 Math.PI Maximum let rTan = r thetaTan - + let PTanx = rTan * cos thetaTan let PTany = rTan * sin thetaTan - + let d1a = tan alpha1 let d1b = -d1a * p1x + p1y - + let d2a = tan alpha2 let d2b = -d2a * p2x + p2y - + let d3a = -1.0 / tan thetaTan let d3b = -d3a * PTanx + PTany - + let Ux = -(d1b - d2b) / (d1a - d2a) let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a) - + let Vx = -(d1b - d3b) / (d1a - d3a) let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a) - + let Wx = p1x + (p2x - p1x) / 2.0 let Wy = p1y + (p2y - p1y) / 2.0 - + let Zx = p1x + (PTanx - p1x) / 2.0 let Zy = p1y + (PTany - p1y) / 2.0 - + let va = -(-Vy + Zy) / (Vx - Zx) let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx) - + let ua = -(-Uy + Wy) / (Ux - Wx) let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx) - + let cx = -(vb - ub) / (va - ua) let cy = -(ua * vb - va * ub) / (va - ua) - + let rc = sqrt (cx**2.0 + cy**2.0) let psi = atan2 cy cx - - let rellipse theta = + + let rellipse theta = sqrt ( rc ** 2.0 + (r1 ** 2.0 * r2 ** 2.0 * (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.0 * sin (theta1 - theta2) ** 2.0) / (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2.0 - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.0) ** 2.0 - (2.0 * 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.0 - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.0)) - - // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval. + + // 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.0 (Math.PI / 2.0) Maximum // Pi/2 and not pi because the period is Pi. let r2eTheta, r2e = goldenSectionSearch rellipse accuracy_extremum_search_2 0.0 (Math.PI / 2.0) 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.0 then - alpha <- alpha + Math.PI - + alpha <- alpha + Math.PI + // Ride off the p3 referential. let cx = cx + p3x let cy = cy + p3y @@ -153,9 +154,9 @@ let private vectorRotation (p1x: float) (p1y: float) (v1x: float) (v1y: float) ( if v1x > 0.0 then rotation <- -1.0 - elif p1y < py + elif p1y < py then - if v1x < 0.0 + if v1x < 0.0 then rotation <- -1.0 elif p1x > px @@ -163,9 +164,9 @@ let private vectorRotation (p1x: float) (p1y: float) (v1x: float) (v1y: float) ( if v1y < 0.0 then rotation <- -1.0 - elif p1x < px + elif p1x < px then - if v1y > 0.0 + if v1y > 0.0 then rotation <- -1.0 rotation @@ -174,15 +175,15 @@ let private vectorRotation (p1x: float) (p1y: float) (v1x: float) (v1y: float) ( let private areVectorsValid (p1x: float) (p1y: float) (p2x: float) (p2y: float) (v1x: float) (v1y: float) (v2x: float) (v2y: float) : (float * float) option = let m1 = -v1x / v1y let m2 = -v2x / v2y - + let b1 = -m1 * p1x + p1y let b2 = -m2 * p2x + p2y let px = -((b1 - b2)/(m1 - m2)) let py = -((m2 * b1 - m1 * b2)/(m1 - m2)) - + let rot1 = vectorRotation p1x p1y v1x v1y px py let rot2 = vectorRotation p2x p2y v2x v2y px py - + if rot1 = rot2 || rot1 * atan2 (p1y - py) (p1x - px) + rot2 * atan2 (p2y - py) (p2x - px) <= 0.0 then None @@ -190,15 +191,16 @@ let private areVectorsValid (p1x: float) (p1y: float) (p2x: float) (p2y: float) Some (m1, m2) let find (edges: Matrix) - (xDir: Image) - (yDir: Image) - (radiusRange: float * float) - (windowSize: int) - (factorNbPick: float) : Types.Ellipse list = + (xDir: Image) + (yDir: Image) + (config: Config) : MatchingEllipses = + + let r1, r2 = config.scale * config.minRBCSize, config.scale * config.maxRBCSize + let windowSize = roundInt (config.factorWindowSize * r2) + let factorNbPick = config.factorNbPick let increment = windowSize / 4 - let r1, r2 = radiusRange let radiusTolerance = (r2 - r1) * 0.2 let minimumDistance = (r2 / 1.5) ** 2.0 @@ -206,7 +208,7 @@ let find (edges: Matrix) let h = edges.Height let w = edges.Width - + let mutable last_i, last_j = Int32.MaxValue, Int32.MaxValue let currentElements = List<(int * int)>() @@ -216,12 +218,12 @@ let find (edges: Matrix) let yDirData = yDir.Data let rng = Random(42) - + let ellipses = MatchingEllipses(r1) - - for window_i in -windowSize + increment .. increment .. h - increment do - for window_j in -windowSize + increment .. increment .. w - increment do - + + for window_i in -windowSize + increment .. increment .. h - increment do + for window_j in -windowSize + increment .. increment .. w - increment do + let window_i_begin = if window_i < 0 then 0 else window_i let window_i_end = if window_i + windowSize - 1 >= h then h - 1 else window_i + windowSize - 1 let window_j_begin = if window_j < 0 then 0 else window_j @@ -258,13 +260,13 @@ let find (edges: Matrix) match areVectorsValid p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0] with | Some (m1, m2) -> match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf p3yf with - | Some e when e.Cx > 0.0 && e.Cx < (float w) - 1.0 && e.Cy > 0.0 && e.Cy < (float h) - 1.0 && + | Some e when e.Cx > 0.0 && e.Cx < (float w) - 1.0 && e.Cy > 0.0 && e.Cy < (float h) - 1.0 && e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance -> ellipses.Add e | _ -> () | _ -> () currentElements.Clear() - - ellipses.Ellipses + + ellipses diff --git a/Parasitemia/Parasitemia/ImgTools.fs b/Parasitemia/Parasitemia/ImgTools.fs index f7e5a4a..7253390 100644 --- a/Parasitemia/Parasitemia/ImgTools.fs +++ b/Parasitemia/Parasitemia/ImgTools.fs @@ -156,41 +156,37 @@ let saveMat (mat: Matrix<'TDepth>) (filepath: string) = mat.CopyTo(img) saveImg img filepath -let drawLine (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: int) (y0: int) (x1: int) (y1: int) = - img.Draw(LineSegment2D(Point(x0, y0), Point(x1, y1)), color, 1); +let drawLine (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: int) (y0: int) (x1: int) (y1: int) (thickness: int) = + img.Draw(LineSegment2D(Point(x0, y0), Point(x1, y1)), color, thickness); -let drawLineF (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: float) (y0: float) (x1: float) (y1: float) = - let x0, y0, x1, y1 = roundInt(x0), roundInt(y0), roundInt(x1), roundInt(y1) - drawLine img color x0 y0 x1 y1 +let drawLineF (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: float) (y0: float) (x1: float) (y1: float) (thickness: int) = + img.Draw(LineSegment2DF(PointF(float32 x0, float32 y0), PointF(float32 x1, float32 y1)), color, thickness, CvEnum.LineType.AntiAlias); -let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColor) = - let cosAlpha = cos e.Alpha - let sinAlpha = sin e.Alpha +let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColor) (alpha: float) = - let mutable x0 = 0.0 - let mutable y0 = 0.0 - let mutable first_iteration = true + if alpha >= 1.0 + then + img.Draw(Ellipse(PointF(float32 e.Cx, float32 e.Cy), SizeF(2. * e.B |> float32, 2. * e.A |> float32), float32 <| e.Alpha / Math.PI * 180.), color, 1, CvEnum.LineType.AntiAlias) + else + let windowPosX = e.Cx - e.A - 5.0 + let gapX = windowPosX - (float (int windowPosX)) - let n = 40 - let thetaIncrement = 2.0 * Math.PI / (float n) + let windowPosY = e.Cy - e.A - 5.0 + let gapY = windowPosY - (float (int windowPosY)) - for theta in 0.0 .. thetaIncrement .. 2.0 * Math.PI do - let cosTheta = cos theta - let sinTheta = sin theta - let x = e.Cx + cosAlpha * e.A * cosTheta - sinAlpha * e.B * sinTheta - let y = e.Cy + sinAlpha * e.A * cosTheta + cosAlpha * e.B * sinTheta + let roi = Rectangle(int windowPosX, int windowPosY, 2. * (e.A + 5.0) |> int, 2.* (e.A + 5.0) |> int) - if not first_iteration + img.ROI <- roi + if roi = img.ROI // We do not display ellipses touching the edges (FIXME) then - drawLineF img color x0 y0 x y - else - first_iteration <- false + use i = new Image<'TColor, 'TDepth>(img.ROI.Size) + i.Draw(Ellipse(PointF(float32 <| (e.A + 5. + gapX) , float32 <| (e.A + 5. + gapY)), SizeF(2. * e.B |> float32, 2. * e.A |> float32), float32 <| e.Alpha / Math.PI * 180.), color, 1, CvEnum.LineType.AntiAlias) + CvInvoke.AddWeighted(img, 1.0, i, alpha, 0.0, img) + img.ROI <- Rectangle.Empty - x0 <- x - y0 <- y -let drawEllipses (img: Image<'TColor, 'TDepth>) (ellipses: Types.Ellipse list) (color: 'TColor) = - List.iter (fun e -> drawEllipse img e color) ellipses +let drawEllipses (img: Image<'TColor, 'TDepth>) (ellipses: Types.Ellipse list) (color: 'TColor) (alpha: float) = + List.iter (fun e -> drawEllipse img e color alpha) ellipses let rngCell = System.Random() @@ -213,13 +209,18 @@ let drawCell (img: Image) (drawCellContent: bool) (c: Types.Cell) = img.Data.[y + dy, x + dx, 1] <- if g + colorG > 255 then 255uy else byte (g + colorG) img.Data.[y + dy, x + dx, 2] <- if r + colorR > 255 then 255uy else byte (r + colorR) - let crossColor = match c.cellClass with - | Types.HealthyRBC -> Bgr(255.0, 0.0, 0.0) - | Types.InfectedRBC -> Bgr(0.0, 0.0, 255.0) - | Types.Peculiar -> Bgr(0.0, 0.0, 0.0) + let crossColor, crossColor2 = + match c.cellClass with + | Types.HealthyRBC -> Bgr(255., 0., 0.), Bgr(255., 255., 255.) + | Types.InfectedRBC -> Bgr(0., 0., 255.), Bgr(120., 120., 255.) + | Types.Peculiar -> Bgr(0., 0., 0.), Bgr(80., 80., 80.) + + drawLine img crossColor2 (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 2 + drawLine img crossColor2 c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 2 + + drawLine img crossColor (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 1 + drawLine img crossColor c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 1 - drawLine img crossColor (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y - drawLine img crossColor c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) let drawCells (img: Image) (drawCellContent: bool) (cells: Types.Cell list) = List.iter (fun c -> drawCell img drawCellContent c) cells \ No newline at end of file diff --git a/Parasitemia/Parasitemia/MainAnalysis.fs b/Parasitemia/Parasitemia/MainAnalysis.fs index c9e9422..9308780 100644 --- a/Parasitemia/Parasitemia/MainAnalysis.fs +++ b/Parasitemia/Parasitemia/MainAnalysis.fs @@ -59,20 +59,15 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li logTime "Finding edges" (fun() -> thin edges) logTime "Removing small connected components from thinning" (fun () -> removeArea edges 12) - (* let kmediansResults = KMedians.kmedians filteredGreen 1.0 let parasites = ParasitesMarker.find green filteredGreen kmediansResults config - let radiusRange = config.scale * config.minRBCSize, config.scale * config.maxRBCSize - let windowSize = roundInt (1.6 * (snd radiusRange)) - let factorNbPick = 1.5 - let ellipses = logTime "Finding ellipses" (fun () -> - Ellipse.find edges xEdges yEdges radiusRange windowSize factorNbPick) + let allEllipses, ellipses = logTime "Finding ellipses" (fun () -> + let matchingEllipses = Ellipse.find edges xEdges yEdges config + matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses ) let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites kmediansResults.fg config) - *) - let cells = [] // Output pictures if debug flag is set. match config.debug with @@ -80,12 +75,16 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li let buildFileName postfix = System.IO.Path.Combine(output, name + postfix) saveMat (edges * 255.0) (buildFileName " - edges.png") - (*saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png") + saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png") saveImg parasites.stain (buildFileName " - parasites - stain.png") saveImg parasites.infection (buildFileName " - parasites - infection.png") + let imgAllEllipses = img.Copy() + drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.1 + saveImg imgAllEllipses (buildFileName " - ellipses - all.png") + let imgEllipses = img.Copy() - drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) + drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0 saveImg imgEllipses (buildFileName " - ellipses.png") saveImg (kmediansResults.fg * 255.0) (buildFileName " - foreground.png") @@ -96,7 +95,7 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li let imgCells' = img.Copy() drawCells imgCells' true cells - saveImg imgCells' (buildFileName " - cells - full.png")*) + saveImg imgCells' (buildFileName " - cells - full.png") | _ -> () cells diff --git a/Parasitemia/Parasitemia/MatchingEllipses.fs b/Parasitemia/Parasitemia/MatchingEllipses.fs index 8a1b4f8..da74c65 100644 --- a/Parasitemia/Parasitemia/MatchingEllipses.fs +++ b/Parasitemia/Parasitemia/MatchingEllipses.fs @@ -10,7 +10,7 @@ open Utils let matchingScoreThreshold1 = 0.6 -let matchingScoreThreshold2 = 1.0 +let matchingScoreThreshold2 = 1. type private EllipseScoreFlaggedKd (matchingScore: float, e: Ellipse) = let mutable matchingScore = matchingScore @@ -24,18 +24,21 @@ type private EllipseScoreFlaggedKd (matchingScore: float, e: Ellipse) = member val Processed = false with get, set member val Removed = false with get, set - interface KdTree.I2DCoords with + interface KdTree.I2DCoords with member this.X = this.Ellipse.Cx member this.Y = this.Ellipse.Cy - + type MatchingEllipses (radiusMin: float) = let ellipses = List() - - member this.Add (e: Ellipse) = + + member this.Add (e: Ellipse) = ellipses.Add(EllipseScoreFlaggedKd(0.0, e)) member this.Ellipses : Ellipse list = + List.ofSeq ellipses |> List.map (fun e -> e.Ellipse) + + member this.PrunedEllipses : Ellipse list = if ellipses.Count = 0 then [] @@ -43,13 +46,13 @@ type MatchingEllipses (radiusMin: float) = // 1) Create a kd-tree from the ellipses list. let tree = KdTree.Tree.BuildTree (List.ofSeq ellipses) - // 2) Compute the matching score of each ellipses. + // 2) Compute the matching score of each ellipses. let windowSize = radiusMin for e in ellipses do e.Processed <- true let areaE = e.Ellipse.Area let window = { KdTree.minX = e.Ellipse.Cx - windowSize / 2.0 - KdTree.maxX = e.Ellipse.Cx + windowSize / 2.0 + KdTree.maxX = e.Ellipse.Cx + windowSize / 2.0 KdTree.minY = e.Ellipse.Cy - windowSize / 2.0 KdTree.maxY = e.Ellipse.Cy + windowSize / 2.0 } for other in tree.Search window do @@ -68,7 +71,7 @@ type MatchingEllipses (radiusMin: float) = // 3) Sort ellipses by their score. ellipses.Sort(fun e1 e2 -> e2.MatchingScore.CompareTo(e1.MatchingScore)) - // 4) Remove ellipses wich have a low score. + // 4) Remove ellipses wich have a low score. let i = ellipses.BinarySearch(EllipseScoreFlaggedKd(matchingScoreThreshold2, Ellipse(0.0, 0.0, 0.0, 0.0, 0.0)), { new IComparer with member this.Compare(e1, e2) = e2.MatchingScore.CompareTo(e1.MatchingScore) }) |> abs @@ -84,12 +87,12 @@ type MatchingEllipses (radiusMin: float) = if not e.Removed then let window = { KdTree.minX = e.Ellipse.Cx - e.Ellipse.A - KdTree.maxX = e.Ellipse.Cx + e.Ellipse.A + KdTree.maxX = e.Ellipse.Cx + e.Ellipse.A KdTree.minY = e.Ellipse.Cy - e.Ellipse.A KdTree.maxY = e.Ellipse.Cy + e.Ellipse.A } for other in tree.Search window do if not other.Removed && other.MatchingScore < e.MatchingScore - then + then if e.Ellipse.Contains other.Ellipse.Cx other.Ellipse.Cy then other.Removed <- true @@ -97,5 +100,5 @@ type MatchingEllipses (radiusMin: float) = List.ofSeq ellipses |> List.map (fun e -> e.Ellipse) - + diff --git a/Parasitemia/Parasitemia/ParasitesMarker.fs b/Parasitemia/Parasitemia/ParasitesMarker.fs index e422b04..9adf185 100644 --- a/Parasitemia/Parasitemia/ParasitesMarker.fs +++ b/Parasitemia/Parasitemia/ParasitesMarker.fs @@ -16,8 +16,6 @@ type Result = { // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes. let find (green: Image) (filteredGreen: Image) (kmediansResult: KMedians.Result) (config: Config.Config) : Result = - let green = ImgTools.gaussianFilter green 1.0 - // We use the filtered image to find the dark stain. let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResult let darkStain = d_fg.Cmp(median_bg * config.darkStainLevel, CvEnum.CmpType.GreaterThan) @@ -25,13 +23,12 @@ let find (green: Image) (filteredGreen: Image) (km darkStain._And(fg) let fgFloat = (fg / 255.0).Convert() - let greenWithoutBg = green.Copy() + use greenWithoutBg = ImgTools.gaussianFilter green 1.0 greenWithoutBg.SetValue(Gray(0.0), fg.Not()) let findSmears (sigma: float) (level: float) : Image = - let greenWithoutBgSmoothed = ImgTools.gaussianFilter greenWithoutBg sigma - let fgSmoothed = ImgTools.gaussianFilter fgFloat sigma - + use greenWithoutBgSmoothed = ImgTools.gaussianFilter greenWithoutBg sigma + use fgSmoothed = ImgTools.gaussianFilter fgFloat sigma let smears = (greenWithoutBg.Mul(fgSmoothed)).Cmp(greenWithoutBgSmoothed.Mul(level), CvEnum.CmpType.LessThan) smears._And(fg) smears diff --git a/Parasitemia/Parasitemia/Program.fs b/Parasitemia/Parasitemia/Program.fs index 76fc244..f23cd39 100644 --- a/Parasitemia/Parasitemia/Program.fs +++ b/Parasitemia/Parasitemia/Program.fs @@ -71,7 +71,10 @@ let main args = doGSigma2 = 20. doGLowFreqPercentageReduction = 0.75 - darkStainLevel = 0.5 + factorNbPick = 2.0 + factorWindowSize = 1.6 + + darkStainLevel = 0.4 // Lower -> more sensitive. stainSigma = 10. stainLevel = 0.9 @@ -85,7 +88,7 @@ let main args = MaxDarkStainRatio = 0.1 - minimumCellArea = 600. * scale ** 2. |> int + minimumCellArea = 1200. * scale ** 2. |> int maxOffcenter = 0.5 } match mode with