Need MATLAB code

Need MATLAB code for imfill and bwdist USING ALGORITHM, NO BUILT-IN FUNCTIONS imfill is used to fill “holes” in an image and performs a flood-fill operation on the background pixels of the input binary image BW, starting from the points specified in locations. bwdist computes the Euclidean distance transform of the binary image BW. For each pixel in BW, the distance transform assigns a number that represents the distance between that pixel and the nearest nonzero pixel of BW.

The Correct Answer and Explanation is:

To implement the imfill and bwdist functions from scratch in MATLAB without using built-in functions, let’s break down the algorithms:

imfill (Flood-Fill for Holes)

The imfill function is typically used to fill “holes” in a binary image. The algorithm works by performing a flood-fill operation, starting from boundary pixels (those connected to the background) and gradually filling in holes. This can be done by marking regions that are connected to the boundary and filling everything else.

Algorithm:

  1. Create a mask that identifies the background pixels (those that are zero).
  2. Flood-fill the background pixels starting from the boundary using a BFS (Breadth-First Search) or DFS (Depth-First Search) approach to propagate the filling.
  3. The result is the filled image.

bwdist (Euclidean Distance Transform)

The bwdist function computes the Euclidean distance of each pixel in the image to the nearest nonzero pixel. The distance transform algorithm uses a multi-pass approach with 2 passes (one forward and one backward) to calculate the minimum distance for each pixel.

Algorithm:

  1. Initialize a distance map with large values where the object pixels are present and zero elsewhere.
  2. Perform a forward pass (from left to right and top to bottom).
  3. Perform a backward pass (from right to left and bottom to top) to refine the distances.

Here’s a MATLAB implementation of both imfill and bwdist without using built-in functions.

MATLAB Code:

matlabCopyEditfunction filledImage = imfill_manual(BW)
    % Create an empty image to store filled results
    filledImage = BW;
    
    % Get image dimensions
    [rows, cols] = size(BW);
    
    % Initialize a mask for visited pixels
    visited = false(rows, cols);
    
    % Start flood fill from the boundary pixels (background)
    for r = 1:rows
        for c = 1:cols
            if (r == 1 || r == rows || c == 1 || c == cols) && BW(r, c) == 0
                % Perform DFS or BFS here to fill the holes
                floodFill(filledImage, visited, r, c, rows, cols);
            end
        end
    end
end

function floodFill(image, visited, r, c, rows, cols)
    % Initialize stack for DFS
    stack = [r, c];
    
    while ~isempty(stack)
        % Get current pixel
        current = stack(end, :);
        stack(end, :) = []; % Pop the last element
        
        r = current(1);
        c = current(2);
        
        if r > 0 && r <= rows && c > 0 && c <= cols && ~visited(r, c)
            % Mark as visited
            visited(r, c) = true;
            
            if image(r, c) == 0 % If it's a background pixel, fill it
                image(r, c) = 1;
                
                % Add neighboring pixels to the stack
                stack = [stack; r-1, c; r+1, c; r, c-1; r, c+1]; % Up, Down, Left, Right
            end
        end
    end
end

function distMap = bwdist_manual(BW)
    % Get image dimensions
    [rows, cols] = size(BW);
    
    % Initialize distance map (large values for the background)
    distMap = inf(rows, cols);
    
    % Set distance of the foreground pixels to 0
    distMap(BW == 1) = 0;
    
    % Perform forward pass (top-left to bottom-right)
    for r = 1:rows
        for c = 1:cols
            if BW(r, c) == 0 % If background pixel
                if r > 1
                    distMap(r, c) = min(distMap(r, c), distMap(r-1, c) + 1); % Top
                end
                if c > 1
                    distMap(r, c) = min(distMap(r, c), distMap(r, c-1) + 1); % Left
                end
            end
        end
    end
    
    % Perform backward pass (bottom-right to top-left)
    for r = rows:-1:1
        for c = cols:-1:1
            if BW(r, c) == 0 % If background pixel
                if r < rows
                    distMap(r, c) = min(distMap(r, c), distMap(r+1, c) + 1); % Bottom
                end
                if c < cols
                    distMap(r, c) = min(distMap(r, c), distMap(r, c+1) + 1); % Right
                end
            end
        end
    end
    
    % Convert to Euclidean distance
    distMap = sqrt(distMap);
end

Explanation:

  1. imfill_manual Function:
    • We initialize a filledImage matrix to store the output of the filling process.
    • We use a flood-fill algorithm (DFS in this case) to propagate from the boundary pixels and fill the holes (background pixels).
    • The function floodFill recursively fills the image by marking the visited pixels and filling the empty regions.
  2. bwdist_manual Function:
    • We initialize the distMap to hold the distance values, setting all foreground pixels (value 1) to 0 and the background pixels to infinity (inf).
    • The forward pass computes the minimum distance from the current pixel to its neighbors (up, left).
    • The backward pass refines the distances by checking the bottom and right neighbors.
    • The result is the Euclidean distance from each pixel to the nearest foreground pixel.

Notes:

  • The flood-fill algorithm is implemented using a stack for DFS. It starts from boundary pixels and recursively fills in background pixels.
  • The distance transform uses a forward and backward pass to compute the Euclidean distance to the nearest object pixel.

This code avoids using built-in MATLAB functions and provides custom implementations for imfill and bwdist.

Scroll to Top