【问题标题】:Hough Transform Equivalent in Octave八度中的霍夫变换等效
【发布时间】:2020-04-01 14:51:42
【问题描述】:

我在下面的MATLAB中附上了霍夫变换的代码:

%Hough Transform to find lines

%Load an Image and Convert to Grayscale to apply canny Filter
im = imread('lines.jpg');
im_gray = rgb2gray(im);
im_edge = edge(im_gray, 'canny');

figure, imshow(im), title('Original Image');
figure, imshow(im_gray), title('Grayscale Image');
figure, imshow(im_edge), title('Canny Filter Edge');

%Apply Hough Transform to Find the Candidate Lines
[accum theta rho] = hough(im_edge);
figure, imagesc(accum, 'xData', theta, 'ydata', rho), title('Hough Accumulator');

peaks = houghpeaks(accum, 100, 'Threshold', ceil(0.6 * max(accum(:))),'NHoodSize', [5,5]);
size(peaks);

%Finding the line segments in the image
line_segs = houghlines(edges, theta, rows, peaks, 'FillGap', 50,'MinLength', 100);

%Plotting
figure, imshow(im), title('Line Segments');
hold on;
for k=1:length(line_segs)
  endpoints = [line_segs(k).point1; line_segs(k).point2];
  plot(endpoints(:,1), endpoints(:,2), 'LineWidth', 2, 'Color','green');
end
hold off;

当我尝试通过将 'hough 更改为 houghtf''houghlines 更改为 hough_line''houghpeaks 更改为 immaximas 在 OCTAVE 中实现相同功能时' 以下列方式:

%Hough Transform to find lines
pkg load image;

%Load an Image and Convert to Grayscale to apply canny Filter
im = imread('lines.jpg');
im_gray = rgb2gray(im);
im_edge = edge(im_gray, 'canny');

figure, imshow(im), title('Original Image');
figure, imshow(im_gray), title('Grayscale Image');
figure, imshow(im_edge), title('Canny Filter Edge');

%Apply Hough Transform to Find the Candidate Lines
[accum theta rho] = houghtf(im_edge); %In Octave and 'hough' in MATLAB
figure, imagesc(accum, 'xData', theta, 'ydata', rho), title('Hough Accumulator');

peaks = immaximas(accum, 100, 'Threshold', ceil(0.6 * max(accum(:))),'NHoodSize', [5,5]);
size(peaks);

%Finding the line segments in the image
line_segs = hough_line(edges, theta, rows, peaks, 'FillGap', 50, 'MinLength', 100);

%Plotting
figure, imshow(im), title('Line Segments');
hold on;
for k=1:length(line_segs)
 endpoints = [line_segs(k).point1; line_segs(k).point2];
 plot(endpoints(:,1), endpoints(:,2), 'LineWidth', 2, 'Color', 'green');
end
hold off;

执行时出现以下错误:

error: element number 3 undefined in return list
error: called from
HoughTransformLines at line 14 column 18

我收到错误消息,指出 'rho' 未定义。 我对 MATLAB 和 Octave 完全陌生。谁能帮我在 Octave 中实现霍夫变换?

【问题讨论】:

    标签: matlab octave


    【解决方案1】:

    我建议对原始代码进行以下更新:

    %Hough Transform to find lines
    pkg load image;
    
    %Load an Image and Convert to Grayscale to apply canny Filter
    im = imread('lines.jpg');
    im_gray = rgb2gray(im);
    im_edge = edge(im_gray, 'canny');
    
    figure 1, imshow(im),      title('Original Image');
    figure 2, imshow(im_gray), title('Grayscale Image');
    figure 3, imshow(im_edge), title('Canny Filter Edge');
    
    %Apply Hough Transform to Find the Candidate Lines
    accum       = houghtf(im_edge);
    theta       = -90:90;
    diag_length = (size(accum)(1) - 1) / 2;
    rho         = -diag_length:diag_length;
    figure 4, imagesc(theta, rho, accum), title('Hough Accumulator');
    
    peaks = houghpeaks(accum, 100, 'Threshold', ceil(0.6 * max(accum(:))), 'NHoodSize', [5,5]);
    
    %Finding the line segments in the image
    line_segs = houghlines(im_edge, theta, rho, peaks, 'FillGap', 50, 'MinLength', 100);
    
    %Plotting
    figure 5, imshow(im), title('Line Segments');
    hold on;
    for k=1:length(line_segs)
        endpoints = [line_segs(k).point1; line_segs(k).point2];
        plot(endpoints(:,1), endpoints(:,2), 'LineWidth', 2, 'Color', 'green');
    end
    hold off;
    

    让我们浏览所有更新并查看它们:

    %Hough Transform to find lines
    pkg load image;
    
    %Load an Image and Convert to Grayscale to apply canny Filter
    im = imread('lines.jpg');
    im_gray = rgb2gray(im);
    im_edge = edge(im_gray, 'canny');
    
    figure 1, imshow(im),      title('Original Image');
    figure 2, imshow(im_gray), title('Grayscale Image');
    figure 3, imshow(im_edge), title('Canny Filter Edge');
    

    只有非常小的变化 - 添加了数字的索引以将它们分成一致的单独窗口(请参阅Multiple Plot Windows)。

    之后,我们将应用 Hough 变换并在 Octave 中恢复“丢失的”thetarho 值:

    %Apply Hough Transform to Find the Candidate Lines
    accum       = houghtf(im_edge);
    theta       = -90:90;
    diag_length = (size(accum)(1) - 1) / 2;
    rho         = -diag_length:diag_length;
    figure 4, imagesc(theta, rho, accum), title('Hough Accumulator');
    

    根据houghtf 函数的文档,它只返回一个累加器,其中的行对应于 rho 值的索引,而列 - 对应于 theta 值的索引。我们如何才能恢复原始的 rho 和 theta 值?好吧,rho 值的数量(accum 矩阵变量中的行)上升到2*diag_length - 1,其中diag_length 是输入图像的对角线长度。知道了这一点,我们应该恢复对角线长度(这是一个相反的动作):diag_length = (size(accum)(1) - 1) / 2。然后我们可以恢复rho 值,从负对角线变为对角线:rho = -diag_length:diag_length。有了 thetas,一切都变得更容易了——它们在pi*(-90:90)/180 的范围内,但我们将使用度数来代替:theta = -90:90。我已经像以前一样添加了figure 的索引,并根据其docs 将调用更改为imagesc - 它应该被称为imagesc (x, y, img)

    之后我们使用houghpeaks 函数获取峰值:

    peaks = houghpeaks(accum, 100, 'Threshold', ceil(0.6 * max(accum(:))), 'NHoodSize', [5,5]);
    

    我们使用houghlines 来获取结果线段(猜测有一些带有变量名称的勘误表):

    line_segs = houghlines(im_edge, theta, rho, peaks, 'FillGap', 50, 'MinLength', 100);
    

    最后是绘图代码——它根本没有改变,因为它工作正常。

    【讨论】:

      【解决方案2】:

      Octave 告诉您 rho 未定义的原因是因为 Matlab 的 hough 函数和 Octave 的 houghtf 函数不是完全等价的。

      以下是 hough 从相应 Mathwork 网页返回的输出参数的描述:

      函数返回rho,原点到直线的距离 沿着垂直于线的向量,θ,角度 x 轴和该向量之间的度数。该函数还返回 标准霍夫变换,H,它是一个参数空间矩阵 其行和列对应于 rho 和 theta 值 分别。

      另一方面,Octave 的 houghtf 只返回矩阵 H:

      结果 H 是一个包含 Hough 变换的 N × M 矩阵。这里, N 是已尝试的 r 的不同值的数量。这是 计算为 2*diag_length - 1,其中 diag_length 是 输入图像的对角线。 M 是不同值的数量 θ。这些可以通过第三个输入参数 arg 设置。这个 必须是实数向量,默认为 pi*(-90:90)/180。

      现在,在您的脚本中,您调用 rho 的唯一位置是在第 15 行,当您尝试显示 Hough 累加器时。 我建议您改为以这种方式绘制累加器:

      figure, imagesc(H),xlabel('xData'),ylabel('ydata'),title('Hough accumulator')
      

      让我知道这是否适合你!

      【讨论】:

      • 它按照您建议的方式来绘制累加器。但我不得不从第 15 行删除“rho”:[accum theta] = houghtf(im_edge); 但仍然出现此错误:error: immaximas: third input argument must be a scalar or an empty matrix error: called from immaximas at line 76 column 5 HoughTransformLines at line 18 column 7
      【解决方案3】:
      [H] = houghtf(edges);
      

      你必须以这种方式传递参数,因为 octave 只返回一个值矩阵。您不能将三个不同的变量分配给一个矩阵。
      所以,给它分配一个变量,你就会得到结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-23
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-26
        • 2014-06-17
        相关资源
        最近更新 更多