15 RBCPositions : Point list
16 infectedRBCPositions : Point list
20 let doAnalysis (img
: Image<Bgr, byte
>) (config
: Config) : Classifier.Cell list =
22 let imgFloat = img
.Convert<Bgr, float32
>()
23 use scaledImg = if config
.scale
= 1.0 then imgFloat else imgFloat.Resize(config
.scale
, CvEnum.Inter.Area)
31 CvInvoke.Resize(img, m, Size(roundInt (float img.Size.Width * config.scale), roundInt (float img.Size.Height * config.scale)))
34 use green = scaledImg.Item(1)
36 //use green = new Matrix<byte>(scaledImg.Size)
37 //CvInvoke.MixChannels(scaledImg, green, [| 1; 0 |])
39 //let greenMatrix = new Matrix<byte>(green.Height, green.Width, green.DataPointer)
41 //let test = greenMatrix.[10, 10]
43 use filteredGreen = (gaussianFilter
green config
.doGSigma1
) - config
.doGLowFreqPercentageReduction
* (gaussianFilter
green config
.doGSigma2
)
46 new ConvolutionKernelF(array2D
[[ 1.0f; 0.0f; -1.0f ]
48 [ 1.0f; 0.0f; -1.0f ]], Point(0, 0))
50 use xEdges = filteredGreen.Convolution(sobelKernel).Convert<Gray, float>()
51 use yEdges = filteredGreen.Convolution(sobelKernel.Transpose()).Convert<Gray, float>()
53 let xEdgesData = xEdges.Data
54 let yEdgesData = yEdges.Data
55 for r
in 0..xEdges.Rows-1 do
56 xEdgesData.[r
, 0, 0] <- 0.0
57 xEdgesData.[r
, xEdges.Cols-1, 0] <- 0.0
58 yEdgesData.[r
, 0, 0] <- 0.0
59 yEdgesData.[r
, xEdges.Cols-1, 0] <- 0.0
61 for c
in 0..xEdges.Cols-1 do
62 xEdgesData.[0, c
, 0] <- 0.0
63 xEdgesData.[xEdges.Rows-1, c
, 0] <- 0.0
64 yEdgesData.[0, c
, 0] <- 0.0
65 yEdgesData.[xEdges.Rows-1, c
, 0] <- 0.0
67 use magnitudes = new Matrix<float>(xEdges.Size)
68 CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles).
71 let minLocation = ref <| Point()
73 let maxLocation = ref <| Point()
74 magnitudes.MinMax(min, max, minLocation, maxLocation)
76 use magnitudesByte = ((magnitudes / !max) * 255.0).Convert<byte
>() // Otsu from OpenCV only support 'byte'.
77 use edges = new Matrix<byte
>(xEdges.Size)
78 CvInvoke.Threshold(magnitudesByte, edges, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) |> ignore
80 logTime
"Finding edges" (fun() ->
83 logTime
"Removing small connected components" (fun () ->
86 saveMat
(edges * 255.0) "edges.png"
89 let kmediansResults = KMedians.kmedians
filteredGreen 1.0
91 let parasites = ParasitesMarker.find
green filteredGreen kmediansResults config
93 saveImg
parasites.darkStain
"parasites_dark_stain.png"
94 saveImg
parasites.stain
"parasites_stain.png"
95 saveImg
parasites.infection
"parasites_infection.png"
97 let radiusRange = config.scale
* 20.0, config.scale
* 40.0
98 let windowSize = roundInt
(1.6 * (snd
radiusRange))
99 let factorNbPick = 1.5
100 let ellipses = logTime
"Finding ellipses" (fun () ->
101 Ellipse.find
edges xEdges yEdges radiusRange windowSize factorNbPick)
103 drawEllipses
img ellipses (Bgr(0.0, 255.0, 255.0))
104 //saveImg img "ellipses.png"
106 Classifier.findCells
ellipses parasites kmediansResults.fg
110 (*use imageHSV = scaledImage.Convert<Hsv, uint8>()
111 let H, S = match imageHSV.Split() with // Warning: H is from 0 to 179°.
112 | [| H; S; _|] -> H, S
113 | _ -> failwith "unable to split the HSV channels"
115 let hueShiftValue = 175
116 // Modulo operator doesn't exist on matrix thus we have to apply a function to every pixels.
117 let correctedH : Image<Gray, byte> = H.Convert(fun b _ _ ->
118 (255 - int(b) * 255 / 179 + hueShiftValue) % 256 |> byte
121 let correctedS : Image<Gray, byte> = S.Not()
123 let filteredH = correctedH.SmoothMedian(5)
124 let filteredS = correctedS.SmoothMedian(5)*)
126 //let greenChannel = scaledImage.Item(1)
128 //let filteredImage = (gaussianFilter greenChannel config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter greenChannel config.doGSigma2)
130 // let filteredImage = greenChannel.ThresholdAdaptive(Gray(255.), CvEnum.AdaptiveThresholdType.GaussianC, CvEnum.ThresholdType.Binary, 61, Gray(5.0))
131 // let thresholdedImage = filteredImage.CopyBlank()
133 // CvInvoke.Threshold(filteredImage, thresholdedImage, 0., 255., CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.BinaryInv) |> ignore