F #, morfologi gambar biner

pengantar


Seperti yang diterapkan pada gambar biner, morfologi digunakan untuk menggambarkan sifat-sifat gambar ini. Dan operasi morfologi matematika digunakan untuk transformasi set. Dalam hal ini, mereka mudah digunakan untuk memproses gambar biner dengan analisis selanjutnya.


Artikel ini akan membahas operasi dasar morfologi matematika (biner) dalam konteks F #. Dan juga, di akhir artikel saya akan memberikan contoh analisis hasil, yang dapat digunakan dalam algoritma visi mesin.


Artikel ini akan menggunakan materi yang dibahas dalam artikel saya sebelumnya tentang analisis algoritma pelabelan untuk komponen terkait. Anda dapat membiasakan diri dengan artikel di sini: F #, sebuah algoritma untuk menandai komponen yang terhubung dari suatu gambar .


Operasi dasar morfologi biner


Untuk melakukan operasi morfologi biner, kita perlu


  • Gambar biner asli (B)
  • Elemen struktural)
    Elemen penataan adalah deskripsi area dengan beberapa bentuk. Area ini dapat memiliki ukuran dan bentuk apa saja, yang dapat direpresentasikan sebagai gambar biner

Di bawah ini adalah beberapa contoh elemen penataan dalam bentuk persegi panjang, disk dan cincin.


Box(3,5)=βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—


Disk(5,5)=0βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—0


Ring(5,5)=0βˆ—βˆ—βˆ—0βˆ—000βˆ—βˆ—000βˆ—βˆ—000βˆ—0βˆ—βˆ—βˆ—0


Elemen - elemen penataan adalah topeng yang menentukan area gambar di mana operasi morfologi biner akan dilakukan.


Unsur penataan juga memiliki titik asal . Biasanya terletak di tengah topeng, tetapi bisa di sembarang tempat. Asal elemen penataan harus bertepatan dengan piksel saat ini dari gambar biner untuk membuat transformasi di atasnya.


Pertimbangkan operasi dasar morfologi biner.


Perpanjangan citra biner B oleh elemen penataan S dilambangkan dengan B βŠ• S dan didefinisikan sebagai berikut

BβŠ•S=βˆͺb∈BSb


. S . , , .


.


Figure 1: B


00000000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—000000000000


Figure 2: S


βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—


Figure 3: B βŠ• S


βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—000


F#, .


let (>.) value checkValue = if value > checkValue then checkValue else value
let (<.) value checkValue = if value < checkValue then checkValue else value

let subArrayOfMaks array2d mask (centerX, centerY) (maskCenterX, maskCenterY) = 
        let (rows, cols) = Matrix.sizes {values = array2d}
        let (maskRows, maskCols) = Matrix.sizes {values = mask}

        let x1 = centerX - maskCenterX
        let y1 = centerY - maskCenterY
        let x2 = x1 + maskCols - 1
        let y2 = y1 + maskRows - 1

        (x1 <. 0, x2 >. (cols - 1), y1 <. 0, y2 >. (rows - 1))

let upbuilding array2d mask (maskCenterX, maskCenterY) =

        let sub source mask (x, y) (maskCenterX, maskCenterY) =
            let (x1, x2, y1, y2) = subArrayOfMaks source mask (x, y) (maskCenterX, maskCenterY)
            source.[y1..y2, x1..x2] <- mask

        let copy = (Matrix.cloneO {values = array2d}).values
        array2d |> Array2D.iteri (fun y x v -> if v = 1 then sub copy mask (x, y) (maskCenterX, maskCenterY))

        copy

upbuilding


let upbuilding array2d mask (maskCenterX, maskCenterY) =

, .


>. <.


let (>.) value checkValue = if value > checkValue then checkValue else value
let (<.) value checkValue = if value < checkValue then checkValue else value


value β€”
checkValue β€” ( )


value ( ) checkValue, checkValue, β€” value. nil Swift (??), , ( ).
,


2 <. 3 //  3


3 <. 2 //  3

, .


subArrayOfMaks


let subArrayOfMaks array2d mask (centerX, centerY) (maskCenterX, maskCenterY) = 

.


array2d β€” ( )
mask β€” ()
(centerX, centerY) β€” ,
(maskCenterX, maskCenterY) β€” . , . , .


upbuilding


array2d β€”
mask β€”
(maskCenterX, maskCenterY) β€”


let upbuilding array2d mask (maskCenterX, maskCenterY) =

        //      ,        
        let sub source mask (x, y) (maskCenterX, maskCenterY) =
            //    
            let (x1, x2, y1, y2) = subArrayOfMaks source mask (x, y) (maskCenterX, maskCenterY)
            //    
            source.[y1..y2, x1..x2] <- mask

        //    ,  
        let copy = (Matrix.cloneO {values = array2d}).values
        //      
        array2d |> Array2D.iteri (fun y x v -> if v = 1 then sub copy mask (x, y) (maskCenterX, maskCenterY))

        //    
        copy

, .


B S B βŠ– S

BβŠ–S=b|b+s∈Bβˆ€s∈S


"" . , .


( , Figure 1).


Figure 4: B βŠ– S


00000000000000000000βˆ—βˆ—000000βˆ—βˆ—000000βˆ—βˆ—00000000000000000000000000


F#. , F#


//    ,     array2d   mask?
let contains array2d mask =
        if Matrix.isEquallySized (Matrix.ofArray2D array2d) (Matrix.ofArray2D mask) then
            not (array2d
                 |> Array2D.mapi (fun x y v -> if mask.[x, y ] = 0 then 1
                                               elif mask.[x, y] = v then 1
                                               else 0)
                 |> Seq.cast<int>
                 |> Seq.contains 0)
        else false

// ,   
let erosion array2d mask (maskCenterX, maskCenterY) =

        let sub source (dest: int [,]) mask (x, y) (maskCenterX, maskCenterY) =
            let (x1, x2, y1, y2) = subArrayOfMaks source mask (x, y) (maskCenterX, maskCenterY)
            let subArray2d = source.[y1..y2, x1..x2]

            if contains subArray2d mask then
                dest.[y, x] <- 1

        let copy = (Matrix.cloneO {values = array2d}).values
        array2d |> Array2D.iteri (fun y x v -> if v = 1 then sub array2d copy mask (x, y) (maskCenterX, maskCenterY))

        copy

, . , , .


.


B S B β€’ S

Bβ€’S=(BβŠ•S)βŠ–S


F#


let closure array2d mask (centerX, centerY) = 
        let step1 = upbuilding array2d mask (centerX, centerY)
        let step2 = erosion step1 mask (centerX, centerY)

        step2


Figure 5: B β€’ S


000000000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—000000000000


B S B β—¦ S

Bβ—¦S=(BβŠ–S)βŠ•S


F#


let opening array2d mask (centerX, centerY) = 
        let step1 = erosion array2d mask (centerX, centerY)
        let step2 = upbuilding step1 mask (centerX, centerY)

        step2

Figure 6: B β—¦ S


00000000000βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—00000000000000000



, , , . , . , .


. , . , .


Figure 7:


000000000000βˆ—00000000βˆ—βˆ—βˆ—000βˆ—0000000000βˆ—00000βˆ—000000000βˆ—βˆ—00βˆ—0000βˆ—βˆ—βˆ—βˆ—0βˆ—0000βˆ—0000βˆ—00000000βˆ—βˆ—00000000000


Figure 8:


βˆ—βˆ—βˆ—


, , ( , ). ,


Figure 9:


00000000000000000000000000000000000000000000βˆ—000000000βˆ—βˆ—00βˆ—0000βˆ—βˆ—βˆ—βˆ—0βˆ—0000βˆ—0000βˆ—00000000βˆ—βˆ—00000000000


F#


//    
let origin = array2D [[0;0;0;0;0;0;0;0;0;0]
                      [0;0;1;0;0;0;0;0;0;0]
                      [0;1;1;1;0;0;0;1;0;0]
                      [0;0;0;0;0;0;0;0;1;0]
                      [0;0;0;0;1;0;0;0;0;0]
                      [0;0;0;0;1;1;0;0;1;0]
                      [0;0;0;1;1;1;1;0;1;0]
                      [0;0;0;1;0;0;0;0;1;0]
                      [0;0;0;0;0;0;0;1;1;0]
                      [0;0;0;0;0;0;0;0;0;0]]

//  
let mask = array2D [[1]
                    [1]
                    [1]]

//      
let marked = Algorithms.markingOfConnectedComponents origin
//  
let erosion = Algorithms.erosion origin mask (0, 1)

printfn "origin = \n %A" origin
printfn "marked = \n %A" marked
printfn "erosion =\n %A" erosion

//        .       ()
let set = erosion |> Seq.cast<int> |> Seq.filter ((<>)0) |> Set.ofSeq

//     ,     
let copy = (Matrix.cloneO {values = marked}).values

//         
erosion
|> Array2D.iteri (fun row col v -> if v = 1 then
                            //     
                            let label = marked.[row, col]
                            if not (set.Contains label) then
                                //       ,       
                                marked |> Array2D.iteri (fun row1 col1 v1 -> if v1 = label then copy.[row1, col1] <- 1)
                         )

printfn "copy =\n %A" copy


origin = 
 [[0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 1; 0; 0; 0; 0; 0; 0; 0]
 [0; 1; 1; 1; 0; 0; 0; 1; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 1; 0]
 [0; 0; 0; 0; 1; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 1; 1; 0; 0; 1; 0]
 [0; 0; 0; 1; 1; 1; 1; 0; 1; 0]
 [0; 0; 0; 1; 0; 0; 0; 0; 1; 0]
 [0; 0; 0; 0; 0; 0; 0; 1; 1; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]]
marked = 
 [[0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 1; 0; 0; 0; 0; 0; 0; 0]
 [0; 1; 1; 1; 0; 0; 0; 3; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 4; 0]
 [0; 0; 0; 0; 5; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 5; 5; 0; 0; 6; 0]
 [0; 0; 0; 5; 5; 5; 5; 0; 6; 0]
 [0; 0; 0; 5; 0; 0; 0; 0; 6; 0]
 [0; 0; 0; 0; 0; 0; 0; 6; 6; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]]
erosion =
 [[0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 1; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 1; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 1; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]]
copy =
 [[0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 1; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 1; 1; 0; 0; 1; 0]
 [0; 0; 0; 1; 1; 1; 1; 0; 1; 0]
 [0; 0; 0; 1; 0; 0; 0; 0; 1; 0]
 [0; 0; 0; 0; 0; 0; 0; 1; 1; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]]

, , ,


F#


Figure 10:


A=0βˆ—00βˆ—βˆ—0βˆ—0,B=000βˆ—βˆ—βˆ—000


A B

AβˆͺB


Figure 11:


0βˆ—0βˆ—βˆ—βˆ—0βˆ—0


A B

A∩B


Figure 12:


0000βˆ—βˆ—000


A

Ac


Figure 13:


βˆ—0βˆ—βˆ—00βˆ—0βˆ—


A B

A/B


Figure 13:


0βˆ—00000βˆ—0


F#


// 
let union array2d1 array2d2 = 
        if Matrix.isEquallyArraySized array2d1 array2d2 then
            array2d1 |> Array2D.mapi (fun x y v -> v ||| array2d2.[x, y])
        else failwith "array2d1 is not equal to array2d2"

// 
let intersection array2d1 array2d2 = 
        if Matrix.isEquallyArraySized array2d1 array2d2 then
            array2d1 |> Array2D.mapi (fun x y v -> v &&& array2d2.[x, y])
        else failwith "array2d1 is not equal to array2d2"

//  (    )
let inverse array2d =
        array2d |> Array2D.mapi (fun _ _ v -> v ^^^ 1)

let complement array2d = 
        inverse array2d

// 
let difference array2d1 array2d2 = 
        if Matrix.isEquallyArraySized array2d1 array2d2 then
            array2d1 |> Array2D.mapi (fun x y v -> if v <> array2d2.[x, y] then v else 0)
        else failwith "array2d1 is not equal to array2d2"


, . , . .


Figure 14:


000000000000000000βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—βˆ—βˆ—βˆ—000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—10βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—00βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—00000000


Figure 15:


0βˆ—0βˆ—βˆ—βˆ—0βˆ—0


F#


let borderAllocation array2d mask (maskCenterX, maskCenterY) = 
        let e = erosion array2d mask (maskCenterX, maskCenterY)
        let border = difference array2d e
        border


000000000000000000βˆ—βˆ—βˆ—βˆ—000βˆ—000βˆ—000βˆ—0000βˆ—10βˆ—00000βˆ—00βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—00000000



. , . , . , , . , . , , .


, , : ,


0000000000000000000000000000000000000000000000000000000000βˆ—βˆ—βˆ—βˆ—00βˆ—βˆ—βˆ—βˆ—00000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—00000βˆ—βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—000000βˆ—βˆ—βˆ—βˆ—βˆ—0βˆ—βˆ—βˆ—βˆ—000000000000βˆ—βˆ—βˆ—βˆ—000000000000βˆ—βˆ—βˆ—βˆ—00βˆ—βˆ—βˆ—0000000βˆ—βˆ—βˆ—βˆ—00βˆ—βˆ—βˆ—0000000βˆ—βˆ—βˆ—βˆ—00βˆ—βˆ—βˆ—0000000βˆ—βˆ—βˆ—βˆ—000000000000


F#


//     
    let multyBinImage = array2D [[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]
                                 [0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]
                                 [0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]
                                 [0;0;0;0;0;0;0;0;0;0;1;1;1;1;0;0]
                                 [1;1;1;1;0;0;0;0;0;1;1;1;1;1;1;0]
                                 [1;1;1;1;0;0;0;0;1;1;1;1;1;1;1;1]
                                 [1;1;1;1;0;0;0;0;1;1;1;1;1;1;1;1]
                                 [1;1;1;1;0;0;0;0;1;1;1;1;1;1;1;1]
                                 [1;1;1;1;0;0;0;0;0;1;1;1;1;1;1;0]
                                 [1;1;1;1;0;0;0;0;0;0;1;1;1;1;0;0]
                                 [1;1;1;1;0;0;0;0;0;0;0;0;0;0;0;0]
                                 [1;1;1;1;0;0;0;0;0;0;0;0;0;0;0;0]
                                 [1;1;1;1;0;0;1;1;1;0;0;0;0;0;0;0]
                                 [1;1;1;1;0;0;1;1;1;0;0;0;0;0;0;0]
                                 [1;1;1;1;0;0;1;1;1;0;0;0;0;0;0;0]
                                 [1;1;1;1;0;0;0;0;0;0;0;0;0;0;0;0]]

,


//   
let markedMultyImages = Algorithms.markingOfConnectedComponents mulyBinImage
printfn "marked = \n %A" markedMultyImages


marked = 
 [[0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 1; 0; 0]
 [2; 2; 2; 2; 0; 0; 0; 0; 0; 1; 1; 1; 1; 1; 1; 0]
 [2; 2; 2; 2; 0; 0; 0; 0; 1; 1; 1; 1; 1; 1; 1; 1]
 [2; 2; 2; 2; 0; 0; 0; 0; 1; 1; 1; 1; 1; 1; 1; 1]
 [2; 2; 2; 2; 0; 0; 0; 0; 1; 1; 1; 1; 1; 1; 1; 1]
 [2; 2; 2; 2; 0; 0; 0; 0; 0; 1; 1; 1; 1; 1; 1; 0]
 [2; 2; 2; 2; 0; 0; 0; 0; 0; 0; 1; 1; 1; 1; 0; 0]
 [2; 2; 2; 2; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [2; 2; 2; 2; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
 [2; 2; 2; 2; 0; 0; 5; 5; 5; 0; 0; 0; 0; 0; 0; 0]
 [2; 2; 2; 2; 0; 0; 5; 5; 5; 0; 0; 0; 0; 0; 0; 0]
 [2; 2; 2; 2; 0; 0; 5; 5; 5; 0; 0; 0; 0; 0; 0; 0]
 [2; 2; 2; 2; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]]

(1, 2, 5)


, β€” , , .


//   

// groups key - , value -    ,   
let groups = Dictionary<int, (int*int) list>()
markedMultyImages
|> Array2D.iteri (fun row col v -> if v <> 0 then
                                       if not (groups.ContainsKey(v)) then
                                            groups.Add(v, [row, col])
                                       else
                                            groups.[v] <- (row, col)::groups.[v])

printfn "groups = \n %A" groups


groups = 
 seq
  [[1, [(9, 13); (9, 12); (9, 11); ... ]];
   [2, [(15, 3); (15, 2); (15, 1); ... ]];
   [5, [(14, 8); (14, 7); (14, 6); ... ]]]


  1. ( )
  2. ( )
  3. ( )

let mapAreas = groups
               |> Seq.map (|KeyValue|)
               |> Map.ofSeq

//   
let areas = mapAreas
            |> Map.map (fun k v -> v |> List.length)
printfn "areas =\n %A" areas

areas β€” , β€” , β€”


areas =
 map [(1, 44); (2, 48); (5, 9)]

, , 1 44 , 2 β€” 48 , 5 β€” 9 .


, , , 1 , . , . , β€” ,


//     
    let subarrays = mapAreas
                    |> Map.map (fun k v ->
                                    let minRow = v |> List.map (fst) |> List.min
                                    let maxRow = v |> List.map (fst) |> List.max
                                    let minCol = v |> List.map (snd) |> List.min
                                    let maxCol = v |> List.map (snd) |> List.max

                                    let dimRow = maxRow - minRow + 2 + 1 // add two zeros on left and right sides
                                    let dimCol = maxCol - minCol + 2 + 1

                                    let array2d = Array2D.create dimRow dimCol 0
                                    v |> List.iter (fun elem -> (array2d.[(fst elem) - minRow + 1, (snd elem) - minCol + 1] <- multyBinImage.[fst elem, snd elem]))

                                    array2d
                               )

    printfn "subarrays =\n %A" subarrays


subarrays =
 map
  [(1, [[0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
        [0; 0; 0; 1; 1; 1; 1; 0; 0; 0]
        [0; 0; 1; 1; 1; 1; 1; 1; 0; 0]
        [0; 1; 1; 1; 1; 1; 1; 1; 1; 0]
        [0; 1; 1; 1; 1; 1; 1; 1; 1; 0]
        [0; 1; 1; 1; 1; 1; 1; 1; 1; 0]
        [0; 0; 1; 1; 1; 1; 1; 1; 0; 0]
        [0; 0; 0; 1; 1; 1; 1; 0; 0; 0]
        [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]]);

   (2, [[0; 0; 0; 0; 0; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 1; 1; 1; 1; 0]
         [0; 0; 0; 0; 0; 0]]);

   (5, [[0; 0; 0; 0; 0]
        [0; 1; 1; 1; 0]
        [0; 1; 1; 1; 0]
        [0; 1; 1; 1; 0]
        [0; 0; 0; 0; 0]])]

. β€” .


//       
    let erosionMaks = array2D [[0;1;0]
                               [1;1;1]
                               [0;1;0]]

    let borders = subarrays
                  |> Map.map (fun label array2d -> Algorithms.borderAllocation array2d erosionMaks (1, 1))
    let bordersLength = borders |> Map.map (fun label elem -> elem |> Seq.cast<int> |> Seq.filter ((<>)0) |> Seq.length)

    printfn "borders =\n %A" borders
    printfn "borders length =\n %A" bordersLength


borders =
 map
  [(1, [[0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
        [0; 0; 0; 1; 1; 1; 1; 0; 0; 0]
        [0; 0; 1; 0; 0; 0; 0; 1; 0; 0]
        [0; 1; 0; 0; 0; 0; 0; 0; 1; 0]
        [0; 1; 0; 0; 0; 0; 0; 0; 1; 0]
        [0; 1; 0; 0; 0; 0; 0; 0; 1; 0]
        [0; 0; 1; 0; 0; 0; 0; 1; 0; 0]
        [0; 0; 0; 1; 1; 1; 1; 0; 0; 0]
        [0; 0; 0; 0; 0; 0; 0; 0; 0; 0]]);

(2, [[0; 0; 0; 0; 0; 0]
      [0; 1; 1; 1; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 0; 0; 1; 0]
      [0; 1; 1; 1; 1; 0]
      [0; 0; 0; 0; 0; 0]]);

   (5, [[0; 0; 0; 0; 0]
        [0; 1; 1; 1; 0]
        [0; 1; 0; 1; 0]
        [0; 1; 1; 1; 0]
        [0; 0; 0; 0; 0]])]

borders length =
 map [(1, 18); (2, 28); (5, 8)]

1 18 , 2 β€” 28 5 β€” 8 .


.


//    
    let centers = borders
                   |> Map.map (fun k v ->
                                 let centerRow = v |> Array2D.mapi (fun r _ _ -> float r) |> Seq.cast<float> |> Seq.average // list.average requires float
                                 let centerCol = v |> Array2D.mapi (fun _ c _ -> float c) |> Seq.cast<float> |> Seq.average
                                 (int centerCol, int centerRow)
                              )
    printfn "centers =\n %A" centers


centers =
 map [(1, (4, 4)); (2, (2, 6)); (5, (2, 2))]

.


C=μRσR


ΞΌR β€”
ΟƒR β€”



ΞΌR=1Kβˆ‘k=1Kβˆ’1||(rk,ck)βˆ’(rΒ―,cΒ―)||


(r_k, c_k) β€”
(r, c) β€”



//    
    let diff point1 point2 = 
        let square x = x * x
        let s1 = square ((fst point1) - (fst point2))
        let s2 = square ((snd point1) - (snd point2))
        sqrt(double (s1 + s2))

ΞΌR .


//  ,  key - , value -   (double)
let mr = borders
         |> Map.map (fun label v ->
                        let aver = v
                                    |> Array2D.mapi (fun r c v -> diff (r, c) centers.[label])
                                    |> Seq.cast<double>
                                    |> Seq.sum
                        aver / (double v.Length)
                 )

ΟƒR


ΟƒR=(1Kβˆ‘k=1Kβˆ’1[||(rk,ck)βˆ’(rΒ―,cΒ―)||βˆ’ΞΌR]2)1/2


let qr = borders
     |> Map.map (fun label v ->
                    let aver = v
                            |> Array2D.mapi (fun r c v ->
                                                let t1 = diff (r, c) centers.[label]
                                                let t2 = (t1 - mr.[label]) * (t1 - mr.[label])
                                                t2
                                            )
                            |> Seq.cast<double>
                            |> Seq.sum
                            |> sqrt
                aver / (double v.Length)
         )

//  .   ,   
    let roundness = mr |> Map.map (fun label v -> v / qr.[label])
    printfn "roundness =\n %A" roundness


roundness =
 map [(1, 25.06054796); (2, 20.37134197); (5, 13.43281947)]

, 1 . .



() () F#. . , . , F# , . , , F# . , . .


Algoritme itu sendiri dibahas dalam artikel dapat ditemukan di sini. Di sana, di repositori tes, ada implementasi dari bagian terakhir artikel, di mana objek paling melingkar di dalam gambar dicari.
Algoritma morfologi biner


All Articles