Ho to Automatically threshold to get only the crack?

3 views (last 30 days)
I apply rgb2gray > Averaging filter > then Canny edge detector
I used f=edge(i,'canny',0.9);
Instead I want MATLAB to automatically determine the ideal threshold for my image which only shows the crack edges and nothing else(I do not want to manually input 0.9)
Thanks in Adavnce
  1 Comment
Walter Roberson
Walter Roberson on 31 Mar 2016
Before we can do that you need to define what exactly a crack will look like for all images that will need to be processed. For example will the program also need to handle cracks in chinaware? Concrete? Will the pictures ever be in sunlight? Will they ever be taken at night with bioluminesent lighting?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 31 Mar 2016
Try this, for a start:
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 Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
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
% Get the base filename.
baseFileName = 'Crack001.PNG'; % Assign the one on the button that they clicked on.
% Get the full filename, with path prepended.
folder = pwd; % Current folder
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.
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
% Read in image.
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, 'Interpreter', 'None');
drawnow;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')
% Let's compute and display the histogram.
subplot(2, 2, 2);
histogram(grayImage);
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
% Threshold to find dark crack.
binaryImage = grayImage < 100;
% Blobs must be at least a certain number of pixels.
binaryImage = bwareaopen(binaryImage, 90);
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
  1 Comment
Walter Roberson
Walter Roberson on 31 Mar 2016
When I look at the original image, about 2/3 of the way towards the right, the obvious crack has a vertical peak and then starts to go down vertically again. Just before it goes down vertically, it appears to me that there is a secondary crack that goes from there to the left, to at least half way to the right, and possibly as far as 1/3 of the way to the right. "Cracks" do not necessarily have much of a gap.
But the original image was RGB and perhaps my interpretation of the original image would be different.

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!