Detect longest line in an image
Show older comments
Hi,
I would like to detect the longest line in an image. Does anyone know how I can detect this line?
Regards,
Mihael
Accepted Answer
More Answers (1)
Akira Agata
on 9 Nov 2017
Hi Mihael-san,
Thank you for sharing your picture. By detecting the bottom edge of the center black rectangle, I think you can detect the goal. Here is an example to do this.
% Read image
I = imread('Strafschopgebied.png');
Igray = rgb2gray(I);
% Extract the black area whose Area is close to that of BoundingBox
BW = imbinarize(Igray);
BW = imclearborder(~BW);
stats = struct2table(regionprops(BW));
ratio = stats.Area./(stats.BoundingBox(:,3).*stats.BoundingBox(:,4));
[~, idx] = max(ratio);
stats = stats(idx,:);
% Goal line (= bottom edge of the BoundingBox)
goalLine = [...
stats.BoundingBox(1),stats.BoundingBox(2)+stats.BoundingBox(4);
stats.BoundingBox(1)+stats.BoundingBox(3),stats.BoundingBox(2)+stats.BoundingBox(4)];
% Show the result
figure
imshow(I)
hold on
plot(goalLine(:,1),goalLine(:,2),'c','LineWidth',4)

12 Comments
Mihael Rakic
on 10 Nov 2017
Mihael Rakic
on 11 Nov 2017
Akira Agata
on 11 Nov 2017
Hi Mihael-san,
Using my sample code, the following will give the coordinates of the goalLine.
(x1,y1) = (stats.BoundingBox(1), stats.BoundingBox(2)+stats.BoundingBox(4))
(x2,y2) = stats.BoundingBox(1)+stats.BoundingBox(3), stats.BoundingBox(2)+stats.BoundingBox(4))
Mihael Rakic
on 5 Dec 2017
Image Analyst
on 5 Dec 2017
You need a ... line continuation indicator at the end of this line:
stats.BoundingBox(1),stats.BoundingBox(2)+stats.BoundingBox(4);
Mihael Rakic
on 5 Dec 2017
Edited: Mihael Rakic
on 9 Dec 2017
Image Analyst
on 9 Dec 2017
You can't use parentheses here:
(x1,y1) = (stats.BoundingBox(1), stats.BoundingBox(2)+stats.BoundingBox(4))
(x2,y2) = stats.BoundingBox(1)+stats.BoundingBox(3), stats.BoundingBox(2)+stats.BoundingBox(4))
You'd have to use brackets, but even that wouldn't work. Do it like this:
x1 = stats.BoundingBox(1);
y1 = stats.BoundingBox(2)+stats.BoundingBox(4);
x2 = x1 + stats.BoundingBox(3);
y2 = y1 + stats.BoundingBox(4);
Mihael Rakic
on 10 Dec 2017
Image Analyst
on 10 Dec 2017
Yes, just use sprintf() and msgbox or helpdlg:
message = sprintf('The values are.....
uiwait(helpdlg(message));
Mihael Rakic
on 10 Dec 2017
Image Analyst
on 10 Dec 2017
Well of course you need to learn how to use sprintf. You didn't put in a format specifier! What is x1? Is it a double? If so use %f.
message = sprintf('The value is: %f',x1)
Is it an integer? If so use %d
message = sprintf('The value = %d',x1)
You really need to learn how to use all the percent format specifiers - it will help you immensely in the future.
Mihael Rakic
on 11 Dec 2017
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!