zaro

How to Calculate Fractal Dimension in Matlab?

Published in Fractal Analysis 4 mins read

Calculating fractal dimension in Matlab, often using the box-counting method, involves determining how the detail in a fractal pattern changes with the scale at which it is measured. Here's a breakdown of the process:

Steps for Calculating Fractal Dimension using Box Counting in Matlab:

  1. Image Preparation:

    • Start with a binary image where the object of interest is represented by pixels with a value of 1 (foreground) and the background is 0.
    • Pad the image with background pixels (0s) so that both dimensions are a power of 2 (e.g., 64x64, 128x128, 256x256). This simplifies the box-counting algorithm.
  2. Box Size Initialization:

    • Initialize the box size e to the size of the entire image (e.g., if the image is 256x256, then e = 256).
  3. Box Counting Loop:

    • Iterate through different box sizes, progressively decreasing them (e.g., e = e / 2).
    • For each box size e:
      • Divide the image into non-overlapping boxes of size e x e.
      • Count the number of boxes N(e) that contain at least one foreground pixel (a pixel with a value of 1). In other words, count the boxes that intersect with the fractal structure.
  4. Repeat Until Smallest Box Size:

    • Continue the box-counting process (step 3) until the box size e reaches a predefined minimum value (e.g., 1 or 2 pixels).
  5. Log-Log Plot:

    • Create a log-log plot with log(1/e) on the x-axis and log(N(e)) on the y-axis. 1/e represents the scaling factor.
  6. Fractal Dimension Estimation:

    • Fit a straight line to the data points in the log-log plot.
    • The fractal dimension D is estimated as the negative of the slope of the fitted line: D = -slope.

Example Matlab Code Snippet:

function fractalDimension = boxCount(image)
  % image: Binary image (0s and 1s)

  % 1. Image preparation (padding to power of 2 - Example)
  [rows, cols] = size(image);
  pow2Size = 2^nextpow2(max(rows, cols));
  paddedImage = padarray(image, [pow2Size-rows, pow2Size-cols], 0, 'post');

  % 2. Initialize
  e = size(paddedImage, 1); % Box size
  E = []; % Vector to store box sizes (e)
  N = []; % Vector to store number of boxes (N(e))

  % 3. Box Counting Loop
  while (e >= 1) % e > 0.5 can provide more refined dimension
    E = [E, e];
    numBoxes = 0;

    % Iterate through boxes
    for i = 1:e:size(paddedImage, 1)
      for j = 1:e:size(paddedImage, 2)
        box = paddedImage(i:min(i+e-1, size(paddedImage, 1)), j:min(j+e-1, size(paddedImage, 2)));
        if any(box(:)) % If the box contains at least one 'on' pixel
          numBoxes = numBoxes + 1;
        end
      end
    end
    N = [N, numBoxes];
    e = e / 2;
  end

  % 5. Log-Log Plot and 6. Fractal Dimension Estimation
  logE = log(1./E);
  logN = log(N);

  % Linear Regression
  p = polyfit(logE, logN, 1); % Fit a line to the data
  fractalDimension = p(1);  % Slope of the fitted line
  % OR (Alternative using regression command)
  % b = regress(logN',[ones(length(logE),1) logE']);
  % fractalDimension = b(2);

  % Optional: Plot the Log-Log data points and fitted line
  % plot(logE,logN,'o',logE,polyval(p,logE),'-')
  % xlabel('log(1/e)');
  % ylabel('log(N(e))');
  % title(['Fractal Dimension: ' num2str(fractalDimension)]);

end

Important Considerations:

  • Image Quality: The accuracy of the fractal dimension estimation depends on the quality of the input image. Noise and artifacts can affect the results.
  • Binary Image Conversion: Thresholding methods can be employed to convert grayscale images to binary images; consider edge detection to highlight the structure.
  • Box Size Range: The range of box sizes used in the calculation can influence the estimated fractal dimension. It's crucial to select a range that captures the relevant scales of the fractal pattern. Usually the range must be large enough.
  • Edge Effects: Padding helps mitigate edge effects, but careful consideration is still needed, especially for fractals that extend to the image boundaries.
  • Line Fitting: The accuracy of the fractal dimension estimate depends on the quality of the linear fit to the log-log plot. Outliers and non-linearities can affect the results.

This method provides an estimation of the fractal dimension. Other methods may be more appropriate for specific types of fractals or images.