Clear Filters
Clear Filters

How to fill the region of interest by white color

9 views (last 30 days)
Hi All,
I have this Black and white mask
I want to fill the region of interest to be like this
approximatlly by white color to use it for segmentation later,please any could help by a Matlab code to do this job,thanks in advance.

Accepted Answer

Image Analyst
Image Analyst on 1 Jan 2014
See this demo which uses activecountour to get the outer envelope:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Read in a demo image.
folder = 'D:\Temporary stuff';
baseFileName = '4_400_before.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
subplot(2, 2, 2);
mask = false(rows, columns);
mask(50:300,20:300) = true;
imshow(mask);
axis on;
title('Initial mask', 'FontSize', fontSize);
drawnow;
% Now find the outer boundary.
bw = activecontour(grayImage, mask, 400, 'edge');
subplot(2, 2, 3);
imshow(bw);
axis on;
title('Outer Boundary Mask', 'FontSize', fontSize);
drawnow;
% Display the original gray scale image in the lower left
% So we can display boundaries over it.
subplot(2, 2, 4);
imshow(grayImage, []);
axis on;
hold on;
% Display the initial contour on the original image in blue.
% Display the final contour on the original image in red.
contour(mask,[0 0],'b', 'LineWidth', 2);
contour(bw,[0 0],'r', 'LineWidth', 4);
title('Image with initial and final contours overlaid', 'FontSize', fontSize);
msgbox('Done with activecontour demo');
  13 Comments
Sakib Mahmud
Sakib Mahmud on 3 Jul 2020
Can't we use rgb2gray() instead of taking only the Green channel?
Image Analyst
Image Analyst on 3 Jul 2020
You can do whatever you want. If you have an image with continuous colors, then using rgb2gray() will take the weighted average of the 3 color channels (that's a lot of computations). However if your image is already gray scale (all three color channels are identical), then taking just one of them will be faster since no math needs to take place. If you're going to take just one of them, you should look at the channels first to determine which of them gives you the most contrast. Chances are you can pick just one of the color channels and it will give you more contrast, making the segmentation you'll do next easier, than the contrast of the average of the R, G, and B channels.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 31 Dec 2013
Your gaps are fairly big and are going to cause you problems, I suspect.
  9 Comments
Walter Roberson
Walter Roberson on 31 Dec 2013
Consider using Alpha Shapes. The effect can be like putting an elastic band around the object, effectively closing the curve.
Image Analyst
Image Analyst on 1 Jan 2014
Alpha shapes might be good. Sometimes the boundary can go inside the points so you have to be aware of how you do it to make sure it gives you a boundary you are happy with. Concave hull doesn't go inside points (I don't think) though it can jump gaps. I don't have experience with alpha shapes (though I'd like to) but I do have experience with concave hulls.

Sign in to comment.


Image Analyst
Image Analyst on 31 Dec 2013
How did you get that bad mask in the first place? I think it might be better to improve it from the start than to try to fix up a bad one. If you need to use that one I'd just close with a bigger disk, or use imline to burn a line into the images manually, or use edge linking methods, or use the concave hull (restricted convex hull, demo attached).
  10 Comments
Walter Roberson
Walter Roberson on 1 Jan 2014
Which "ant algorithm" ? Ant Colony Optimization?
Image Analyst
Image Analyst on 1 Jan 2014
Not sure, but that's what I assumed. It's the only ant algorithm I've ever heard of.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!