How do I find the centerlines shown in the image?

7 views (last 30 days)
Original image: "myimage"
Binary image: "binary"
Code to reach from original image to binary image:
I = imread("myimage.jpg");
I = im2gray(I);
I = imadjust(I);
Igs = I > 150;
imshow(Igs)
Igs = imfill(Igs,"holes");
Igs = bwareaopen(Igs,50);
imshow(Igs);
SE = strel("disk",10);
Igs = imclose(Igs,SE);
Igs = imopen(Igs,SE);
imshow(Igs)
I want to detect the 6 centerlines shown in the binary image marked in red. What functions or method do I use to detect these centerlines?
Thankyou.

Answers (1)

Prateek
Prateek on 22 Nov 2022
Hi Husain,
There can be different approaches to find and draw centerlines, such as scanning image columns or using the function “bwskel. The following links contain detailed discussions on the same:
  1. Finding center of a line - MATLAB Answers - MATLAB Central (mathworks.com)
  2. How to find the centreline of two curves - MATLAB Answers - MATLAB Central (mathworks.com)
  3. Centerline and bounding curve in image - MATLAB Answers - MATLAB Central (mathworks.com)
Please refer to these discussions. These should be helpful in constructing a solution.
Regards,
Prateek
  2 Comments
Husain Signalwala
Husain Signalwala on 29 Nov 2022
Thankyou for your reply. I could not construct a solution from the above methods. The bwskel() operation does not give me accurate results.
Here is my code using the bwskel() operation.
I = imread("myimage.jpg");
Error using imread>get_full_filename
File "myimage.jpg" does not exist.

Error in imread (line 372)
fullname = get_full_filename(filename);
I = im2gray(I);
I = imadjust(I);
Igs = I > 150;
imshow(Igs)
Igs = bwareaopen(Igs,30);
imshow(Igs)
avg = fspecial("average",[45 45]); %average filtering
Iavg = imfilter(Igs,avg);
If = medfilt2(Iavg,[45 45]); %median filtering
imshow(If)
S = bwskel(If);
imshow(S)
[H,T,R] = hough(S); %applying the hough transform
houghMatViz(H,T,R)
peaks = houghpeaks(H,8)
lines = houghlines(S,T,R,peaks)
plotHoughLines(I,lines)
function houghMatViz(H,T,R)
% Convert the hough matrix to a grayscale image
Hgray = mat2gray(H);
% Enhance its brightness
Hgray = imadjust(Hgray);
% Display the hough transform
imagesc(Hgray,"XData",T,"YData",R)
% Assign a colormap for the image
colormap(hot)
title("Hough Transform")
% Add x-y labels
xlabel("\theta")
ylabel("\rho")
end
function plotHoughLines(image, lines)
% Show the image
imshow(image)
% Plot the hough transform lines
hold on
numLines = length(lines);
for k = 1:numLines
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),"LineWidth",2,"Color","red",...
"Marker", "x", "MarkerEdgeColor", "g")
end
hold off
end
The "filt.jpg" shows the filtered image. The "skel.jpg" shows the image after applying bwskel(). The "hough.jpg" shows the lines detected by applying hough transform.
Is there any other way to do it or any improvements to my code that works to detect the lines?
Prateek
Prateek on 24 Jan 2023
Hi Husain,
I believe you would have found a solution by now. If that is not the case, I suggest you approach the MathWorks Technical Support for this. Here's the link for it - https://in.mathworks.com/support/contact_us.html?requestedDomain=
Regards,
Prateek

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!