八度中的Hough变换等价

时间:2017-12-26 07:41:58

标签: matlab octave

我在MATLAB中附加了 Hough Transform 代码:

%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'来实现OCTAVE时,'houghlines to hough_line''houghpeaks into immaximas '以下列方式:

%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中实现Hough-Transform?

3 个答案:

答案 0 :(得分: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)。

之后,我们将应用霍夫变换并在八度音阶中恢复“丢失的” 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。使用theta,一切都变得更容易-它们在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);

最后还有绘图代码-因为它可以正常工作,所以根本没有更改。

答案 1 :(得分:0)

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

以下是来自相应Mathwork网页的 hough 返回的输出参数的说明:

  

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

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

  

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

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

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

请告诉我这是否适合您!

答案 2 :(得分:-1)

[H] = houghtf(edges);

由于八度仅返回值矩阵,因此您必须以这种方式传递参数。您不能将三个不同的变量分配给矩阵。
因此,为该变量分配一个变量,您将获得结果。

相关问题