Image processing for crack detection and length estimation

Hi, I have written the following matlab code to do the following:-
  • load rgb image of surface
  • contrast stretch
  • convert rgb to gray scale
  • image segmentation
  • morphological operations (thin, clean , fill, etc...)
  • imtool for pixel length determination
  • Calculation of crack length based on calibration of image and above determined pixel lenght.
My aim is to develop the SIMPLEST matlab code for automatic detection of cracks and estimate the length of the crack (if possible other geometrical properties) from a sample image.
The code is shown below:
%%load image
I=imread('two.jpg');
figure,imshow(I)
title('Original image')
%%Image adjust
Istrech = imadjust(I,stretchlim(I));
figure,imshow(Istrech)
title('Contrast stretched image')
%%Convert RGB image to gray
Igray_s = rgb2gray(Istrech);
figure,imshow(Igray_s,[])
title('RGB to gray (contrast stretched) ')
%%Image segmentation by thresholding
%use incremental value to run this selection till required threshold 'level' is
%achieved
level = 0.08;
Ithres = im2bw(Igray_h,level);
figure,imshow(Ithres)
title('Segmented cracks')
%%Image morphological operation
BW = bwmorph(gradmag,'clean',10);
figure,imshow(BW)
title('Cleaned image')
BW = bwmorph(gradmag,'thin', inf);
figure,imshow(BW)
title('Thinned image')
BW = imfill(gradmag, 'holes')
figure,imshow(BW)
title('Filled image')
%%Image tool
figure,imtool(BW1)
figure,imtool(I)
%%Calaculate crack length
calibration_length=0.001;
calibration_pixels=1000;
crack_pixel=35;
crack_length=(crack_pixel *calibration_length)/calibration_pixels;
Please, I need help from image specialist to improve the code from above to meet my aim. I have also attached a sample picture that I am using for this code.
Picture two.jpg is attached below:
Thanks

3 Commenti

BB BSB
BB BSB il 9 Apr 2015
Modificato: BB BSB il 9 Apr 2015
At he moment my supervisor thinks the above code is a manual method of determining crack length and requires it is 'automated' (by labeling and extracting length of labelled cracks??? is that possible?)
Cheers!
Could someone please help me to measure the crack and sketch a line on a diagram .
We'll try, after you've read this link

Accedi per commentare.

 Risposta accettata

You're just arbitrarily setting
crack_pixel=35;
You're not even doing it manually (with user assistance) - you're just setting some arbitrary number. What's up with that? If you need code to find the distance between the farthest points in a binary blob, see my attached demo.

15 Commenti

Thanks for your blob demo, helped alot! In line with your example I have started to tweak my code as below and would like to ask the following questions before.
i) Is the bwlabel function labelling ALL 4-5 cracks in the image under one label? if yes how do I make sure it labels each crack separately?
ii) the area returned from regionprop function is it for ALL 4-5 cracks in image? if yes how do get the area for each crack separately?
iii) the bwboundaries function returns 34 boundaries how do plot these boundaries such that the edges of each crack is highlighted.
iv) finally based on all these can you clarify me on how to determine the length of each crack (4-5 as shown in the sample image)? It was not clear from your example.
%% load image
I=imread('two.jpg');
Igray = rgb2gray(I);
figure,imshow(Igray)
title('Gray image')
%% Binarize
level = graythresh(Igray);
binaryImage = im2bw(Igray, level);
figure,imshow(binaryImage)
title('Binarized image')
%% Labeling & regionprop
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Area');
%% Boundaries
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
No, they're all separate. measurements is an array of structures. Each structure represents one blob. The fields of the structure are whatever you measure, like area, perimeter, etc.
BB BSB
BB BSB il 10 Apr 2015
Modificato: BB BSB il 10 Apr 2015
Okay, thanks. I see the problem in my code. When I convert the image 'two.jpg' image to binary the cracks in are shown in black as background, while the surface of the steel is taken as foreground white. Thus the label and regionprop function returns only 'one' label and field respectively. How can I fix this pls?
I think with this fixed I can apply bwboundaries function to find which two boundaries are the farthest for each label (each crack) right?
YES! I have managed to label and plot out each crack and also get its boundaries and area. see code below:
%%load image
I=imread('GCC4.jpg');
figure,imshow(I)
title('Original Image')
%%Binarize
Igray = rgb2gray(I);
level = 0.599;
binaryImage = im2bw(Igray,level);
figure,imshow(binaryImage)
title('Binarized image')
%%Sawp background and foreground pixels
[row col] = size(binaryImage);
binaryImage2=zeros(row,col);
for i=1:row
for j=1:col
if binaryImage(i,j)==0
binaryImage2(i,j)=1;
else
binaryImage2(i,j)=0;
end
end
end
figure,imshow(binaryImage2)
title('Inverse binary Image')
%%Cleaning thining skel fill open close etc
binaryImage2 = bwmorph(binaryImage2,'clean',inf);
figure,imshow(binaryImage2)
title('Cleaned image')
binaryImage2 = imfill(binaryImage2, 'holes');
figure,imshow(binaryImage2)
title('Filled image')
% Get rid of anything less than 1% of the image
binaryImage2 = bwareaopen(binaryImage2, round(0.001*numel(binaryImage2)));
figure,imshow(binaryImage2)
%%Labeling image and regionprop for area
labeledImage = bwlabel(binaryImage2);
number_of_labeles = max(max(labeledImage))
for i= 1:number_of_labeles
Im = (labeledImage==i)
figure, imshow(Im)
end
measurements = regionprops(labeledImage, 'Area');
%%Boundaries
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
I would like to know if I can apply farthest boundaries points demo on each label to determine the length of each crack???? Thanks alot!
This code is not very efficient and you could cut out a few steps (inverse and clean). However when I tried to run it on the image your originally attached, two.jpg, it didn't seem to work at all. Please attach 'GCC4.jpg' so I can improve your code.
Right, In Binarize section the value of level is not same for 'two.jpg'.
level = 0.063;
binaryImage = im2bw(Igray,level);
That is another short fall of my code.
GCC4.jpg is attached
Thanks!
Well that looks completely different and would require a different algorithm. Your first image could probably be segmented fairly well with a global threshold. Maybe try adapthisteq() on this to even out the slow undulations and give a flatter image which you can then use a global threshold on.
BB BSB
BB BSB il 14 Apr 2015
Modificato: BB BSB il 14 Apr 2015
Like you said the first image is straight forward with global threshold.
However the second is not well segmented even with the adapthisteq() function.
Is there a Variable or optimum global thresholding function? and can I use it here?
Else I will stick to selecting graythresh level by trial and error for now (if no simple way out), with that, what are the improvements you offered the code?
If it doesn't work well after adapthisteq(), then you're going to have to develop a much more sophisticated algorithm. It looks like if you improved this lighting, camera, and focus that you could get a much better image to start with.
I went through your code and couldn't understand the distance measurement. if possible, could you let me know the algorithm.
I have no idea what algorithm or program you're talking about. There has been lots of code segments posted here. Please be specific.
to Image Analyst:
I have tried your code "farthest_points.m" . how can I choose another image other than you have set?
sir can you help with my project i want to measure crack length,width
ma'am I am doing the same project. I would reaaly appreciate it if you could help me out!
@Pilli Lalvinnu, sure. What sort of help do you need? I'm assuming you started with the solutions given here but they didn't work for your image(s) so start a new question with your code and images attached, and ask a specific question and we'll help.

Accedi per commentare.

Più risposte (6)

I just glanced at your code and if you need a completely automated system, you need to either define what constitutes a crack beforehand OR have the system learn these rules using something like a neural net.
But... that's a bigger project. In the short term, you might want to play with the graythresh function that will automate your selection of a threshold.
Just my $0.02.
Dan

1 Commento

BB BSB
BB BSB il 14 Apr 2015
Modificato: BB BSB il 14 Apr 2015
I see, for timeline limits Id rather attempt the easier of the 3 methods you outlined which I guess is playing around with graythresh function.
Any hints as to how or what to look out for in doing so? Because my first approach would be to scale by a factor??? make any sense? Thanks.

Accedi per commentare.

Sir help me to find out the area of a solar panel interms of Sq.mm....
i got error in line no.21 as Ithres = im2bw(Igray_h,level) can u explain it ?

4 Commenti

You forgot to tell us the error! What is there to explain?
You probably have a color image. Start your own question since this is not an Answer to the original question. Attach your code, your image, and the error message.
Understand your error
Ithres = im2bw(Igray_h,level) here
in this line just change igray_h with igray_s.
Undefined function or variable 'gradmag'.
Error in Untitled (line 30)
BW = bwmorph(gradmag,'clean',10);
@Ayoub MOSSLIH, we have no idea what code you ran. Please start a new question and attach your image and m-file there.

Accedi per commentare.

See my attached demo that computes both the tortuosity and mean width.

10 Commenti

Sir please share me the code for crack detention like width length measurement.. My email khamooshbaba786@gmail.com It's my final year project sir
@KAMOOSH BABA SHAIK, since you're not allowed to turn in my code for your senior project, all I have to share with you is the code I already did in my answer. Just segment your image to find cracks, like via thresholding or some other method, then run my code on the binary image to get the widths of all the crack blobs. I'm sure you can do it - it's not hard.
sir i didnt get the code please share the code.
It was attached to my main answer. Since you're not seeing it, I'm attaching demos here again.
Problem with this code
in line 'mask = bwareafilt(mask, 1)'
%Program to compute the mean width of a bob in an image.
clearvars
close all
clc
fontSize = 15
%Read in original image, with white lighting on black background.
baseFileName = '1.jpg'
fullFileName = fullfile(pwd,"1.jpg")
grayImage = imread("1.jpg")
%Get the dimensions of the image.
%Number of color channels should br = 1 for gray scale image, and 3 for abd
%RGB color image.
[rows,columns,numberOfColorchannels] = size(grayImage)
if numberOfColorchannels>1
else
end
subplot(2, 3, 1)
imshow(grayImage,[])
impixelinfo
title('Original Image','FontSize',fontSize)
mask = imbinarize(grayImage)
mask = imfill(mask,'holes')
mask = bwareafilt(mask, 1)
subplot(2,3,2)
imshow(mask)
impixelinfo
title('Mask','FontSize',fontSize)
What is the problem? You forgot to post both the error and the image 1.jpg.
Why did you take out the code that converted the image to gray scale if it was color?
Sir please give me the code. I want to find width and length of the crack using image processing method. I don't know how to write the code. I am not from coding background. This is my b.tech final year project sir. Please provide me code. My email : kamooshbaba786@gmail.com
So your technical university really assigns you an image processing programming task, doesn't give you any training on programming by your final year, and allows you to ask strangers/others to program it up and give it to you to turn in their code as your own? I've never heard of a university like that. In fact, most would land you in serious trouble for doing that. Maybe you can ask them to give you a different task - one you can complete by yourself. Because, sorry, but we're not going to go to all the work of programming it and giving it to you. The Mathworks would but of course they would charge a consulting fee since it will take their valuable time. The best I can suggest are these two links:
MATLAB Academy - Free 2 hour training Image Segmentation Tutorial
With about 4 hours work looking over those two web pages, you should easily be able to modify my Image Segmentation Tutorial to work with your images for simple images.
sir, i have done image segmentation for crack images. Now, i want to find the length and width of crack, so i should use this code on original images or segmented images ??

Accedi per commentare.

%%load image
I=imread('two.jpg'); figure,imshow(I) title('Original image')
%%Image adjust
Istrech = imadjust(I,stretchlim(I)); figure,imshow(Istrech) title('Contrast stretched image')
%%Convert RGB image to gray
Igray_s = rgb2gray(Istrech); figure,imshow(Igray_s,[]) title('RGB to gray (contrast stretched) ')
%%Image segmentation by thresholding %use incremental value to run this selection till required threshold 'level' is %achieved
level = 0.08; Ithres = im2bw(Igray_h,level); figure,imshow(Ithres) title('Segmented cracks')
%%Image morphological operation
BW = bwmorph(gradmag,'clean',10); figure,imshow(BW) title('Cleaned image')
BW = bwmorph(gradmag,'thin', inf); figure,imshow(BW) title('Thinned image')
BW = imfill(gradmag, 'holes') figure,imshow(BW) title('Filled image')
%%Image tool
figure,imtool(BW1) figure,imtool(I)
%%Calaculate crack length
calibration_length=0.001; calibration_pixels=1000; crack_pixel=35;
crack_length=(crack_pixel *calibration_length)/calibration_pixels;

1 Commento

Your crack length computation does not even look at any measurements from the image. It just uses arbitrary values hard coded in.

Accedi per commentare.

Preetham Manjunatha
Preetham Manjunatha il 30 Nov 2024
Modificato: Preetham Manjunatha il 16 Mag 2025
Here is the MATLAB Crack segmentation and Crack width, length and area estimation codes to calculate/estimate the crack area, width and length. In addition, this package assumes the crack is segmented either using morphological method or multiscale gradient-based or deep learning semantic segmentation methods. This package estimates the crack area, width and length (pixel scale can be provided to estimate these physical quantities). Lastly, the semantic segmentation and object detection metrics for the cracks can be found using Cracks binary class bounding box and segmentation metrics package.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by