Example #2: straight lines in image

Load gray-scale image

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

Display image

ShowImage[img, scale] ;

[Graphics:../HTMLFiles/index_103.gif]

Gaussian-smooth image

filterSize = 5 ;

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

[Graphics:../HTMLFiles/index_106.gif]

Sobel gradient image

Sobel row gradient image (magnitude)

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

[Graphics:../HTMLFiles/index_108.gif]

Sobel column gradient image (magnitude)

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

[Graphics:../HTMLFiles/index_110.gif]

Sobel gradient image

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

[Graphics:../HTMLFiles/index_112.gif]

Threshold image

Evaluate different thresholds

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

[Graphics:../threshold_example2.gif]

Pick threshold

threshold = 200 ;

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

[Graphics:../HTMLFiles/index_142.gif]

Median filter threshold image

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

[Graphics:../HTMLFiles/index_144.gif]

Find straight lines (through Hough transform)

Retain only the top n_lines

nlines = 20 ;

Compute the top n_lineslines (through Hough transform)

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

RowBox[{{, RowBox[{RowBox[{8.11,  , Second}], ,, Null}], }}]

Visualize top n_lineslines over thresholded image

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

[Graphics:../lines_example2a.gif]

Visualize top n_lineslines over original image

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

[Graphics:../lines_example2b.gif]

Visualize Hough accumulator

Compute Hough accumulator matrix

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

RowBox[{{, RowBox[{RowBox[{8.1,  , Second}], ,, Null}], }}]

Display  Hough accumulator matrix

ShowHoughAccumulator[timg, ha] ;

[Graphics:../HTMLFiles/index_199.gif]


Created by Mathematica  (February 5, 2004)