{"id":259565,"date":"2025-07-19T03:55:12","date_gmt":"2025-07-19T03:55:12","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=259565"},"modified":"2025-07-19T03:55:14","modified_gmt":"2025-07-19T03:55:14","slug":"need-matlab-code","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/07\/19\/need-matlab-code\/","title":{"rendered":"Need MATLAB code"},"content":{"rendered":"\n<p>Need MATLAB code for imfill and bwdist USING ALGORITHM, NO BUILT-IN FUNCTIONS imfill is used to fill &#8220;holes&#8221; 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.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>To implement the <code>imfill<\/code> and <code>bwdist<\/code> functions from scratch in MATLAB without using built-in functions, let&#8217;s break down the algorithms:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>imfill<\/code> (Flood-Fill for Holes)<\/h3>\n\n\n\n<p>The <code>imfill<\/code> function is typically used to fill &#8220;holes&#8221; 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.<\/p>\n\n\n\n<p><strong>Algorithm<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a mask that identifies the background pixels (those that are zero).<\/li>\n\n\n\n<li>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.<\/li>\n\n\n\n<li>The result is the filled image.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><code>bwdist<\/code> (Euclidean Distance Transform)<\/h3>\n\n\n\n<p>The <code>bwdist<\/code> 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.<\/p>\n\n\n\n<p><strong>Algorithm<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize a distance map with large values where the object pixels are present and zero elsewhere.<\/li>\n\n\n\n<li>Perform a forward pass (from left to right and top to bottom).<\/li>\n\n\n\n<li>Perform a backward pass (from right to left and bottom to top) to refine the distances.<\/li>\n<\/ol>\n\n\n\n<p>Here\u2019s a MATLAB implementation of both <code>imfill<\/code> and <code>bwdist<\/code> without using built-in functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MATLAB Code:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">matlabCopyEdit<code>function filledImage = imfill_manual(BW)\n    % Create an empty image to store filled results\n    filledImage = BW;\n    \n    % Get image dimensions\n    [rows, cols] = size(BW);\n    \n    % Initialize a mask for visited pixels\n    visited = false(rows, cols);\n    \n    % Start flood fill from the boundary pixels (background)\n    for r = 1:rows\n        for c = 1:cols\n            if (r == 1 || r == rows || c == 1 || c == cols) &amp;&amp; BW(r, c) == 0\n                % Perform DFS or BFS here to fill the holes\n                floodFill(filledImage, visited, r, c, rows, cols);\n            end\n        end\n    end\nend\n\nfunction floodFill(image, visited, r, c, rows, cols)\n    % Initialize stack for DFS\n    stack = [r, c];\n    \n    while ~isempty(stack)\n        % Get current pixel\n        current = stack(end, :);\n        stack(end, :) = []; % Pop the last element\n        \n        r = current(1);\n        c = current(2);\n        \n        if r &gt; 0 &amp;&amp; r &lt;= rows &amp;&amp; c &gt; 0 &amp;&amp; c &lt;= cols &amp;&amp; ~visited(r, c)\n            % Mark as visited\n            visited(r, c) = true;\n            \n            if image(r, c) == 0 % If it's a background pixel, fill it\n                image(r, c) = 1;\n                \n                % Add neighboring pixels to the stack\n                stack = [stack; r-1, c; r+1, c; r, c-1; r, c+1]; % Up, Down, Left, Right\n            end\n        end\n    end\nend\n\nfunction distMap = bwdist_manual(BW)\n    % Get image dimensions\n    [rows, cols] = size(BW);\n    \n    % Initialize distance map (large values for the background)\n    distMap = inf(rows, cols);\n    \n    % Set distance of the foreground pixels to 0\n    distMap(BW == 1) = 0;\n    \n    % Perform forward pass (top-left to bottom-right)\n    for r = 1:rows\n        for c = 1:cols\n            if BW(r, c) == 0 % If background pixel\n                if r &gt; 1\n                    distMap(r, c) = min(distMap(r, c), distMap(r-1, c) + 1); % Top\n                end\n                if c &gt; 1\n                    distMap(r, c) = min(distMap(r, c), distMap(r, c-1) + 1); % Left\n                end\n            end\n        end\n    end\n    \n    % Perform backward pass (bottom-right to top-left)\n    for r = rows:-1:1\n        for c = cols:-1:1\n            if BW(r, c) == 0 % If background pixel\n                if r &lt; rows\n                    distMap(r, c) = min(distMap(r, c), distMap(r+1, c) + 1); % Bottom\n                end\n                if c &lt; cols\n                    distMap(r, c) = min(distMap(r, c), distMap(r, c+1) + 1); % Right\n                end\n            end\n        end\n    end\n    \n    % Convert to Euclidean distance\n    distMap = sqrt(distMap);\nend\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>imfill_manual<\/code> Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We initialize a <code>filledImage<\/code> matrix to store the output of the filling process.<\/li>\n\n\n\n<li>We use a flood-fill algorithm (DFS in this case) to propagate from the boundary pixels and fill the holes (background pixels).<\/li>\n\n\n\n<li>The function <code>floodFill<\/code> recursively fills the image by marking the visited pixels and filling the empty regions.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>bwdist_manual<\/code> Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We initialize the <code>distMap<\/code> to hold the distance values, setting all foreground pixels (value <code>1<\/code>) to <code>0<\/code> and the background pixels to infinity (<code>inf<\/code>).<\/li>\n\n\n\n<li>The forward pass computes the minimum distance from the current pixel to its neighbors (up, left).<\/li>\n\n\n\n<li>The backward pass refines the distances by checking the bottom and right neighbors.<\/li>\n\n\n\n<li>The result is the Euclidean distance from each pixel to the nearest foreground pixel.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Notes:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The flood-fill algorithm is implemented using a stack for DFS. It starts from boundary pixels and recursively fills in background pixels.<\/li>\n\n\n\n<li>The distance transform uses a forward and backward pass to compute the Euclidean distance to the nearest object pixel.<\/li>\n<\/ul>\n\n\n\n<p>This code avoids using built-in MATLAB functions and provides custom implementations for <code>imfill<\/code> and <code>bwdist<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner6-997.jpeg\" alt=\"\" class=\"wp-image-259566\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Need MATLAB code for imfill and bwdist USING ALGORITHM, NO BUILT-IN FUNCTIONS imfill is used to fill &#8220;holes&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-259565","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/259565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=259565"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/259565\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=259565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=259565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=259565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}