clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'test.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
grayImage = rgbImage(:, :, 1);
else
grayImage = rgbImage;
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
binaryImage = grayImage < 128;
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
drawnow;
bpImage = bwmorph(binaryImage, 'branchpoints');
bpImage2 = imdilate(bpImage, ones(3));
linesImage = binaryImage & ~bpImage2;
linesImage = bwareaopen(linesImage, 15);
[labeledImage, numberOfBlobs] = bwlabel(linesImage, 8);
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(2, 2, 3);
imshow(coloredLabelsImage);
title('Lines Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
drawnow;
props = regionprops(linesImage, 'PixelList', 'Solidity');
allSolidities = [props.Solidity]
subplot(2, 2, 4);
histogram(allSolidities);
grid on;
figure;
linesImage = bwpropfilt(linesImage, 'Solidity', [0.35, inf]);
linesImage = linesImage | bpImage;
linesImage = bwareaopen(linesImage, 15);
imshow(linesImage);
title('Lines Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
drawnow;
binaryImage2 =~ binaryImage;
binaryImage2(linesImage) = true;
figure;
imshow(binaryImage2);
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
drawnow;