Cómo utilizar la transformada de Hough para detectar la línea recta más larga en una imagen binaria
I = imread('E:\pic\2\number\4.bmp'); rotI = imrotate(I, -4, 'crop'); rotar la imagen 33°
subplot(221),
fig1 = imshow(I); la imagen rotada se dibuja en la figura
BW = edge(I, 'canny'); uso el método astuto para detectar el límite objetivo
subplot(222),
imshow(BW); Muestra la imagen binaria del límite
[H, theta, rho] = hough(BW); p>imshow( imadjust(mat2gray(H)), [], 'XData', theta, 'YData', rho,...
'InitialMagnification', 'fit'); Mostrar la transformación transformada matriz
xlabel('\theta (grados)'), ylabel('\rho');
eje en, eje normal, espera;
tabla de mapeo de colores colormap(hot)
P = houghpeaks(H, 5, 'threshold', ceil(0.3*max(H(:)))); p>x = theta(P(:, 2));
y = rho(P(:, 1));
plot(x, y, 's', 'color' , 'black'); traza los puntos extremos
lines = houghlines(BW, theta, rho, P, 'FillGap',.
5, 'MinLength', 7); Devuelve el punto final de la línea
subplot(224),
imshow(rotI), espera
max_len = 0;
para k = 1: longitud(líneas)
xy = [líneas(k).punto1; líneas(k).punto2];
plot(xy(:, 1),xy( :,2),'LineWidth',2,'Color','green');Línea detectada
plot(xy(1,1),xy(1,2), 'x',' LineWidth', 2,...
'Color', 'amarillo'); punto final inicial
plot(xy(2,1),xy(2) ,2), 'x', 'LineWidth', 2,...
'Color', 'Rojo' punto final
len = norm(lines(k)); .point1 - líneas( k).point2); La longitud de la línea
if (len gt; max_len)
max_len = len Determinar la línea más larga
<; p> xy_long = xy;fin
fin
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,...
'color ', 'cian'); dibuja la línea más larga