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
Hari krishna
2019년 11월 3일
Could someone please help me to measure the crack and sketch a line on a diagram .
Image Analyst
2019년 11월 3일
채택된 답변
추가 답변 (6개)
Dan
2015년 4월 13일
0 개 추천
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
Yashwanth G M
2019년 3월 25일
0 개 추천
Sir help me to find out the area of a solar panel interms of Sq.mm....
댓글 수: 1
Image Analyst
2019년 3월 25일
Then post a new Question, not an Answer to BB's question.
Ram kumar R
2020년 2월 12일
0 개 추천
i got error in line no.21 as Ithres = im2bw(Igray_h,level) can u explain it ?
댓글 수: 4
Image Analyst
2020년 2월 12일
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
2021년 1월 2일
Understand your error
Ithres = im2bw(Igray_h,level) here
in this line just change igray_h with igray_s.
Ayoub MOSSLIH
2021년 2월 28일
Undefined function or variable 'gradmag'.
Error in Untitled (line 30)
BW = bwmorph(gradmag,'clean',10);
Image Analyst
2021년 2월 28일
@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
2021년 1월 2일
0 개 추천
See my attached demo that computes both the tortuosity and mean width.

댓글 수: 10
KAMOOSH BABA SHAIK
2021년 5월 19일
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
2021년 5월 19일
@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
2021년 5월 22일
sir i didnt get the code please share the code.
Image Analyst
2021년 5월 22일
It was attached to my main answer. Since you're not seeing it, I'm attaching demos here again.
Aman Kumar
2021년 6월 3일
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
2021년 6월 3일
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
2021년 6월 7일
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
2021년 6월 8일
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
2023년 1월 2일
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
2023년 1월 2일
@AMAR SAISH, the segmented images.
Meherunnisa
2023년 5월 12일
0 개 추천
%%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
Image Analyst
2023년 5월 12일
Your crack length computation does not even look at any measurements from the image. It just uses arbitrary values hard coded in.
Preetham Manjunatha
2024년 11월 30일
편집: Preetham Manjunatha
2025년 5월 16일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
