X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FImgTools.fs;h=7253390b31813230a69e47926db113f3eba9bd53;hb=10afa9a402eb88c8e073fe8b0d607faa25230eef;hp=c34b9c76469c17d49d4af615781391db67cfb08a;hpb=e76da913cd58078ad2479357b2430ed62a6e0777;p=master-thesis.git diff --git a/Parasitemia/Parasitemia/ImgTools.fs b/Parasitemia/Parasitemia/ImgTools.fs index c34b9c7..7253390 100644 --- a/Parasitemia/Parasitemia/ImgTools.fs +++ b/Parasitemia/Parasitemia/ImgTools.fs @@ -25,62 +25,45 @@ let gaussianFilter (img : Image<'TColor, 'TDepth>) (standardDeviation : float) : // Zhang and Suen algorithm. // Modify 'mat' in place. let thin (mat: Matrix) = - let neighbors = [| - (-1, 0) // p2 - (-1, 1) // p3 - ( 0, 1) // p4 - ( 1, 1) // p5 - ( 1, 0) // p6 - ( 1, -1) // p7 - ( 0, -1) // p8 - (-1, -1) |] // p9 - let w = mat.Width let h = mat.Height let mutable data1 = mat.Data - let mutable data2 = Array2D.zeroCreate h w - - // Return the list of neighbor values. - let neighborsValues (p1i, p1j) = - Array.map (fun (ni, nj) -> - let pi = p1i + ni - let pj = p1j + nj - if pi < 0 || pi >= h || pj < 0 || pj >= w then 0uy else data1.[pi, pj] - ) neighbors - - // Return the number of 01 pattern in 'values' in a circular way. - let pattern01 (values: byte[]) = - let mutable nb = 0 - let mutable lastValue = 255uy - for v in values do - if lastValue = 0uy && v = 1uy - then - nb <- nb + 1 - lastValue <- v - if lastValue = 0uy && values.[0] = 1uy - then - nb <- nb + 1 - nb + let mutable data2 = Array2D.copy data1 let mutable pixelChanged = true let mutable oddIteration = true + while pixelChanged do pixelChanged <- false for i in 0..h-1 do for j in 0..w-1 do if data1.[i, j] = 1uy then - let values = neighborsValues (i, j) - let s = Array.reduce (+) values - if s >= 2uy && s <= 6uy && - pattern01 values = 1 && - (not oddIteration || (values.[0] * values.[2] * values.[4] = 0uy && values.[2] * values.[4] * values.[6] = 0uy)) && // Odd iteration. - (oddIteration || (values.[0] * values.[2] * values.[6] = 0uy && values.[0] * values.[4] * values.[6] = 0uy)) // Even iterations. + let p2 = if i = 0 then 0uy else data1.[i-1, j] + let p3 = if i = 0 || j = w-1 then 0uy else data1.[i-1, j+1] + let p4 = if j = w-1 then 0uy else data1.[i, j+1] + let p5 = if i = h-1 || j = w-1 then 0uy else data1.[i+1, j+1] + let p6 = if i = h-1 then 0uy else data1.[i+1, j] + let p7 = if i = h-1 || j = 0 then 0uy else data1.[i+1, j-1] + let p8 = if j = 0 then 0uy else data1.[i, j-1] + let p9 = if i = 0 || j = 0 then 0uy else data1.[i-1, j-1] + + let sumNeighbors = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + if sumNeighbors >= 2uy && sumNeighbors <= 6uy && + (if p2 = 0uy && p3 = 1uy then 1 else 0) + + (if p3 = 0uy && p4 = 1uy then 1 else 0) + + (if p4 = 0uy && p5 = 1uy then 1 else 0) + + (if p5 = 0uy && p6 = 1uy then 1 else 0) + + (if p6 = 0uy && p7 = 1uy then 1 else 0) + + (if p7 = 0uy && p8 = 1uy then 1 else 0) + + (if p8 = 0uy && p9 = 1uy then 1 else 0) + + (if p9 = 0uy && p2 = 1uy then 1 else 0) = 1 && + if oddIteration + then p2 * p4 * p6 = 0uy && p4 * p6 * p8 = 0uy + else p2 * p4 * p8 = 0uy && p2 * p6 * p8 = 0uy then data2.[i, j] <- 0uy pixelChanged <- true - else - data2.[i, j] <- 1uy else data2.[i, j] <- 0uy @@ -90,7 +73,6 @@ let thin (mat: Matrix) = data2 <- tmp - let pop (l: List<'a>) : 'a = let n = l.[l.Count - 1] l.RemoveAt(l.Count - 1) @@ -174,45 +156,37 @@ let saveMat (mat: Matrix<'TDepth>) (filepath: string) = mat.CopyTo(img) saveImg img filepath -(*let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColor) = - let e' = Ellipse(PointF(float32 e.cx, float32 e.cy), SizeF(2.0f * float32 e.a, 2.0f * float32 e.b), float32 e.alpha) - img.Draw(e', color)*) +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 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 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 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 drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColor) (alpha: float) = -let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColor) = - let cosAlpha = cos e.Alpha - let sinAlpha = sin e.Alpha - - 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() @@ -235,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