How to measure a crack length (using skeleton function)?

I am trying to calculate the length of the crack (attached) however I can't figure out a way to remove the extra sections of the skeleton that branch off from the main crack path - crackoverlay.png.
Does anyone have any advice as to how I could do this?
clc
clear variables
RGB = imread('crack2.jpg'); % Aquires original image
I = rgb2gray(RGB); % Creates greyscale image
BW = imbinarize(I,'global'); % Binarises it
BW2 = ~(bwmorph(BW,'majority',Inf)); % Fills in holes
BW3 = bwmorph(BW2,'skel',Inf); % Reduces image to center areas of the white sections
BW4 = bwmorph(BW3,'spur',Inf); % Removes spurs
Ov = imfuse(RGB,BW4); % Overlay of original image and skeleton
figure('Name','All_Steps')
subplot(3,2,1), imshow(RGB), title('Original Image')
subplot(3,2,2), imshow(BW) , title('Binarised Image')
subplot(3,2,3), imshow(BW2), title('Binarised Inverted')
subplot(3,2,4), imshow(BW3), title('Skeleton')
subplot(3,2,5), imshow(BW4), title('Simplified Skeleton')
subplot(3,2,6), imshow(Ov), title('Overlay')

댓글 수: 4

This is the shortest path problem and there are several algorithms you can search for and learn about. You can apply it to the coordinates in BW4 which can be found using [y,x]=find(~BW4). You'll probably need to identify the starting and ending points manually which can be done using ginput() or something slimiar and then finding the closest coordinates to your selection.
Thomas Bainbridge
Thomas Bainbridge 2020년 10월 21일
편집: Thomas Bainbridge 2020년 10월 22일
Thank you - I'll give that a go.
UPDATE: Thats worked - thank you for your help
Well done! Perhaps you could share your solution as an answer. I'd be interested in seeing it.
Thanks - Ive uploaded my code as an answer along with a hyperlink to a MATLAB blog that had a lot of useful information.

댓글을 달려면 로그인하십시오.

 채택된 답변

Thomas Bainbridge
Thomas Bainbridge 2020년 10월 22일
Here is my solution code.
Thanks to Adam Danz for the pointers.
clc
clear variables
RGB = imread('crack2.jpg'); % Aquires original image
I = rgb2gray(RGB); % Creates greyscale image
BW = imbinarize(I,'global'); % Binarises it
BW2 = ~(bwmorph(BW,'majority',Inf)); % Fills in holes
BW3 = bwmorph(BW2,'skel',Inf); % Reduces image to center areas of the white sections
BW4 = bwmorph(BW3,'spur',Inf); % Removes spurs
Ov = imfuse(RGB,BW4); % Overlay of original image and skeleton
imshow(BW4) % Displays the skeleton de spurred
y1 = 151; % Start of crack co-ordinates
x1 = 23;
y2 = 169; % End of crack co-ords
x2 = 307;
hold on
plot(x1, y1, 'g*', 'MarkerSize', 15) % Puts * at location of start and end of crack
plot(x2, y2, 'g*', 'MarkerSize', 15)
hold off
D1 = bwdistgeodesic(BW4, x1, y1, 'quasi-euclidean');
D2 = bwdistgeodesic(BW4, x2, y2, 'quasi-euclidean');
D = D1 + D2;
D = round(D * 8) / 8;
D(isnan(D)) = inf;
skeleton_path = imregionalmin(D); % Returns regional minima
P = imoverlay(BW4, imdilate(skeleton_path, ones(3,3)), [1 0 0]);
imshow(P, 'InitialMagnification', 200)
hold on
plot(x1, y1, 'g*', 'MarkerSize', 15) % Start position marker
plot(x2, y2, 'g*', 'MarkerSize', 15) % End poisition marker
hold off
path_length = D(skeleton_path); % Path length calculation
path_length = path_length(1)
figure('Name','Montaged') % Montage of stages
montage({RGB, BW, BW2, BW3,BW4, Ov})
figure('Name','All_Steps')
subplot(3,2,1), imshow(RGB), title('Original Image')
subplot(3,2,2), imshow(BW) , title('Binarised Image')
subplot(3,2,3), imshow(BW2), title('Binarised Inverted')
subplot(3,2,4), imshow(BW3), title('Skeleton')
subplot(3,2,5), imshow(BW4), title('Simplified Skeleton')
subplot(3,2,6), imshow(Ov), title('Overlay')
figure('Name','Reduced')
subplot(2,3,1), imshow(RGB), title('Original Image')
subplot(2,3,2), imshow(BW2), title('Binarised Inverted')
subplot(2,3,3), imshow(BW4), title('De-Spurred Skeleton')
subplot(2,3,4), imshow(Ov), title('Overlay')
subplot(2,3,6), imshow(P), title('Path on Skeleton')

추가 답변 (1개)

0 개 추천

the script was so helpfull for my program too thanks!!
one question, the units from the path length are pixels?

댓글 수: 1

Yes, units are pixels. See attached spatial calibration demo and other demos.
Also, you might want to look at bwskel() - I think it has less spurs than bwmorph()

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by