Project the colors to have the best contrast for RBCs and parasites analyze.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ImgTools.fs
index 4be893f..51b0c19 100644 (file)
@@ -13,6 +13,40 @@ open Const
 open Types
 open Utils
 
+let normalize (img: Image<Gray, float32>) (upperLimit: float) : Image<Gray, float32> =
+    let min = ref [| 0.0 |]
+    let minLocation = ref <| [| Point() |]
+    let max = ref [| 0.0 |]
+    let maxLocation = ref <| [| Point() |]
+    img.MinMax(min, max, minLocation, maxLocation)
+    let normalized = (img - (!min).[0]) / ((!max).[0] - (!min).[0])
+    if upperLimit = 1.0
+    then normalized
+    else upperLimit * normalized
+
+let mergeChannels (img: Image<Bgr, float32>) (rgbWeights: float * float * float) : Image<Gray, float32> =
+    match rgbWeights with
+    | 1., 0., 0. -> img.[2]
+    | 0., 1., 0. -> img.[1]
+    | 0., 0., 1. -> img.[0]
+    | redFactor, greenFactor, blueFactor ->
+        let result = new Image<Gray, float32>(img.Size)
+        CvInvoke.AddWeighted(result, 1., img.[2], redFactor, 0., result)
+        CvInvoke.AddWeighted(result, 1., img.[1], greenFactor, 0., result)
+        CvInvoke.AddWeighted(result, 1., img.[0], blueFactor, 0., result)
+        result
+
+let mergeChannelsWithProjection (img: Image<Bgr, float32>) (v1r: float32, v1g: float32, v1b: float32) (v2r: float32, v2g: float32, v2b: float32) (upperLimit: float)  : Image<Gray, float32> =
+    let vr, vg, vb = v2r - v1r, v2g - v1g, v2b - v1b
+    let vMagnitude = sqrt (vr ** 2.f + vg ** 2.f + vb ** 2.f)
+    let project (r: float32) (g: float32) (b: float32) = ((r - v1r) * vr + (g - v1g) * vg + (b - v1b) * vb) / vMagnitude
+    let result = new Image<Gray, float32>(img.Size)
+    // TODO: Essayer en bindant Data pour gagner du temps
+    for i in 0 .. img.Height - 1 do
+        for j in 0 .. img.Width - 1 do
+            result.Data.[i, j, 0] <- project img.Data.[i, j, 2] img.Data.[i, j, 1] img.Data.[i, j, 0]
+    normalize result upperLimit
+
 // Normalize image values between 0uy and 255uy.
 let normalizeAndConvert (img: Image<Gray, 'TDepth>) : Image<Gray, byte> =
     let min = ref [| 0.0 |]
@@ -107,7 +141,7 @@ let otsu (hist: Histogram) : float32 * float32 * float32 =
     let mutable wB = 0
     let mutable maximum = 0.0
     let mutable level = 0
-    let sum = hist.data |> Array.mapi (fun i v -> i * v) |> Array.sum |> float
+    let sum = hist.data |> Array.mapi (fun i v -> i * v |> float) |> Array.sum
 
     for i in 0 .. hist.data.Length - 1 do
         wB <- wB + hist.data.[i]