gpt4 book ai didi

matlab - 不使用 hough 函数在 MATLAB 中进行 Hough 变换

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:05 24 4
gpt4 key购买 nike

我在 MATLAB 中找到了 Hough 变换的实现,地址是 Rosetta Code ,但我无法理解它。我还想修改它以显示原始图像和重建的线条(去霍夫)。

感谢任何有助于理解它和消除霍夫的帮助。谢谢

  1. 为什么图像翻转了?

    theImage = flipud(theImage);

  2. 我无法理解规范函数。它的目的是什么,是否可以避免?

编辑:范数只是欧氏距离的同义词:sqrt(width^2 + height^2)

rhoLimit = norm([宽高]);

  1. 有人可以解释 rho、theta 和 houghSpace 的计算方式/原因吗?

    rho = (-rhoLimit:1:rhoLimit);          
    theta = (0:thetaSampleFrequency:pi);

    numThetas = numel(theta);
    houghSpace = zeros(numel(rho),numThetas);
  2. 我将如何去霍夫空间以重新创建线条?

使用恒等(眼睛)函数创建的对角线的 10x10 图像调用函数

theImage = eye(10)
thetaSampleFrequency = 0.1
[rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)

实际功能

function [rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)

%Define the hough space
theImage = flipud(theImage);
[width,height] = size(theImage);

rhoLimit = norm([width height]);
rho = (-rhoLimit:1:rhoLimit);
theta = (0:thetaSampleFrequency:pi);

numThetas = numel(theta);
houghSpace = zeros(numel(rho),numThetas);

%Find the "edge" pixels
[xIndicies,yIndicies] = find(theImage);

%Preallocate space for the accumulator array
numEdgePixels = numel(xIndicies);
accumulator = zeros(numEdgePixels,numThetas);

%Preallocate cosine and sine calculations to increase speed. In
%addition to precallculating sine and cosine we are also multiplying
%them by the proper pixel weights such that the rows will be indexed by
%the pixel number and the columns will be indexed by the thetas.
%Example: cosine(3,:) is 2*cosine(0 to pi)
% cosine(:,1) is (0 to width of image)*cosine(0)
cosine = (0:width-1)'*cos(theta); %Matrix Outerproduct
sine = (0:height-1)'*sin(theta); %Matrix Outerproduct

accumulator((1:numEdgePixels),:) = cosine(xIndicies,:) + sine(yIndicies,:);

%Scan over the thetas and bin the rhos
for i = (1:numThetas)
houghSpace(:,i) = hist(accumulator(:,i),rho);
end

pcolor(theta,rho,houghSpace);
shading flat;
title('Hough Transform');
xlabel('Theta (radians)');
ylabel('Rho (pixels)');
colormap('gray');

end

最佳答案

霍夫变换是一种“投票”方法,其中每个图像点都对图像中某条线(不是线)的存在进行投票。投票是在直线的参数空间中进行的:法向量的极坐标表示。

我们将参数空间离散化,并允许每个图像点建议与通过该点的线兼容的参数。您的每个问题都可以根据代码中如何处理参数空间来解决。 Wikipedia有一篇很好的文章,其中包含可能会澄清事情的示例(如果您有任何概念上的问题)。

针对您的具体问题:

  1. 图像被翻转,原点在右下角。据我所知,这一步在技术上不是必需的。由于离散化问题,它确实在一定程度上改变了结果。 Rosetta Code 上的其他实现不会翻转图像。
  2. rhoLimit 保存极坐标中图像点的最大半径(回想一下向量的范数是它的大小)。
  3. rhotheta 是极坐标平面根据采样率的离散化。 houghSpace 创建一个矩阵,其中包含离散 rho/theta 值的每种可能组合的元素。
  4. 霍夫变换不指定假定线的长度;投票空间中的峰值仅指定直线法向量的极坐标。您可以通过选择峰值并绘制相应的线来“去霍夫”,或者可能通过绘制每条可能的线并将投票数用作灰度权重。不可能从霍夫变换重新创建原始图像,只能重新创建由变换识别的线条(以及您对选票的阈值方案)。

按照问题中的示例生成以下图表。网格线和数据提示光标的位置可能有点误导(尽管“提示”中的变量值是正确的)。由于这是参数空间 的图像而不是图像空间,我们选择的采样率决定了每个变量中的 bin 数量。在此采样率下,图像点与不止一条可能的线兼容;换句话说,我们的线条具有亚像素分辨率,因为在 10x10 的图像中它们不能在没有重叠的情况下绘制。

一旦我们选择了一个峰值,例如与正常 (rho,theta) = (6.858,0.9) 的线相对应的峰值,我们就可以在图像中绘制我们选择的那条线。自动峰值选择,即通过阈值找到高票数线,是它自己的问题 - 您可以在 DSP 中提出关于该主题的另一个问题。或此处关于特定算法。

例如方法见代码和documentation MATLAB 的 houghpeakshoughlines功能。

enter image description here

关于matlab - 不使用 hough 函数在 MATLAB 中进行 Hough 变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9916253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com