Understanding the Hough transform

Local definitions

General definitions

Turn off annoying spelling warnings

Off[General::spell];
Off[General::spell1];

Load necessary packages

<< Graphics`Graphics`;
<< Graphics`Colors`;
<< Graphics`ImplicitPlot`;

Hough transform illustrations

Shorthand notation

dp = DisplayFunction -> $DisplayFunction;

Illustrate Hough space for a single point and a single θ value

SinglePointSingleTheta[{xc_, yc_},  {xmax_,  
            ymax_},  θc_, {ga_, 
            gb_}] := 
Module[{ρ, ρc, ρmin,  ρmax,  θ, θmin,  θmax,  x,  y, g1, g2, g12, g3, 
            g3b, seq1, 
            dn}, 
ρmin = -Sqrt[
                xmax^2  +  
                  ymax^2]; 
ρmax = -ρmin; 
θmin  =  
            0; 
θmax  =  π; 
ρc = 
            xc Cos[θc]  +  yc Sin[θc]  //  
              N; 
dn = 
            DisplayFunction -> Identity; 
g1 = 
            ImplicitPlot[ρc  ==  
                  x Cos[θc]  +  y Sin[θc]  //  N, {x, 0, 
                xmax}, {y, 0, ymax},  
              PlotStyle -> {Red,  Thickness[ .01]}, 
              dn]; 
g2 = 
            Show[Graphics[{Blue, PointSize[ .04], Point[{xc, yc}]}], 
              dn]; 
g3 = 
            Show[Graphics[{Blue, PointSize[ .01], 
                  Point[{θc, ρc}]}], dn]; 
If[
            ga == Null, seq1 = Sequence[g1, g2], , 
            seq1 = Sequence[ga, g1, g2]]; 
g12 = 
            Show[seq1, AspectRatio -> ymax/xmax,  
              PlotRange -> {{0, xmax}, {0, ymax}}, Frame -> True, 
              dn]; 
If[gb == Null, 
            seq1 = Sequence[g3], , 
            seq1 = Sequence[gb, g3]]; 
g3b = 
            Show[seq1, AspectRatio -> 1,  
              PlotRange -> {{θmin -  .1, θmax +  .1}, {ρmin - 1, ρmax + 1}}, Frame -> True]; 
{g12, 
            g3b}
];

Illustrate Hough space for a single point and all possible orientations

SinglePoint[{xc_, yc_}, {xmax_, ymax_}, dθ_,  
          gfirst_: {Null, 
              Null}] := 
Module[{θl}, 
θl = 
            Table[θ, {θ, 0, π, 
                dθ}]; 
Drop[
            FoldList[
              SinglePointSingleTheta[{xc, yc}, {xmax, 
                    ymax}, #2, {gfirst[[1]], #1 // Last}] &, {Null, 
                gfirst[[2]]}, θl], 1]];

Visualize a set of "edge" points

PointSet[l_, {xmax_, ymax_}] := 
Show[
          Graphics[{Magenta, PointSize[ .02], Point  /@  l}], 
          AspectRatio -> ymax/xmax,  
          PlotRange -> {{0, xmax}, {0, ymax}}, Frame -> True, 
          DisplayFunction -> Identity];

Illustrate Hough space for a set of points

SetOfPoints[pts_,  {xmax_,  ymax_},  
          dθ_] := 
Module[{gb, 
            tmp}, 
gb = 
            PointSet[pts, {xmax, ymax}]; 
tmp = 
            FoldList[
              SinglePoint[#2, {xmax, ymax}, 
                  dθ, {gb, #1 // Last  //  Last}] &,  {{Null, 
                  Null}}, pts]; 
Partition[
            Flatten[Drop[tmp, 1]], 2]
];

Illustrations

Define set of edge points and image size

pts  =  {{50, 50}, {80, 80}, {100, 100}, {140, 
            20}};
dims = {160, 120};

Visualize set of edge points

gpts = PointSet[pts, dims];
Show[gpts, dp];

Visualize Hough accumulator: line parameter space (ρ, θ)

Line parameterization: ρ = x cos(θ) + y sin(θ)

Single point

example1 = 
        SetOfPoints[{pts  //  First}, 
          dims, π/100];
Show[GraphicsArray[#],  ImageSize -> 640, dp] &  /@  
        example1;

Set of points

example2 = 
        SetOfPoints[pts,  dims,  π/100];
Show[GraphicsArray[#], ImageSize -> 640, dp] &  /@  
        example2;