From image to straight lines: Hough transform

Note: This notebook assumes that all definitions in ipcv_definitions.nb have been evaluated first. It also assumes that the directory in the Set image directory points to an image directory with the following images: part192x144.ppm bills256x256 and sahara200x150.ppm.

Set image directory

If[StringQ[$ImagePath], $path = $ImagePath,  $path = "."]; 
    SetDirectory[$path];

Example #1: straight lines in image

Load gray-scale image

img = 
        ColorToGrayScale[
          ReadImage["part192x144.ppm"]];
scale  =  2;

Display image

ShowImage[img, scale];

Gaussian-smooth image

filterSize = 5;

n = filterSize;
gf = GaussianFilter[n, (n - 1)/4. ];
smooth = Mask[img, gf];
ShowImage[smooth, scale];

Sobel gradient image

Sobel row gradient image (magnitude)

sr = Mask[smooth, SobelRow[]]  //  N;
ShowImage[sr, scale,  .8  Max[sr]];

Sobel column gradient image (magnitude)

sc = 
        Mask[smooth, SobelColumn[]]  //  N;
ShowImage[sc, scale,  .8  Max[sc]];

Sobel gradient image

gradient = Sqrt[sc^2 +  sr^2];
ShowImage[gradient, scale,  .8  Max[gradient]];

Threshold image

Evaluate different thresholds

tst = ThresholdImage[gradient, #] &  /@  
          Table[t, {t, 10, 250, 10}];
ShowImage[#, scale] &  /@  tst;

Pick threshold

threshold = 200;

timg1  =  
        ThresholdImage[gradient, threshold];
ShowImage[timg1, scale];

Median filter threshold image

timg2 = MedianFilter[timg1, 1];
timg = MedianFilter[timg2, 1];
ShowImage[timg, scale];

Find straight lines (through Hough transform)

Retain only the top (Subscript[n, lines])

nlines = 20;

Compute the top (Subscript[n, lines])lines (through Hough transform)

Timing[sl = StraightLines[timg, nlines, {100, 100}];]

Visualize top (Subscript[n, lines])lines over thresholded image

ShowStraightLines[timg, #, 
            ImageSize -> Dimensions[timg][[2]]*scale] &  /@  
        sl;

Visualize top (Subscript[n, lines])lines over original image

ShowStraightLines[img, #, 
            ImageSize -> Dimensions[timg][[2]]*scale] &  /@  
        sl;

Visualize Hough accumulator

Compute Hough accumulator matrix

Timing[ha = HoughAccumulator[timg, {100, 100}];]

Display  Hough accumulator matrix

ShowHoughAccumulator[timg, ha];

Example #2: straight lines in image

Load gray-scale image

img = 
        ColorToGrayScale[
          ReadImage["bills256x256.ppm"]];
scale  =  1;

Display image

ShowImage[img, scale];

Gaussian-smooth image

filterSize = 5;

n = filterSize;
gf = GaussianFilter[n, (n - 1)/4. ];
smooth = Mask[img, gf];
ShowImage[smooth, scale];

Sobel gradient image

Sobel row gradient image (magnitude)

sr = Mask[smooth, SobelRow[]]  //  N;
ShowImage[sr, scale,  .8  Max[sr]];

Sobel column gradient image (magnitude)

sc = 
        Mask[smooth, SobelColumn[]]  //  N;
ShowImage[sc, scale,  .8  Max[sc]];

Sobel gradient image

gradient = Sqrt[sc^2 +  sr^2];
ShowImage[gradient, scale,  .8  Max[gradient]];

Threshold image

Evaluate different thresholds

tst = ThresholdImage[gradient, #] &  /@  
          Table[t, {t, 10, 250, 10}];
ShowImage[#, scale] &  /@  tst;

Pick threshold

threshold = 200;

timg1  =  
        ThresholdImage[gradient, threshold];
ShowImage[timg1, scale];

Median filter threshold image

timg2 = MedianFilter[timg1, 1];
timg = MedianFilter[timg2, 1];
ShowImage[timg, scale];

Find straight lines (through Hough transform)

Retain only the top (Subscript[n, lines])

nlines = 20;

Compute the top (Subscript[n, lines])lines (through Hough transform)

Timing[sl = StraightLines[timg, nlines, {100, 100}];]

Visualize top (Subscript[n, lines])lines over thresholded image

ShowStraightLines[timg, #, 
            ImageSize -> Dimensions[timg][[2]]*scale] &  /@  
        sl;

Visualize top (Subscript[n, lines])lines over original image

ShowStraightLines[img, #, 
            ImageSize -> Dimensions[timg][[2]]*scale] &  /@  
        sl;

Visualize Hough accumulator

Compute Hough accumulator matrix

Timing[ha = HoughAccumulator[timg, {100, 100}];]

Display  Hough accumulator matrix

ShowHoughAccumulator[timg, ha];

Example #3: no obvious straight lines in image

Load gray-scale image

img = 
        ReadImage["sahara200x150.ppm"];
scale  =  2;

Display image

ShowImage[img, scale];

Gaussian-smooth image

filterSize = 5;

n = filterSize;
gf = GaussianFilter[n, (n - 1)/4. ];
smooth = Mask[img, gf];
ShowImage[smooth, scale];

Sobel gradient image

Sobel row gradient image (magnitude)

sr = Mask[smooth, SobelRow[]]  //  N;
ShowImage[sr, scale,  .8  Max[sr]];

Sobel column gradient image (magnitude)

sc = 
        Mask[smooth, SobelColumn[]]  //  N;
ShowImage[sc, scale,  .8  Max[sc]];

Sobel gradient image

gradient = Sqrt[sc^2 +  sr^2];
ShowImage[gradient, scale,  .8  Max[gradient]];

Threshold image

Evaluate different thresholds

tst = ThresholdImage[gradient, #] &  /@  
          Table[t, {t, 10, 250, 10}];
ShowImage[#, scale] &  /@  tst;

Pick threshold

threshold = 160;

timg1  =  
        ThresholdImage[gradient, threshold];
ShowImage[timg1, scale];

Median filter threshold image

timg2 = MedianFilter[timg1, 1];
timg = MedianFilter[timg2, 1];
ShowImage[timg, scale];

Find straight lines (through Hough transform)

Retain only the top (Subscript[n, lines])

nlines = 20;

Compute the top (Subscript[n, lines])lines (through Hough transform)

Timing[sl = StraightLines[timg, nlines, {100, 100}];]

Visualize top (Subscript[n, lines])lines over thresholded image

ShowStraightLines[timg, #, 
            ImageSize -> Dimensions[timg][[2]]*scale] &  /@  
        sl;

Visualize top (Subscript[n, lines])lines over original image

ShowStraightLines[ColorToGrayScale[img], #, 
            ImageSize -> Dimensions[timg][[2]]*scale] &  /@  
        sl;

Visualize Hough accumulator

Compute Hough accumulator matrix

Timing[ha = HoughAccumulator[timg, {100, 100}];]

Display  Hough accumulator matrix

ShowHoughAccumulator[timg, ha];