{"id":1112,"date":"2014-07-31T13:54:00","date_gmt":"2014-07-31T17:54:00","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1112"},"modified":"2019-11-01T11:15:38","modified_gmt":"2019-11-01T15:15:38","slug":"filling-circles","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2014\/07\/31\/filling-circles\/","title":{"rendered":"Filling circles"},"content":{"rendered":"<div class=\"content\"><p>Today's blog post comes from the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/?term=tag%3A%22image+processing%22\">stream of image processing questions<\/a> over at <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/\">MATLAB Answers<\/a>.<\/p><p>User <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/4728824-arjun\">arjun<\/a> wanted to know <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/144108-how-to-remove-specific-portions-of-a-image\">how to remove circles from an image by specifying their center and radius<\/a>.<\/p><p>Famous MATLAB Answer man <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/1343420-image-analyst\">Image Analyst<\/a> quickly gave a helpful reply.<\/p><p>I thought it might be worth a little discussion here (plus, I wanted to point out the value of MATLAB Answers.)<\/p><p>Typically, the process of answering the question started out with trying to understand the question better. \"What does <i>remove<\/i> mean to you?\" asked Image Analyst. The answer came back, \"I want to make the pixels in the circle be black.\"<\/p><p>OK, let's give that a try.<\/p><pre class=\"codeinput\">I = imread(<span class=\"string\">'coins.png'<\/span>);\r\nimshow(I)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_01.jpg\" alt=\"\"> <p>(Hmm. Guess I didn't have any quarters in my pocket when I took this picture.)<\/p><p>Suppose we want to set to black all the pixels centered at (X = 175, Y = 120) and with radius 35.<\/p><p>I like Image Analyst's recommended procedure:<\/p><div><ol><li>Compute a <i>mask image<\/i>. Generally, the term <i>mask<\/i> or <i>mask image<\/i> means a binary image whose foreground pixels correspond to a set of pixels of interest.<\/li><li>Use logical indexing with the mask image to modify the desired pixels.<\/li><\/ol><\/div><p>Start with creating a mask image.<\/p><pre class=\"codeinput\">xc = 175;\r\nyc = 120;\r\nr = 33;\r\n\r\nx = 1:size(I,2);\r\ny = 1:size(I,1);\r\n[xx,yy] = meshgrid(x,y);\r\n\r\nmask = hypot(xx - xc, yy - yc) &lt;= r;\r\n\r\nimshow(mask)\r\ntitle(<span class=\"string\">'Mask image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_02.jpg\" alt=\"\"> <p>Next, use logical indexing to modify the pixels of <tt>I<\/tt> corresponding to the foreground pixels of <tt>mask<\/tt>.<\/p><pre class=\"codeinput\">I2 = I;\r\nI2(mask) = 0;\r\nimshow(I2)\r\ntitle(<span class=\"string\">'Image with some pixels set to black'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_03.jpg\" alt=\"\"> <p>And we just lost 5 cents.<\/p><p>Image Analyst pointed out that you can use this technique to set pixel values in the circle to any value you want. How about white:<\/p><pre class=\"codeinput\">I2(mask) = 255;\r\nimshow(I2)\r\ntitle(<span class=\"string\">'Image with some pixels set to white'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_04.jpg\" alt=\"\"> <p>For fun, how about setting the pixels to the background color. How do we determine that? I might start by thresholding the original image.<\/p><pre class=\"codeinput\">bw = im2bw(I,graythresh(I));\r\nimshow(bw)\r\ntitle(<span class=\"string\">'Thresholded image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_05.jpg\" alt=\"\"> <p>Now fill in the holes.<\/p><pre class=\"codeinput\">bw = imfill(bw,<span class=\"string\">'holes'<\/span>);\r\nimshow(bw)\r\ntitle(<span class=\"string\">'Thresholded image with holes filled'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_06.jpg\" alt=\"\"> <p>Compute the mean value of the background pixels (using logical indexing again!).<\/p><pre class=\"codeinput\">bg_mean = mean(I(~bw))\r\n<\/pre><pre class=\"codeoutput\">\r\nbg_mean =\r\n\r\n   66.9959\r\n\r\n<\/pre><p>Replace the pixels in the circle with the computed average background value.<\/p><pre class=\"codeinput\">I2(mask) = bg_mean;\r\nimshow(I2)\r\ntitle(<span class=\"string\">'Circle pixels filled with average background'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_07.jpg\" alt=\"\"> <p>Let me point out two related Image Processing Toolbox functions. The <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/roifill.html\"><tt>roifill<\/tt><\/a> function can fill a region based on a mask; it fills pixels \"smoothly\" from the adjacent background pixels.<\/p><pre class=\"codeinput\">I3 = roifill(I,mask);\r\nimshow(I3)\r\ntitle(<span class=\"string\">'Circle pixels replaced using roifill'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_08.jpg\" alt=\"\"> <p>I also want to mention <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/imellipse.html\"><tt>imellipse<\/tt><\/a>. You can use this function to draw circles interactively using the mouse. Then you can use the <tt>createMask<\/tt> function to get the mask image corresponding to the circle you drew.<\/p><p>Happy circle filling!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_3caf73126b5e46fbadc8618f9c59acba() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='3caf73126b5e46fbadc8618f9c59acba ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3caf73126b5e46fbadc8618f9c59acba';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_3caf73126b5e46fbadc8618f9c59acba()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><\/div><!--\r\n3caf73126b5e46fbadc8618f9c59acba ##### SOURCE BEGIN #####\r\n%%\r\n% Today's blog post comes from the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/?term=tag%3A%22image+processing%22\r\n% stream of image processing questions> over at\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/ MATLAB Answers>.\r\n%\r\n% User\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/4728824-arjun\r\n% arjun> wanted to know\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/144108-how-to-remove-specific-portions-of-a-image\r\n% how to remove circles from an image by specifying their center and\r\n% radius>.\r\n%\r\n% Famous MATLAB Answer man\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/1343420-image-analyst\r\n% Image Analyst> quickly gave a helpful reply.\r\n%\r\n% I thought it might be worth a little discussion here (plus, I wanted to\r\n% point out the value of MATLAB Answers.)\r\n%\r\n% Typically, the process of answering the question started out with trying\r\n% to understand the question better. \"What does _remove_ mean to you?\"\r\n% asked Image Analyst. The answer came back, \"I want to make the pixels in\r\n% the circle be black.\"\r\n%\r\n% OK, let's give that a try.\r\n\r\nI = imread('coins.png');\r\nimshow(I)\r\n\r\n%%\r\n% (Hmm. Guess I didn't have any quarters in my pocket when I took this\r\n% picture.)\r\n%\r\n% Suppose we want to set to black all the pixels centered at (X = 175, Y =\r\n% 120) and with radius 35.\r\n%\r\n% I like Image Analyst's recommended procedure:\r\n%\r\n% # Compute a _mask image_. Generally, the term _mask_ or _mask image_\r\n% means a binary image whose foreground pixels correspond to a set of\r\n% pixels of interest.\r\n% # Use logical indexing with the mask image to modify the desired pixels.\r\n%\r\n% Start with creating a mask image.\r\n\r\nxc = 175;\r\nyc = 120;\r\nr = 33;\r\n\r\nx = 1:size(I,2);\r\ny = 1:size(I,1);\r\n[xx,yy] = meshgrid(x,y);\r\n\r\nmask = hypot(xx - xc, yy - yc) <= r;\r\n\r\nimshow(mask)\r\ntitle('Mask image')\r\n\r\n%%\r\n% Next, use logical indexing to modify the pixels of |I| corresponding to\r\n% the foreground pixels of |mask|.\r\n\r\nI2 = I;\r\nI2(mask) = 0;\r\nimshow(I2)\r\ntitle('Image with some pixels set to black')\r\n\r\n%%\r\n% And we just lost 5 cents.\r\n%\r\n% Image Analyst pointed out that you can use this technique to set pixel\r\n% values in the circle to any value you want. How about white:\r\n\r\nI2(mask) = 255;\r\nimshow(I2)\r\ntitle('Image with some pixels set to white')\r\n\r\n%%\r\n% For fun, how about setting the pixels to the background color. How do we\r\n% determine that? I might start by thresholding the original image.\r\n\r\nbw = im2bw(I,graythresh(I));\r\nimshow(bw)\r\ntitle('Thresholded image')\r\n\r\n%%\r\n% Now fill in the holes.\r\n\r\nbw = imfill(bw,'holes');\r\nimshow(bw)\r\ntitle('Thresholded image with holes filled')\r\n\r\n%%\r\n% Compute the mean value of the background pixels (using logical indexing\r\n% again!).\r\n\r\nbg_mean = mean(I(~bw))\r\n\r\n%%\r\n% Replace the pixels in the circle with the computed average background\r\n% value.\r\n\r\nI2(mask) = bg_mean;\r\nimshow(I2)\r\ntitle('Circle pixels filled with average background')\r\n\r\n%%\r\n% Let me point out two related Image Processing Toolbox functions. The\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/roifill.html |roifill|>\r\n% function can fill a region based on a mask; it fills pixels \"smoothly\"\r\n% from the adjacent background pixels.\r\n\r\nI3 = roifill(I,mask);\r\nimshow(I3)\r\ntitle('Circle pixels replaced using roifill')\r\n\r\n%%\r\n% I also want to mention\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/imellipse.html |imellipse|>.\r\n% You can use this function to draw circles interactively using the mouse.\r\n% Then you can use the |createMask| function to get the mask image\r\n% corresponding to the circle you drew.\r\n%\r\n% Happy circle filling!\r\n##### SOURCE END ##### 3caf73126b5e46fbadc8618f9c59acba\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/remove_circles_08.jpg\" onError=\"this.style.display ='none';\" \/><\/div><p>Today's blog post comes from the stream of image processing questions over at MATLAB Answers.User arjun wanted to know how to remove circles from an image by specifying their center and radius.Famous... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/07\/31\/filling-circles\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[82,334,84,414,136,76,36,308,30,368,190,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1112"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=1112"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1112\/revisions"}],"predecessor-version":[{"id":1114,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1112\/revisions\/1114"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}