Task

Implement the Hough transform, which is used as part of feature extraction with digital images.

It is a tool that makes it far easier to identify straight lines in the source image, whatever their orientation.

The transform maps each point in the target image, 



(
ρ
,
θ
)


{\displaystyle (\rho ,\theta )}

, to the average color of the pixels on the corresponding line of the source image (in 



(
x
,
y
)


{\displaystyle (x,y)}

-space, where the line corresponds to points of the form 



x
cos
⁡
θ
+
y
sin
⁡
θ
=
ρ


{\displaystyle x\cos \theta +y\sin \theta =\rho }

). The idea is that where there is a straight line in the original image, it corresponds to a bright (or dark, depending on the color of the background field) spot; by applying a suitable filter to the results of the transform, it is possible to extract the locations of the lines in the original image.

 Sample PNG image to use for the Hough transform.
The target space actually uses polar coordinates, but is conventionally plotted on rectangular coordinates for display. There's no specification of exactly how to map polar coordinates to a flat surface for display, but a convenient method is to use one axis for 



θ


{\displaystyle \theta }

 and the other for 



ρ


{\displaystyle \rho }

, with the center of the source image being the origin.

There is also a spherical Hough transform, which is more suited to identifying planes in 3D data.




import lenientops, math
import grayscale_image
 
const White = 255
 
func houghTransform*(img: GrayImage; hx = 460; hy = 360): GrayImage =
  assert not img.isNil
  assert hx > 0 and hy > 0
  assert (hy and 1) == 0, "hy argument must be even"
 
  result = newGrayImage(hx, hy)
  result.fill(White)
 
  let rMax = hypot(img.w.toFloat, img.h.toFloat)
  let dr = rMax / (hy / 2)
  let dTh = PI / hx
 
  for y in 0..<img.h:
    for x in 0..<img.w:
      if img[x, y] == White: continue
      for iTh in 0..<hx:
        let th = dTh * iTh
        let r = x * cos(th) + y * sin(th)
        let iry = hy div 2 - (r / dr).toInt
        if result[iTh, iry] > 0:
          result[iTh, iry] = result[iTh, iry] - 1
 
 
when isMainModule:
  import nimPNG
  import bitmap
 
  const Input = "Pentagon.png"
  const Output = "Hough.png"
 
  let pngImage = loadPNG24(seq[byte], Input).get()
  let grayImage = newGrayImage(pngImage.width, pngImage.height)
 
  # Convert to grayscale.
  for i in 0..grayImage.pixels.high:
    grayImage.pixels[i] = Luminance(0.2126 * pngImage.data[3 * i] +
                                    0.7152 * pngImage.data[3 * i + 1] +
                                    0.0722 * pngImage.data[3 * i + 2] + 0.5)
 
  # Apply Hough transform and convert to an RGB image.
  let houghImage = grayImage.houghTransform().toImage()
 
  # Save into a PNG file.
  # As nimPNG expects a sequence of bytes, not a sequence of colors, we have to make a copy.
  var data = newSeqOfCap[byte](houghImage.pixels.len * 3)
  for color in houghImage.pixels:
    data.add([color.r, color.g, color.b])
  discard savePNG24(Output, data, houghImage.w, houghImage.h)