15 let doAnalysis (img
: Image<Bgr, byte
>) (name
: string) (config
: Config) : Cell list =
17 use scaledImg = if config
.Parameters.scale
= 1.0 then img
else img.Resize(config
.Parameters.scale
, CvEnum.Inter.Area)
19 use green = scaledImg.Item(1)
21 let greenFloat = green.Convert<Gray, float32
>()
23 let filteredGreen = gaussianFilter
green config
.Parameters.preFilterSigma
25 (*let maximaImg = filteredGreen.Copy()
26 let maxima = logTime "maxima" (fun () -> ImgTools.findMaxima maximaImg)
28 ImgTools.drawPoints maximaImg m 255uy
30 let greenOpen1 = filteredGreen.Copy()
31 logTime "areaOpen1" (fun () -> ImgTools.areaOpen greenOpen1 2000)*)
33 let initialAreaOpen = 2000
34 logTime
"areaOpen 1" (fun () -> ImgTools.areaOpen
filteredGreen 2000)
36 config
.RBCRadius <- Granulometry.findRadius
filteredGreen (10, 100) 0.5 |> float
38 let secondAreaOpen = int <| config.RBCArea / 3.
39 if secondAreaOpen > initialAreaOpen
41 logTime
"areaOpen 2" (fun () -> ImgTools.areaOpen
filteredGreen secondAreaOpen)
43 let filteredGreenFloat = filteredGreen.Convert<Gray, float32
>() // Is it neccessary?
45 let parasites, filteredGreenWhitoutInfection
, filteredGreenWhitoutStain
= ParasitesMarker2.find
filteredGreen filteredGreenFloat config
46 let filteredGreenWhitoutInfectionFloat = filteredGreenWhitoutInfection
.Convert<Gray, float32
>()
47 let filteredGreenWhitoutStainFloat = filteredGreenWhitoutStain
.Convert<Gray, float32
>()
50 new ConvolutionKernelF(array2D
[[ 1.0f; 0.0f; -1.0f ]
52 [ 1.0f; 0.0f; -1.0f ]], Point(0, 0))
54 use xEdges = filteredGreenWhitoutStainFloat.Convolution(sobelKernel).Convert<Gray, float>()
55 use yEdges = filteredGreenWhitoutStainFloat.Convolution(sobelKernel.Transpose()).Convert<Gray, float>()
57 let xEdgesData = xEdges.Data
58 let yEdgesData = yEdges.Data
59 for r
in 0..xEdges.Rows-1 do
60 xEdgesData.[r
, 0, 0] <- 0.0
61 xEdgesData.[r
, xEdges.Cols-1, 0] <- 0.0
62 yEdgesData.[r
, 0, 0] <- 0.0
63 yEdgesData.[r
, xEdges.Cols-1, 0] <- 0.0
65 for c
in 0..xEdges.Cols-1 do
66 xEdgesData.[0, c
, 0] <- 0.0
67 xEdgesData.[xEdges.Rows-1, c
, 0] <- 0.0
68 yEdgesData.[0, c
, 0] <- 0.0
69 yEdgesData.[xEdges.Rows-1, c
, 0] <- 0.0
71 use magnitudes = new Matrix<float>(xEdges.Size)
72 CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles).
75 let minLocation = ref <| Point()
77 let maxLocation = ref <| Point()
78 magnitudes.MinMax(min, max, minLocation, maxLocation)
80 use magnitudesByte = ((magnitudes / !max) * 255.0).Convert<byte
>() // Otsu from OpenCV only support 'byte'.
81 use edges = new Matrix<byte
>(xEdges.Size)
82 CvInvoke.Threshold(magnitudesByte, edges, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) |> ignore
84 logTime
"Finding edges" (fun() -> thin
edges)
85 logTime
"Removing small connected components from thinning" (fun () -> removeArea
edges 12)
87 let allEllipses, ellipses
= logTime
"Finding ellipses" (fun () ->
88 let matchingEllipses = Ellipse.find
edges xEdges yEdges config
89 matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses )
91 let cells = logTime
"Classifier" (fun () -> Classifier.findCells
ellipses parasites filteredGreenWhitoutStain config)
93 // Output pictures if debug flag is set.
94 match config.Debug with
96 let dirPath = System.IO.Path.Combine(output
, name
)
97 System.IO.Directory.CreateDirectory dirPath |> ignore
99 let buildFileName postfix
= System.IO.Path.Combine(dirPath, name
+ postfix
)
101 saveMat
(edges * 255.0) (buildFileName " - edges.png")
103 saveImg
parasites.darkStain
(buildFileName " - parasites - dark stain.png")
104 saveImg
parasites.stain
(buildFileName " - parasites - stain.png")
105 saveImg
parasites.infection
(buildFileName " - parasites - infection.png")
107 let imgAllEllipses = img.Copy()
108 drawEllipses
imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.1
109 saveImg
imgAllEllipses (buildFileName " - ellipses - all.png")
111 let imgEllipses = img.Copy()
112 drawEllipses
imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0
113 saveImg
imgEllipses (buildFileName " - ellipses.png")
115 // saveImg (kmediansResults.fg * 255.0) (buildFileName " - foreground.png")
117 let imgCells = img.Copy()
118 drawCells
imgCells false cells
119 saveImg
imgCells (buildFileName " - cells.png")
121 let imgCells' = img.Copy()
122 drawCells imgCells' true cells
123 saveImg
imgCells' (buildFileName " - cells - full.png")
125 let filteredGreenMaxima = gaussianFilter green config.Parameters.preFilterSigma
126 for m in ImgTools.findMaxima filteredGreenMaxima do
127 ImgTools.drawPoints filteredGreenMaxima m 255uy
128 saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
130 saveImg filteredGreen (buildFileName " - filtered.png")
131 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
132 saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
134 (*saveImg parasitesMarker (buildFileName " - parasites (area closing).png")
135 saveImg stainMarker (buildFileName " - stain (area closing).png")*)
137 saveImg green (buildFileName " - green.png")
139 use blue = scaledImg.Item(0)
140 saveImg blue (buildFileName " - blue.png")
142 use red = scaledImg.Item(2)
143 saveImg red (buildFileName " - red.png")