Hough Transform

Introduction:

The Hough transform is an algorithm that will take a collection of points, and find all the lines on which these points lie.

we define a line as a collection of points that are adjacent and have the same direction.

In order to understand the Hough transform, we will first consider the normal (perpendicular) representation of a line:

If we have a line in our row and column (rc)-based image space, we can define that line by ρ, the distance from the origin to the line along a perpendicular to the line, and θ, the angle between the r-axis and the ρ-line.

Now, for each pair of values of ρ and θ we have defined a particular line.

The range on θ is ±90° and ρ ranges from 0 to sqrt(2)N, where N is the image size.

Next, we can take this ρθ parameter space and quantize it, to reduce our search time,We quantize the ρθ parameter space, by dividing the space into a specific number of blocks.

Hough Space

Each block corresponds to a line, or group of possible lines, with ρ and θ varying across the increment as defined by the size of the block.

The size of these blocks corresponds to the coarseness of the quantization; bigger blocks provide less line resolution.

 

The algorithm used for the Hough consists of three primary steps:

1.                  Define the desired increments on ρ and θ, ∆ρ and ∆θ, and quantize the space accordingly.

2.     For every point of interest (typically points found by edge detectors that exceed some threshold value), plug the values for r and c into the line equation:

Then, for each value of θ in the quantized space, solve for ρ.

3.  For each ρθ pair from step 2, record the rc pair in the corresponding block in the quantized space. This constitutes a hit for that particular block.

When this process is completed, the number of hits in each block corresponds to the number of pixels on the line as defined by the values of ρ and θ in that block.

Hough Transform Flowchart

The flowchart is followed until all l(r, c) have been examined.

 

The advantage of large quantization blocks is that the search time is reduced, but the price paid is less line resolution in the image space.

Examining this figure , we can see that this means that the line of interest in the image space can vary more.

Effects of Quantization Block Size for Hough Transform

a. Range of lines included by choice of ∆ρ.

b. Range of lines included by choice of ∆θ.

c. Range of lines included by choice of block size.

One block in the Hough Space corresponds to all the solid lines in this figure—this is what we mean by reduced line resolution.
Next, select a threshold and examine the quantization blocks that contain more
points than the threshold.
Here, we look for continuity by searching for gaps in the line by looking at the
distance between points on the line(the points on a line correspond to points
recorded in the block).
When this process is completed, the lines are marked in the output image.

The program:

My program begin with a menu to choose the type
of input image, if you have a regular image(Grayscale image) you can make edge
detection (Robert, Sobel, Prewitt) and grayscale threshold by the program, and
if you have a binary image you can start from point 3.

Next step, you enter the name
of the image (must be in the program directory), each step you pass the result
is shown, and in the grayscale threshold you can repeat the threshold operation
if the binary image not as you prevent.

In the Hough transform menu you can select a
specified angle or you can input
θ
mean that you enter the angles {-90, -90+∆θ
, -90+2∆θ
………,90}.

How it can be programmed?

 

1-edge detection:

we convolute the masks of the operator with the image at
each pixel, Robert Operator is simple  its like this

out.SetPixel(i,j, abs( input.GetPixel(i,j) 
-
input.GetPixel(i-1,j-1))

+abs(
input.GetPixel(i-1,j)-
input.GetPixel(i,j-1) 
));

And in  Sobel Operator we have two numbers s1,s2
corresponding to the results of the two masks and the edge magnitude =

   

we can implement this algorithm by multiplying a two
dimensional  array contains the values of the mask.

but since the two masks contains zeros so it has no effect
in computing the results (its just take some time from the calculations) so we
can discard these zeros as in this code.

S1=-(input.GetPixel(i-1,j-1)+2*input.GetPixel(i-1,j)+input.GetPixel(i-1,j+1))

+(input.GetPixel(i+1,j-1)+2*input.GetPixel(i+1,j)+input.GetPixel(i+1,j+1))

S2=-(input.GetPixel(i-1,j-1)+2*input.GetPixel(i,j-1)+input.GetPixel(i+1,j-1))

+(input.GetPixel(i-1,j+1)+2*input.GetPixel(i,j+1)+input.GetPixel(i+1,j+1))

by implementing array multiplication and the code after
discard the zeros multiplication I find experimentally that the time is reduced
by about 35%

Prewitt Operator can be implemented in the same way.

Hough Transform:

“Hough Transform” is the main function it takes a binary
image and some parameter of the lines that the user wants to select it, this
mean that if the user want to select the lines that have a slope from
–90,-90+deltaTheta …… to 90 it can enter the value of deltaTheta he want.
Another parameter is deltaRo, to decrease or increase the
line resolution as we talk above.
connectDistance parameter is to select the maximum number in
pixels in the gap that cut the line that the program can connect them.
pixelsCcount (threshold) is the minimum number of line
pixels, if the line contain number of pixels less than this number its not
selected .
theta is the angle
of the lines that the user want to select them (he enter one angle at a time
then the result shown and if he want to select another angles, he can.).

How to implement Hough transform algorithm?

 

1-first of all we must allocate the Hough space witch has a
width of 180/deltaTheta an a height of chord/deltaRo.

The algorithm does not clear what we do if

ρ
has negative value, so we
can ignore these values or allocate another Hough space for the negative values.
which is the best ignore them or allocate to them?

If we have this image:

Angle = -45o

If ρ2
= x => ρ1=
-x.

So the first line ignored !!

If we put – ρ

block pixels and +ρ

block pixels in the same block this mean that these tow lines becomes on
line !!  .

I confirm experimentally
from these assumption and I found that actually that some lines are losses if I
merge the –ve and +ve ρ blocks.

So we must allocate two deferent blocks to the +ve
ρ and -ve ρ
pixels.

This
mean that the dimensions the height of Hough space become 2*chord/deltaRo wher
chord =  if the image has a dimensions of NxN.

 

Implementation of Hough blocks:

 

Hough block can be implemented by a two dimensional array of
linked list of points, this mean that each Hough block contain the value of the
first pint lies in this Ro and Theta and contain a pointer that points to the
next point in this block.

If the block contain no points the value of x,y in this
block should be –1,the last point in the linked list points must be NULL, so it
must be initialized.

struct
LinkedPoint{

int
X,Y;

LinkedPoint *nextPoint;

};

since I use malloc() function which is allocate one
dimensional array of bytes so I convert Hough space from two dimension to one
dimension, and I can reach any block by a simple equation.
If  Hough space contain ‘roNum’ of rows and
thetaNum of columns I can reach HS[x][y]=HS[x*yheatNum + y];

Before any point added to a Hough block we must allocate
some memory bytes by the function:

tempLinkedPoint->nextPoint=(LinkedPoint* )malloc(12);//12
byte

tempLinkedPoint=tempLinkedPoint->nextPoint;

tempLinkedPoint->X=i;

tempLinkedPoint->Y=j;

tempLinkedPoint->nextPoint=NULL;

after we end the addition loop we must check if the gap
between each two points(neighbors in the linked list) is less than the gap that
the user select it, we repeat this step and go forward to the next point … until
the we find a large gap then we count the previous points if there count is more
than the threshold that selected by the user we draw a line between the first
point and the last point and start the gap checking from the next point and its
next and start the count again.

Finally if we not find any large gap and the points count is
more than the user selected number we connect these points.

if
(hough[index].X!=-1)  //if its –1 =>there is no points

{

counter=1;

tempLinkedPoint=&hough[index];

startPoint=&hough[index];

while (tempLinkedPoint->nextPoint!=NULL)

{

x1=tempLinkedPoint->X;

y1=tempLinkedPoint->Y;

x2=tempLinkedPoint->nextPoint->X;

y2=tempLinkedPoint->nextPoint->Y;

actualDistance=sqrt(pow((x2-x1),2)+pow((y2-y1),2));

if(actualDistance<=connectDistance)

counter+=1;

else

{

if(counter>=pixelsCount)

{

t1=startPoint;

DrawLine(transImg,t1->X,t1->Y

,tempLinkedPoint->X,tempLinkedPoint->Y);

}

startPoint=tempLinkedPoint->nextPoint;//the
new line

counter=1;

}

tempLinkedPoint=tempLinkedPoint->nextPoint ;

}

if(counter>=pixelsCount)          //rech to the
end

{

t1=startPoint;

DrawLine(transImg,t1->X,t1->Y

,tempLinkedPoint->X,tempLinkedPoint->Y);

}

}

how to draw the line?

This function “DrawLine ” simply draw a line between two
given points by select

the point set that lies in the line.

This algorithm from “INTRODUCTION TO COMPUTER GRAPHICS” book

and have some changes.

void
DrawLine(CImage* transImg,int x1,int
y1,int x2,int
y2)

{

int dx=x2-x1;

int dy=y2-y1;

float m;

if (abs(dx)>abs(dy))

{

int xInc =dx/abs(dx);

m=(float)dy/fabs(dx);

int X;

float Y=(float)y1;

for(X=x1;X!=x2;X+=xInc)

{

transImg->SetPixelFast(X,GetIntValue(Y),1);

Y+=m;

}

}

else

{

int yInc =dy/abs(dy);

m=(float)dx/fabs(dy);

float X=(float)x1;

int Y;

for(Y=y1;Y!=y2;Y+=yInc)

{

transImg->SetPixelFast(GetIntValue(X),Y,1);

X+=m;

}

transImg->SetPixelFast(GetIntValue(X),Y,1);

}

}

Experimental Example:

Regular image

Robert edge detector applied

Threshold at gray level 75

Hough output
ρ
=1 , ∆θ
=1,threshold = 12 and max distance connected = 4

We
see that the separated points is discarded and we took only the lines with given
parameters

Another example:

Binary image cointains lines
and some noise

The output image after Hough
transform with Threshold=20, distance=10, DeltaRo=0.5, Theta=45o
The user input the
angle =45 but the program the perpendicular so it add 90 and invert
it because the image orign is from upper left not lower left.

Theta=45o

And Theta=0o

Theta = 45, 0 , -45

Theta = 45, 0 , -45 , 90

Theta = 45, 0 , -45 , 90 ,120

   
 

The output if we use

Threshold=20, distance=10, DeltaRo=0.5
, deltaTheta = 5o

 

Hough Transform Source Code(VC++) (
Download )

Hough Transform Console Application

Sample Pics

Image Processing Library tools (needed)


正在加载中,请稍候…

Hough Transform的更多相关文章

  1. Hough Transform直线检测

    本文原创,如转载请注明出处. Hough Transform 是一种能提取图像中某种特定形状特征的方法,可以将其描述成一种把图像空间中的像素转换成Hough空间中直线或曲线的一种映射函数.通过利用Ho ...

  2. 第三章 霍夫变换(Hough Transform)

    主要内容: 霍夫变换的作用 霍夫变换检测直线的原理 霍夫变换检测圆的原理 OpenCV中的霍夫变换 1.霍夫变换检测直线原理 霍夫变换,英文名称Hough Transform,作用是用来检测图像中的直 ...

  3. 概率霍夫变换(Progressive Probabilistic Hough Transform)原理详解

    概率霍夫变换(Progressive Probabilistic Hough Transform)的原理很简单,如下所述: 1.随机获取边缘图像上的前景点,映射到极坐标系画曲线: 2.当极坐标系里面有 ...

  4. Matlab 霍夫变换 ( Hough Transform) 直线检测

    PS:好久没更新,因为期末到了,拼命复习中.复习久了觉得枯燥,玩玩儿霍夫变换直线检测 霍夫变换的基本原理不难,即便是初中生也很容易理解(至少在直线检测上是这样子的). 霍夫变换直线检测的基本原理:(不 ...

  5. Hough transform(霍夫变换)

    主要内容: 1.Hough变换的算法思想 2.直线检测 3.圆.椭圆检测 4.程序实现 一.Hough变换简介 Hough变换是图像处理中从图像中识别几何形状的基本方法之一.Hough变换的基本原理在 ...

  6. 霍夫变换(hough transform)

    x-y轴坐标:y=kx+b k-b轴坐标:b=-xk+y θ-r轴坐标:

  7. 灰度图像--图像分割 霍夫变换(Hough Transform)--直线

    学习DIP第50天 转载请标明本文出处:http://blog.csdn.net/tonyshengtan ,出于尊重文章作者的劳动,转载请标明出处!文章代码已托管,欢迎共同开发:https://gi ...

  8. 霍夫变换(Hough Transform)

    霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进算法.最基本的霍夫变换是从黑白图像中检测直线(线段). 我们先看这样一个问题: 设已知一黑白图像上画了一条直线,要求出这 ...

  9. OpenCV2马拉松第22圈——Hough变换直线检測原理与实现

    计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/27220445 收入囊中 Hough变换 概率Ho ...

随机推荐

  1. 开源磁力搜索爬虫dhtspider原理解析

    开源地址:https://github.com/callmelanmao/dhtspider. 开源的dht爬虫已经有很多了,有php版本的,python版本的和nodejs版本.经过一些测试,发现还 ...

  2. 用RxJava处理嵌套请求

    用RxJava处理嵌套请求 互联网应用开发中由于请求网络数据频繁,往往后面一个请求的参数是前面一个请求的结果,于是经常需要在前面一个请求的响应中去发送第二个请求,从而造成"请求嵌套" ...

  3. 【MVVMLight小记】二.开发一个简单图表生成程序附源码

    上一篇文章介绍了怎样快速搭建一个基于MVVMLight的程序http://www.cnblogs.com/whosedream/p/mvvmlight1.html算是简单入门了下,今天我们来做一个稍许 ...

  4. Scala入门之控制结构

    package com.dtspark.scala.basics /** * Scala中的基本控制结构有顺序.条件和循环三种方式,这个其它的JVM语言是一致的,但是Scala也有一些高级的流程控制结 ...

  5. CSS3属性border-radius绘制多种多样的图形

    border-radius,国内翻译成圆角,你可能以为这个属性就是用来画圆角,没错,但是除此之外,它还可以做点别的事情.radius其实指的是边框所在圆的半径,这个CSS3属性不仅能够创建圆角,还可以 ...

  6. 贪吃蛇C#和JAVA实现

    using System; using System.Windows.Forms; using System.Drawing; class Window : Form { Point[] a = ]; ...

  7. 我的一个小作品 android App ---校园资讯助手

        软件主界面采用Fragment+ViewPager组成.在点开后将会自动对学校新闻页面使用URl类来抓取,然后对网页中的信息提取,使用WebView来loadData在主界面上面显示, 为了使 ...

  8. selector 和 shape结合使用

    <?xml version="1.0" encoding="utf-8"?><selector xmlns:android="htt ...

  9. AOP 注入失败的问题

    启用了AOP 后,报这样的类似错误: Error creating bean with name 'bpmpSysUserService': Injection of autowired depend ...

  10. 【HDU 5832】A water problem(大数取模)

    1千万长度的数对73和137取模.(两个数有点像,不要写错了) 效率要高的话,每15位取一次模,因为取模后可能有3位,因此用ll就最多15位取一次. 一位一位取模也可以,但是比较慢,取模运算是个耗时的 ...