Smoothing object except the portion which touches the border of the image

1 view (last 30 days)
Hi all, As attached my bw image here.
I want to smooth all cracks (black lines inside the mask). When I smooth them using imerode() followed by imdilate () with disk size 20 (for example), I have found objects touch the border (red circle in image) get affected. Is there any way to smooth objects except the ones touch the border of the image? Thanks in advance.

Accepted Answer

Image Analyst
Image Analyst on 27 Mar 2018
Sure. Use imfill() to find the black cracks/holes not touching the black background. Then blur with conv2() to smooth the edges. Then use the smoothed holes image to mask them out of the original image. See code below:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'Image_Smoothing.png';
% Get the full filename, with path prepended.
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
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 3); % Take blue channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis off
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Get the background and the black incursions.
binaryImage = grayImage > 128;
% Fill holes (black regions not touching the edge).
filledImage = imfill(binaryImage, 'holes');
% Subtract to find holes alone
holesOnly = xor(binaryImage, filledImage);
% Display the image.
subplot(2, 2, 2);
imshow(holesOnly, []);
title('Holes Alone', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Blur the holes image to smooth it.
windowSize = 21; % Adjust this to adjust smoothness.
kernel = ones(windowSize) / windowSize^2;
smoothedHoles = conv2(double(holesOnly), kernel, 'same');
% Display the image.
subplot(2, 2, 3);
imshow(smoothedHoles, []);
title('Smoothed Holes Only', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo(); % Let us snoop around at the values.
hp.Units = 'normalized';
hp.Position = [.2, 0.03, 0.1, 0.03];
% Threshold to get a smoothed binary image
smoothedHoles2 = smoothedHoles > 0.05; % Adjust this to get the smoothness you want.
% Use that to mask the original binary image with the smoothed interior holes.
finalImage = binaryImage & ~smoothedHoles2;
% Display the image.
subplot(2, 2, 4);
imshow(finalImage, []);
title('Smoothed Holes inside Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
  7 Comments
Noor Mohammad
Noor Mohammad on 29 Mar 2018
Thanks for your prompt answer. If you zoom in the mage, you could see that none pass through T-junction and all are separated. Almost similar colour make this confusion. Anyway, how to separate many thick cracks at junction points. Here they are clearly distinguished and separated without losing any pixels. If I take pixels away to separate cracks at the junction points, I eventually lose actual length of many cracks. Is there any way to recognise each crack without losing anything? Please the attached image for details. Thanks again.
Image Analyst
Image Analyst on 29 Mar 2018
Don't worry about it. Anyway, it's not clear at a point that divides two regions which it belongs to. So just erase it.
skelImage = bwmorph(binaryImage, 'skel', inf);
branchpointImage = bwmorph(skelImage, 'branchpoints');
segmentsImage = skelImage & ~branchpointImage;
If you want the original thick segments, you could dilate the branchpoints before ANDing with the original binary image.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!