First commit of the f# source code.
[master-thesis.git] / Parasitemia / Parasitemia / ImageAnalysis.fs
1 module ImageAnalysis
2
3 open System
4 open System.Drawing
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 open Utils
10 open ImgTools
11 open Config
12 open Types
13
14 type Result = {
15 RBCPositions : Point list
16 infectedRBCPositions : Point list
17 img: Image<Bgr, byte>
18 }
19
20 let doAnalysis (img: Image<Bgr, byte>) (config: Config) : Result =
21
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)
24
25 (*use scaledImg =
26 if config.scale = 1.0
27 then
28 img
29 else
30 let m = new Mat()
31 CvInvoke.Resize(img, m, Size(roundInt (float img.Size.Width * config.scale), roundInt (float img.Size.Height * config.scale)))
32 m*)
33
34 use green = scaledImg.Item(1)
35
36 //use green = new Matrix<byte>(scaledImg.Size)
37 //CvInvoke.MixChannels(scaledImg, green, [| 1; 0 |])
38
39 //let greenMatrix = new Matrix<byte>(green.Height, green.Width, green.DataPointer)
40
41 //let test = greenMatrix.[10, 10]
42
43 use filteredGreen = (gaussianFilter green config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter green config.doGSigma2)
44
45 use sobelKernel =
46 new ConvolutionKernelF(array2D [[ 1.0f; 0.0f; -1.0f ]
47 [ 2.0f; 0.0f; -2.0f ]
48 [ 1.0f; 0.0f; -1.0f ]], Point(0, 0))
49
50 use xEdges = filteredGreen.Convolution(sobelKernel).Convert<Gray, float>()
51 use yEdges = filteredGreen.Convolution(sobelKernel.Transpose()).Convert<Gray, float>()
52
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
60
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
66
67 use magnitudes = new Matrix<float>(xEdges.Size)
68 CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles).
69
70 let min = ref 0.0
71 let minLocation = ref <| Point()
72 let max = ref 0.0
73 let maxLocation = ref <| Point()
74 magnitudes.MinMax(min, max, minLocation, maxLocation)
75
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)
79 thin edges
80 removeArea edges 12
81
82 saveMat (edges * 255.0) "edges.png"
83
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
88
89 drawEllipse img (List.head ellipses) (Bgr(0.0, 255.0, 255.0))
90 saveImg img "ellipses.png"
91
92 { RBCPositions = []; infectedRBCPositions = []; img = img }
93
94 //
95
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"
100
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
105 )
106
107 let correctedS : Image<Gray, byte> = S.Not()
108
109 let filteredH = correctedH.SmoothMedian(5)
110 let filteredS = correctedS.SmoothMedian(5)*)
111
112 //let greenChannel = scaledImage.Item(1)
113
114 //let filteredImage = (gaussianFilter greenChannel config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter greenChannel config.doGSigma2)
115
116 // let filteredImage = greenChannel.ThresholdAdaptive(Gray(255.), CvEnum.AdaptiveThresholdType.GaussianC, CvEnum.ThresholdType.Binary, 61, Gray(5.0))
117 // let thresholdedImage = filteredImage.CopyBlank()
118
119 // CvInvoke.Threshold(filteredImage, thresholdedImage, 0., 255., CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.BinaryInv) |> ignore
120
121 // filteredImage <|