Hi Alexander,
I can infer from the information given that there is a need for better segmentation of image in order to remove noises and small objects. One aspect where the image can be processed better is by leveraging the "bwareaopen" function.It helps to remove remove small objects from binary image.
Result after using "bwareaopen" function:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1612156/image.png)
Previous Result:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1612161/image.jpeg)
After segmentation, we can apply hough transforms to image in order to detect the crack lines:
% Existing implementation
I = imread(filename);
[BW, maskedImage] = segmentImage(I);
BW2_left = imcrop(BW, [tlX_left tlY_left width height]);
BW2_right = imcrop(BW, [tlX_right tlY_right width height]);
% -------------------------
RGB_left = imcrop(I, [tlX_left tlY_left width height]);
RGB_right = imcrop(I, [tlX_right tlY_right width height]);
% Using bwareaopen to eliminate small noises
BW2_left = bwareaopen(BW2_left, 100); % Adjust the size threshold as needed
BW2_right = bwareaopen(BW2_right, 100);
figure,
imshowpair(BW2_left,BW2_right, "montage")
% Extracting Hough Lines
lines_left = HoughLines(BW2_left);
lines_right = HoughLines(BW2_right);
%Draw hough lines for BW2_left
figure, imshow(RGB_left), hold on
max_len = 0;
for k = 1:length(lines_left)
xy = [lines_left(k).point1; lines_left(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','green');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','green');
% Determine the endpoints of the longest line segment
len = norm(lines_left(k).point1 - lines_left(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% Display max length on left hand side image
disp("max length on left side: ")
disp(max_len)
hold off
img_frame = getframe(gca);
BW2_left = img_frame.cdata;
% Draw hough lines for BW2_right
figure, imshow(RGB_right), hold on
max_len = 0;
for k = 1:length(lines_right)
xy = [lines_right(k).point1; lines_right(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','green');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','green');
% Determine the endpoints of the longest line segment
len = norm(lines_right(k).point1 - lines_right(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% Display max length on right hand side image
disp("max length on right side: ")
disp(max_len)
hold off
img_frame = getframe(gca);
BW2_right = img_frame.cdata;
% Show final result
figure,
imshowpair(BW2_left,BW2_right, "montage")
Extracting Hough lines implementation:
function lines = HoughLines(BW)
% Canny edges
BWc = edge(BW,'canny');
[H,T,R] = hough(BWc);
% Hough Peaks
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
% Hough lines (parameters can be adjusted)
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
end
Final result:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1612166/image.png)
These results can be improved more with better segmentation techniques and parameter tuning.
For more information on "bwareopen" function and "Hough Transforms", please refer to following links: