Image processing for crack detection and length estimation
Mostra commenti meno recenti
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
Hari krishna
il 3 Nov 2019
Could someone please help me to measure the crack and sketch a line on a diagram .
Image Analyst
il 3 Nov 2019
Risposta accettata
Più risposte (6)
Dan
il 13 Apr 2015
0 voti
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
Yashwanth G M
il 25 Mar 2019
0 voti
Sir help me to find out the area of a solar panel interms of Sq.mm....
1 Commento
Image Analyst
il 25 Mar 2019
Then post a new Question, not an Answer to BB's question.
Ram kumar R
il 12 Feb 2020
0 voti
i got error in line no.21 as Ithres = im2bw(Igray_h,level) can u explain it ?
4 Commenti
Image Analyst
il 12 Feb 2020
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.
Dharmik Gohel
il 2 Gen 2021
Understand your error
Ithres = im2bw(Igray_h,level) here
in this line just change igray_h with igray_s.
Ayoub MOSSLIH
il 28 Feb 2021
Undefined function or variable 'gradmag'.
Error in Untitled (line 30)
BW = bwmorph(gradmag,'clean',10);
Image Analyst
il 28 Feb 2021
@Ayoub MOSSLIH, we have no idea what code you ran. Please start a new question and attach your image and m-file there.
Image Analyst
il 2 Gen 2021
0 voti
See my attached demo that computes both the tortuosity and mean width.

10 Commenti
KAMOOSH BABA SHAIK
il 19 Mag 2021
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
Image Analyst
il 19 Mag 2021
@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.
KAMOOSH BABA SHAIK
il 22 Mag 2021
sir i didnt get the code please share the code.
Image Analyst
il 22 Mag 2021
It was attached to my main answer. Since you're not seeing it, I'm attaching demos here again.
Aman Kumar
il 3 Giu 2021
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)
Image Analyst
il 3 Giu 2021
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?
KAMOOSH BABA SHAIK
il 7 Giu 2021
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
Image Analyst
il 8 Giu 2021
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.
AMAR SAISH
il 2 Gen 2023
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 ??
Image Analyst
il 2 Gen 2023
@AMAR SAISH, the segmented images.
Meherunnisa
il 12 Mag 2023
0 voti
%%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
Image Analyst
il 12 Mag 2023
Your crack length computation does not even look at any measurements from the image. It just uses arbitrary values hard coded in.
Preetham Manjunatha
il 30 Nov 2024
Modificato: Preetham Manjunatha
il 16 Mag 2025
0 voti
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
Scopri di più su Image Processing Toolbox in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
