15 RBCPositions : Point list
16 infectedRBCPositions
: Point list
20 let doAnalysis (img
: Image<Bgr, byte
>) (config
: Config) : Result =
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 let threshold = CvInvoke.Threshold(magnitudesByte, edges, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary)
82 saveMat
(edges * 255.0) "edges.png"
84 let radiusRange = config
.scale
* 20.0, config
.scale
* 40.0
85 let windowSize = roundInt
(1.6 * (snd
radiusRange))
86 let factorNbPick = 1.0;
87 let ellipses = Ellipse.find
edges xEdges yEdges radiusRange windowSize factorNbPick
89 drawEllipse
img (List.head
ellipses) (Bgr(0.0, 255.0, 255.0))
90 saveImg
img "ellipses.png"
92 { RBCPositions = []; infectedRBCPositions
= []; img = img }
96 (*use imageHSV = scaledImage.Convert<Hsv, uint8>()
97 let H, S = match imageHSV.Split() with // Warning: H is from 0 to 179°.
98 | [| H; S; _|] -> H, S
99 | _ -> failwith "unable to split the HSV channels"
101 let hueShiftValue = 175
102 // Modulo operator doesn't exist on matrix thus we have to apply a function to every pixels.
103 let correctedH : Image<Gray, byte> = H.Convert(fun b _ _ ->
104 (255 - int(b) * 255 / 179 + hueShiftValue) % 256 |> byte
107 let correctedS : Image<Gray, byte> = S.Not()
109 let filteredH = correctedH.SmoothMedian(5)
110 let filteredS = correctedS.SmoothMedian(5)*)
112 //let greenChannel = scaledImage.Item(1)
114 //let filteredImage = (gaussianFilter greenChannel config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter greenChannel config.doGSigma2)
116 // let filteredImage = greenChannel.ThresholdAdaptive(Gray(255.), CvEnum.AdaptiveThresholdType.GaussianC, CvEnum.ThresholdType.Binary, 61, Gray(5.0))
117 // let thresholdedImage = filteredImage.CopyBlank()
119 // CvInvoke.Threshold(filteredImage, thresholdedImage, 0., 255., CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.BinaryInv) |> ignore