Detect longest line in an image
Mostrar comentarios más antiguos
Hi,
I would like to detect the longest line in an image. Does anyone know how I can detect this line?
Regards,
Mihael
2 comentarios
Akira Agata
el 8 de Nov. de 2017
Could you upload a sample image?
Mihael Rakic
el 8 de Nov. de 2017
Respuesta aceptada
Más respuestas (1)
Akira Agata
el 9 de Nov. de 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 comentarios
Mihael Rakic
el 10 de Nov. de 2017
Mihael Rakic
el 11 de Nov. de 2017
Akira Agata
el 11 de Nov. de 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
el 5 de Dic. de 2017
Image Analyst
el 5 de Dic. de 2017
You need a ... line continuation indicator at the end of this line:
stats.BoundingBox(1),stats.BoundingBox(2)+stats.BoundingBox(4);
Mihael Rakic
el 5 de Dic. de 2017
Editada: Mihael Rakic
el 9 de Dic. de 2017
Image Analyst
el 9 de Dic. de 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
el 10 de Dic. de 2017
Image Analyst
el 10 de Dic. de 2017
Yes, just use sprintf() and msgbox or helpdlg:
message = sprintf('The values are.....
uiwait(helpdlg(message));
Mihael Rakic
el 10 de Dic. de 2017
Image Analyst
el 10 de Dic. de 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
el 11 de Dic. de 2017
Categorías
Más información sobre Region and Image Properties en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
