Extract the INNER white region of an image with black and white intersection.

4 views (last 30 days)
Hello,
I would like to get the inner part of the white region in the image below, the region I want is the red rectangluar part (INNER part of the white region). I do not want the outter part of the white region.
However, with my code below, I can only get the out part of the white region, is there anyone can help me?
Thanks a lot.
%%%%%My code
I = imread(filename);
Igray = rgb2gray(I);
BW2 = Igray < 50;
% Detect rectangle region (inside the line)
BW2 = imclearborder(~BW2);
BW2 = bwareafilt(BW2,1);
% Calculate bounding box
props2= regionprops(BW2,'BoundingBox');
boundingBox2 = props2.BoundingBox;
figure
imshow(BW2)

Accepted Answer

Image Analyst
Image Analyst on 5 Mar 2021
First of all you don't need imclearborder because there is no part of your blob that touches the border. And after bwareafilt() you're probably still getting the original blob because some of the outer blobs are touching your inner blob by having the corners of the pixels touch. The default for bwareafilt() is 8-connected, which considers blobs touching on the diagonal to be the same blob. You should specify the "4-connected" option so that a blob is only connected if it's fully touching on the top, bottom, left, or right (not the diagonal/corner).
  4 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Mar 2021
Try this:
% Demo by Image Analyst, March, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'diamond.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, '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);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
% Display the test image full size.
subplot(2, 3, 1);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Original Gray Scale Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
%--------------------------------------------------------------------------------------------------------
% Get histogram
subplot(2, 3, 2);
counts = histcounts(grayImage);
bar(log(counts));
grid on;
title('Log Histogram of gray scale image', 'FontSize', fontSize);
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
% Threshold image.
mask = grayImage > 128;
% Display mask image.
subplot(2, 3, 3);
imshow(mask, []);
axis('on', 'image');
title('Initial Mask (Binary Image)', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(mask, 4);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 3, 4);
imshow(coloredLabelsImage);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('%d Labeled Blobs', numberOfBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, grayImage, 'all');
numberOfBlobs = size(blobMeasurements, 1);
% Do a morphological opening to separate the blobs.
radius = 6;
se = strel('disk', radius, 0);
mask = imerode(mask, se); % Shrink blobs
% Take the largest blob only
mask = bwareafilt(mask, 1);
% Grow them back to the original size.
mask = imdilate(mask, se); % Shrink blobs
% Display mask image.
subplot(2, 3, 5);
imshow(mask, []);
axis('on', 'image');
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
fprintf('Done running %s.m\n', mfilename);
  4 Comments
zhifei xu
zhifei xu on 5 Mar 2021
The largest one can be sure. But can I adjust the structuring element size automatically to get what I want? Such as this figure, how can adjust the structuring element size for serval different figures similar to the one in the question and the one below? By the way, I found that the "Threshold of the image" also plays an important role.
Thanks
Image Analyst
Image Analyst on 5 Mar 2021
I think it should work on that. Does it not?
You'll have to come up with some segmentation algorithm. Using a fixed threshold is best but if you can't get control over your image capture conditions, like camera shutter speed or illumination level then you'll have to come up with some sort of automatic threshold selection routine.

Sign in to comment.

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!