Hough变换R PET包检测线

时间:2015-02-02 21:19:37

标签: r hough-transform

我有如下图像,我想使用PET包的hough tranform检测图像中的线条。我需要帮助来了解如何从该图像中获取该行。

library("PET", lib.loc="~/R/win-library/3.1")
library("raster", lib.loc="~/R/win-library/3.1")

p=matrix(diag(100), 100)
library(raster)
r <- raster(p)
plot(r)
abc=hough(p)

viewData(list(p, abc$hData), list("Phantom", "Hough transformed phantom"))

我如上所述应用了霍夫变换。运行最后一行后得到的原始图像和图像如下enter image description here enter image description here

有关如何获取线条坐标(来自原始图像)的任何输入?我知道第二张图像右侧窗格中的白点代表该行。该线是使用Polar Cordinate系统绘制的。但我不知道如何使用第二个图像来获得原始线的坐标

我查看了PET软件包的文档,但发现很难理解:(我运行了他们的示例代码,但我没有理解它

=============================================== ===============================

我按照用户NicE的评论给出了建议,并更新了我的代码,如下所示

library("PET", lib.loc="~/R/win-library/3.1")
library("raster", lib.loc="~/R/win-library/3.1")

#p=matrix(diag(1000), 1000)
p=matrix(rep(0,10000), 100, 100)
# for (i in 1:100)
# {p[i,100-i+1]=1
# }
for (i in 1:100)
{p[i,50]=1
}


# library(raster)
# r <- raster(p)
# plot(r)
abc=hough(p)

maxPoint<-which(abc$hData==max(abc$hData),arr.ind=T)
library(pracma)
a<-cot(maxPoint[1,"row"]*pi/180)
b<-maxPoint[1,"col"]/sin(maxPoint[1,"row"]*pi/180)
a
b
par(pty="s")
par(mfrow=c(1,2))
#image(r, main="org")
image(p,main="original")
image(abc$hData, main="Houghmatrix")

a和b的新值是否正确?我觉得b应该是50(原始线与(0,0)的垂直距离)。我做错了什么?

我还想知道为什么abc $ hData有181行和143列。我可以想象181行与PI弧度有关,是180度。但我对143列没有任何线索......

=============================================== ======================== 更新2 如果我更新我的原始矩阵,因为我觉得我得到了奇怪的答案。我得到= -57.6和b = 1786.12。

p=matrix(rep(0,10000), 100, 100)

for (i in 1:100)
{p[80,i]=1
}

1 个答案:

答案 0 :(得分:1)

一旦你对数据进行了Hough变换,找到矩阵最大值的索引(假设你只有一行):

maxPoint<-which(abc$hData==max(abc$hData),arr.ind=T)

如果您知道只有一条线,您也可以取所有maxPoit的平均值。在你的情况下,你得到这个:

    row col
[1,] 137  72

您还需要该功能的默认参数

houghParam&LT; -unlist(ABC $报头)

这些给你RhoMin,ThethaMin,DeltaMin和DeltaRho,这两个变量的增量。有了这些,你可以从矩阵坐标中得到rho和theta。

theta=(maxPoint[1,"row"]-1)*houghParam["DeltaXY1"]+houghParam["XYmin1"]
rho=(maxPoint[1,"col"]-1)*houghParam["DeltaXY2"]+houghParam["XYmin2"]

如果该行的等式为y=ax+b,您可以使用以下内容获取ab

library(pracma)
a<--cot(theta)
b<-(rho)/sin(theta)

此外,在hough的手册页中,他们声明他们将图像的中心视为(0,0)点。

有关数学解释,请查看Wikipedia page of Hough Transform ...

编辑:更改了公式并删除了一些错误信息

总代码为:

library(PET)
library(pracma)

a=matrix(rep(0,10000), 100, 100)
for (i in 1:100)
{a[i,60]=1
}

d=matrix(rep(0,10000), 100, 100)
for (i in 1:100)
{d[60,i]=1
}

e=matrix(diag(100), 100)

getLineHough<-function(p){
abc=hough(p)
#get the brightest point in the hough tranform
maxPoint<-which(abc$hData==max(abc$hData),arr.ind=T)
#if there is only one line, can average the results in case there are several brightest points
maxPoint<-apply(maxPoint,2,mean)

houghParam<-unlist(abc$Header)

theta=(maxPoint[1]-1)*houghParam["DeltaXY1"]+houghParam["XYmin1"]
rho=(maxPoint[2]-1)*houghParam["DeltaXY2"]+houghParam["XYmin2"]

a<--cot(theta)
b<-rho/sin(theta)


par(mfrow=c(1,2))
image(p,main="original")
#add the predicted lines, also have to change the slope and intercept because
#the origin of the plot function is not the center of the image the bottom left corner
if(theta==0){
        abline(v=(rho+50)/100)
} else{
        abline((b+50-a*50)/100,a)
}
image(abc$hData, main="Houghmatrix")
}

getLineHough(a)
getLineHough(d)
getLineHough(e)

Edit2:文档并没有真正说出矩阵第一行的值是什么。由于有181行,它应该从0开始而不是1 * houghParam [&#34; DeltaXY1&#34;]。相应地更改了代码

Edit3:将代码转换为函数,并将预测的行添加到图中