Curve detection using matlab

5 views (last 30 days)
Joseph Cybul
Joseph Cybul on 26 Oct 2020
Commented: Joseph Cybul on 27 Oct 2020
Hello, I have the following image:
And im trying to find the line that goes trough the center of the red line, like this:
Any suggestions?

Accepted Answer

Stephan
Stephan on 26 Oct 2020
Edited: Stephan on 26 Oct 2020
here is an approach with only using Matlab basic functions:
% Open image file
I = imread('image.jpeg');
% Use Green channel for further opearations
I = I(:,:,2);
% Binarize Image
I(I>20) = 255;
I(I~=255) = 0;
% Preallocate vector of line middle coordinates
mid = 255*ones(size(I,1),1);
% loop through and find the mid of every single line
% and make a white dot at the middle position
for k = 1:numel(mid)
mid(k) = floor(median(find(I(k,:)==0)));
try
I(k,mid(k)) = 255;
catch
end
end
% show image
imshow(I)
The cordinates of the middle line are saved in mid and are NaN at all lines where no line exists-
Edit:
We can also do this without a for loop:
% Open image file
I = imread('image.jpeg');
% Use Green channel for further opearations
I = I(:,:,2);
% Binarize Image
I(I>20) = 255;
I(I~=255) = 0;
% remove content without line
I(255*size(I,2) == sum(I,2),:) = [];
% find black pixels
[r,c] = find(I==0);
% find mid in x-direction
Gx = findgroups(r);
mid_x = sub2ind([size(I,1), size(I,2)], (1:size(I,1))', floor(splitapply(@median,c,r)));
% mark the mid-line white
I(mid_x) = 255;
% show image
imshow(I)
  1 Comment
Joseph Cybul
Joseph Cybul on 27 Oct 2020
Thanks a lot for the quick anwser it worked very well!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!