e3a9950290f2d5af14a91ea9ba439adcb06c52fe
[master-thesis.git] / Parasitemia / ParasitemiaCore / ImgTools / Histogram.fs
1 module ParasitemiaCore.Histogram
2
3 open System
4 open System.Drawing
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 type Histogram = {
10 data: int[]
11 total: int // Number of elements.
12 sum: int // Sum of all intensity.
13 min: float32
14 max: float32 }
15
16 let histogramImg (img: Image<Gray, float32>) (nbSamples: int) : Histogram =
17 let imgData = img.Data
18
19 let min, max =
20 let min = ref [| 0.0 |]
21 let minLocation = ref <| [| Point() |]
22 let max = ref [| 0.0 |]
23 let maxLocation = ref <| [| Point() |]
24 img.MinMax(min, max, minLocation, maxLocation)
25 float32 (!min).[0], float32 (!max).[0]
26
27 let inline bin (x: float32) : int =
28 let p = int ((x - min) / (max - min) * float32 nbSamples)
29 if p >= nbSamples then nbSamples - 1 else p
30
31 let data = Array.zeroCreate nbSamples
32
33 for i = 0 to img.Height - 1 do
34 for j = 0 to img.Width - 1 do
35 let p = bin imgData.[i, j, 0]
36 data.[p] <- data.[p] + 1
37
38 { data = data; total = img.Height * img.Width; sum = Array.sum data; min = min; max = max }
39
40 let histogramMat (mat: Matrix<float32>) (nbSamples: int) : Histogram =
41 let matData = mat.Data
42
43 let min, max =
44 let min = ref 0.0
45 let minLocation = ref <| Point()
46 let max = ref 0.0
47 let maxLocation = ref <| Point()
48 mat.MinMax(min, max, minLocation, maxLocation)
49 float32 !min, float32 !max
50
51 let inline bin (x: float32) : int =
52 let p = int ((x - min) / (max - min) * float32 nbSamples)
53 if p >= nbSamples then nbSamples - 1 else p
54
55 let data = Array.zeroCreate nbSamples
56
57 for i = 0 to mat.Height - 1 do
58 for j = 0 to mat.Width - 1 do
59 let p = bin matData.[i, j]
60 data.[p] <- data.[p] + 1
61
62 { data = data; total = mat.Height * mat.Width; sum = Array.sum data; min = min; max = max }
63
64 let histogram (values: float32 seq) (nbSamples: int) : Histogram =
65 let mutable min = Single.MaxValue
66 let mutable max = Single.MinValue
67 let mutable n = 0
68
69 for v in values do
70 n <- n + 1
71 if v < min then min <- v
72 if v > max then max <- v
73
74 let inline bin (x: float32) : int =
75 let p = int ((x - min) / (max - min) * float32 nbSamples)
76 if p >= nbSamples then nbSamples - 1 else p
77
78 let data = Array.zeroCreate nbSamples
79
80 for v in values do
81 let p = bin v
82 data.[p] <- data.[p] + 1
83
84 { data = data; total = n; sum = Array.sum data; min = min; max = max }