X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FImgTools%2FEdges.fs;h=63ec80a188cc2a0196f68126d66367cefca610df;hb=b87b35b922551f122228df1fd9c530bbb807935a;hp=b174ee9d060ed08ad4541b3e45112465cbe8a943;hpb=3f8b0d281b3058faf23dbd0363de440bd04c6574;p=master-thesis.git diff --git a/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs b/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs index b174ee9..63ec80a 100644 --- a/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs +++ b/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs @@ -11,19 +11,25 @@ open Const open Histogram open Otsu +// Sensibilities of the hysteresis search. +let sensibilityHigh = 0.1f +let sensibilityLow = 0.0f + /// /// Find edges of an image by using the Canny approach. /// The thresholds are automatically defined with otsu on gradient magnitudes. /// /// -let find (img: Image) : Matrix * Matrix * Matrix = +let find (img : Image) : Matrix * Matrix * Matrix = let w = img.Width let h = img.Height use sobelKernel = - new Matrix(array2D [[ 1.0f; 0.0f; -1.0f ] - [ 2.0f; 0.0f; -2.0f ] - [ 1.0f; 0.0f; -1.0f ]]) + new Matrix( + array2D [[ -1.0f; 0.0f; 1.0f ] + [ -2.0f; 0.0f; 2.0f ] + [ -1.0f; 0.0f; 1.0f ]] + ) let xGradient = new Matrix(img.Size) let yGradient = new Matrix(img.Size) @@ -32,11 +38,9 @@ let find (img: Image) : Matrix * Matrix * Matrix(xGradient.Size) use angles = new Matrix(xGradient.Size) - CvInvoke.CartToPolar(xGradient, yGradient, magnitudes, angles) // Compute the magnitudes and angles. + CvInvoke.CartToPolar(xGradient, yGradient, magnitudes, angles) // Compute the magnitudes and angles. The angles are between 0 and 2 * pi. let thresholdHigh, thresholdLow = - let sensibilityHigh = 0.1f - let sensibilityLow = 0.0f let threshold, _, _ = otsu (histogramMat magnitudes 300) threshold + (sensibilityHigh * threshold), threshold - (sensibilityLow * threshold) @@ -49,46 +53,37 @@ let find (img: Image) : Matrix * Matrix * Matrix 0.f || vy <> 0.f - then + if vx <> 0.f || vy <> 0.f then let angle = anglesData.[i, j] let vx', vy' = abs vx, abs vy let ratio2 = if vx' > vy' then vy' / vx' else vx' / vy' let ratio1 = 1.f - ratio2 - let mNeigbors (sign: int) : float32 = - if angle < PI / 4.f - then ratio1 * magnitudesData.[i, j + sign] + ratio2 * magnitudesData.[i + sign, j + sign] - elif angle < PI / 2.f - then ratio2 * magnitudesData.[i + sign, j + sign] + ratio1 * magnitudesData.[i + sign, j] - elif angle < 3.f * PI / 4.f - then ratio1 * magnitudesData.[i + sign, j] + ratio2 * magnitudesData.[i + sign, j - sign] - elif angle < PI - then ratio2 * magnitudesData.[i + sign, j - sign] + ratio1 * magnitudesData.[i, j - sign] - elif angle < 5.f * PI / 4.f - then ratio1 * magnitudesData.[i, j - sign] + ratio2 * magnitudesData.[i - sign, j - sign] - elif angle < 3.f * PI / 2.f - then ratio2 * magnitudesData.[i - sign, j - sign] + ratio1 * magnitudesData.[i - sign, j] - elif angle < 7.f * PI / 4.f - then ratio1 * magnitudesData.[i - sign, j] + ratio2 * magnitudesData.[i - sign, j + sign] - else ratio2 * magnitudesData.[i - sign, j + sign] + ratio1 * magnitudesData.[i, j + sign] + let mNeigbors (sign : int) : float32 = + if angle < PI / 4.f then + ratio1 * magnitudesData.[i, j + sign] + ratio2 * magnitudesData.[i + sign, j + sign] + elif angle < PI / 2.f then + ratio2 * magnitudesData.[i + sign, j + sign] + ratio1 * magnitudesData.[i + sign, j] + elif angle < 3.f * PI / 4.f then + ratio1 * magnitudesData.[i + sign, j] + ratio2 * magnitudesData.[i + sign, j - sign] + elif angle < PI then + ratio2 * magnitudesData.[i + sign, j - sign] + ratio1 * magnitudesData.[i, j - sign] + elif angle < 5.f * PI / 4.f then + ratio1 * magnitudesData.[i, j - sign] + ratio2 * magnitudesData.[i - sign, j - sign] + elif angle < 3.f * PI / 2.f then + ratio2 * magnitudesData.[i - sign, j - sign] + ratio1 * magnitudesData.[i - sign, j] + elif angle < 7.f * PI / 4.f then + ratio1 * magnitudesData.[i - sign, j] + ratio2 * magnitudesData.[i - sign, j + sign] + else + ratio2 * magnitudesData.[i - sign, j + sign] + ratio1 * magnitudesData.[i, j + sign] let m = magnitudesData.[i, j] - if m >= thresholdLow && m > mNeigbors 1 && m > mNeigbors -1 - then + if m >= thresholdLow && m > mNeigbors 1 && m > mNeigbors -1 then nmsData.[i, j] <- 1uy // suppressMConnections nms // It's not helpful for the rest of the process (ellipse detection). @@ -98,23 +93,20 @@ let find (img: Image) : Matrix * Matrix * Matrix() - for i in 0 .. h - 1 do - for j in 0 .. w - 1 do - if nmsData.[i, j] = 1uy && magnitudesData.[i, j] >= thresholdHigh - then + for i = 0 to h - 1 do + for j = 0 to w - 1 do + if nmsData.[i, j] = 1uy && magnitudesData.[i, j] >= thresholdHigh then nmsData.[i, j] <- 0uy toVisit.Push(Point(j, i)) while toVisit.Count > 0 do let p = toVisit.Pop() edgesData.[p.Y, p.X] <- 1uy - for i' in -1 .. 1 do - for j' in -1 .. 1 do - if i' <> 0 || j' <> 0 - then + for i' = -1 to 1 do + for j' = -1 to 1 do + if i' <> 0 || j' <> 0 then let ni = p.Y + i' let nj = p.X + j' - if ni >= 0 && ni < h && nj >= 0 && nj < w && nmsData.[ni, nj] = 1uy - then + if ni >= 0 && ni < h && nj >= 0 && nj < w && nmsData.[ni, nj] = 1uy then nmsData.[ni, nj] <- 0uy toVisit.Push(Point(nj, ni))