1

I am trying to use box-counting in R to calculate the fractal dimension of an object represented by a binary matrix (a 512 x 512 matrix entirely populated by 1s and 0s). Naturally, I found this question, but the question is not similar to mine and the answer is lackluster. I'm not myself familiar with the mathematical details of box-counting, but am familiar with its utility in the calculation of fractal dimension. That being said, much of the documentation behind some functions in R claiming to calculate the box-counting dimension are explained in ways that are a bit over my head (from what I've read online, I'm not the only one). I'm curious as to whether or not anyone on this site has had experience with these programs, and if they could share their wisdom?

Edit

The data I am trying to analyze is in the form of a binary matrix, with the only nonzero entries being 1's. An example of such a matrix is as follows:

A = matrix(c(0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1),byrow=T,nrow=4,ncol=4)

I'm only inquiring about square matrices. Although this matrix is small, the matrices I am dealing with are much larger (512x512 as stated earlier). The ideal function would take as an input a matrix, as one above, and output the box-counting dimension of the figure contained in said matrix.

Derek Adams
  • 162
  • 7
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Right now your question seems a bit too broad to be answerable on Stack Overflow. This is a Q&A site, not really a discussion forum. – MrFlick Oct 16 '19 at 16:04
  • I edited the question. Does this help? – Derek Adams Oct 16 '19 at 16:38
  • [Here's a similar question](https://stackoverflow.com/questions/35050990) with. an answer that (I hope) is not so lackluster. While that problem works with Python/Numpy, it might still prove useful. – Mark McClure Oct 16 '19 at 17:25
  • @MarkMcClure I had not seen this example, yes it is very useful - thank you for the recommendation! – Derek Adams Oct 16 '19 at 17:29

1 Answers1

2

This code should work:

# create a dataset to calculate de Hausdorff-Besicovitch dimension
mat <- matrix(runif(512*512),nrow = 512,ncol = 512)

mat[mat<=0.5] <- 0
mat[mat>0.5] <- 1

cant <- sum(mat)

fragment <- rep(2,10)**(0:9)
Table <- data.frame(Delta = rep(512,10)/(fragment ), N = fragment**2)
Table$LogDelta <- log(Table$Delta)

for(i in 2:10){
  delta_aux <- Table$Delta[i]

  for(j in 1:fragment [i]){
    row_id <- ((j-1)*delta_aux+1):(j*delta_aux)
    for(k in 1:fragment [i]){
      col_id <- ((k-1)*delta_aux+1):(k*delta_aux)
      if(sum(mat[row_id,col_id]) == 0){
        Table$N[i] <- Table$N[i] - 1
      }
    }
  }
}

Table$LogN <- log(Table$N)
lm_dim <- lm(Table$LogN ~ Table$LogDelta)

plot(Table$LogN ~ Table$LogDelta)
abline(lm_dim)

print('The box-counting dimension is:')
print(-lm_dim$coefficients[2])

# without the borders
Table <- Table[2:nrow(Table),]
lm_dim <- lm(Table$LogN ~ Table$LogDelta)

plot(Table$LogN ~ Table$LogDelta)
abline(lm_dim)

print('The box-counting dimension is:')
print(-lm_dim$coefficients[2])

Note that, this code is done only for matrixes of 512X512, for different dimensions you will have to change it. Also, you may want to do the linear fitting without the two borders because that will give you an error in the dimensions.

Santiago I. Hurtado
  • 1,113
  • 1
  • 10
  • 23
  • 1
    Thank you for the script. I'll give it a try. What do you mean "without the two borders"? Do you mean the borders of the matrix? – Derek Adams Oct 16 '19 at 17:09
  • 1
    I've tested your code on some obvious examples. A line produced a dimension of 0.93 while a rectangle produced a dimension of 1.88; while the results are not ideal they are certainly better than the other things I have come across - thanks again! – Derek Adams Oct 16 '19 at 18:01
  • Try to do the linear regression without the two borders (without the first and last element of Table). That should improve it. Also, note that for discrete data as the one you have, the box-counting dimension is an approximation and may differ a little from the real fractal dimension. – Santiago I. Hurtado Oct 16 '19 at 18:20
  • I edit the answer to show how to do the linear regression without the borders. Depending on the dataset (the length of it and the structure), you may have to eliminate more values of the borders. – Santiago I. Hurtado Oct 16 '19 at 18:23
  • Thank you for the clarification! – Derek Adams Oct 16 '19 at 19:18