Hough line detection

  • Include <mln/transforms/hough_lines.hpp>

image2d<int> hough_lines(const image2d<bool> &input, std::span<float> angles, image2d<int> *out = nullptr);

Compute the hough transform a binary image and returns the vote image

Parameters:
  • input – The input binary image

  • angles – The angles (in radians) for which to vote (of length W)

  • out – (optional) The output image of size (W × H) where the vote will be added. It is useful for example if you want α and α ± π/2 to vote for the same thing.

Returns:

The output image of size (W × H) where H is the length of the diagonal

Rtype:

image2d<int>

std::vector<HoughLine> hough_lines_detect_peaks(const image2d<int> &acc, std::span<float> angles, float intensity_reject = 0.5f, int min_sep_distance = 5, int min_sep_angle = 5, int max_num_peaks = 10);

Detect the peaks in a Hough vote image

Parameters:
  • acc – The vote image of size (W x H)

  • angles – The angles used to generate the vote image (of length W)

  • instensity_reject – The percentage of the global max peak value to consider the local max as a peak

  • min_sep_distance – Size of the Non-Maximum-Suppression (NMS) window (units of radius in pixels)

  • min_sep_angle – Size of the Non-Maximum-Suppression (NMS) window (units of angles)

  • max_num_peaks – The maximum number of peaks returned

Returns:

The peaks of the vote image ordered by vote

Rtype:

std::vector<HoughLine>

std::vector<HoughLineAngle> hough_lines_detect_peak_angles(const image2d<int> &acc, std::span<float> angles, float intensity_reject = 0.5f, int min_sep_distance = 5, int min_sep_angle = 5);

Detect the main angles in a Hough vote image

Parameters:
  • acc – The vote image of size (W x H)

  • angles – The angles used to generate the vote image (of length W)

  • instensity_reject – The percentage of the global max peak value to consider the local max as a peak

  • min_sep_distance – Size of the Non-Maximum-Suppression (NMS) window (units of radius in pixels)

  • min_sep_angle – Size of the Non-Maximum-Suppression (NMS) window (units of angles)

Returns:

The main directions in the image

Rtype:

std::vector<HoughLineAngle>

Usage

Retrieve straight lines in images

    std::vector<float> angles =
    {-0.01745329, -0.01570796, -0.01396263, -0.0122173 , -0.01047198, //
     -0.00872665, -0.00698132, -0.00523599, -0.00349066, -0.00174533, //
      0.        ,  0.00174533,  0.00349066,  0.00523599,  0.00698132, //
      0.00872665,  0.01047198,  0.0122173 ,  0.01396263,  0.01570796, //
      0.01745329};



    auto votes = mln::transforms::hough_lines(input, angles);
    auto peaks = mln::transforms::hough_lines_detect_peaks(votes, angles, .5f, 10, 15);

    for (auto p : peaks)
      fmt::print("angle={}; radius={}; count={}\n", p.angle * 180 / M_PI, p.radius, p.count);

    mln::image2d<argb8> out(input.width(), input.height(), mln::image_build_params { .border = 0, .init_value = {} });
    mln::transform(input, out, [](bool x) { return !x ? argb8{255,255,255,255} : argb8{0,0,0,255}; });
    render_lines(out, peaks, argv[2]);

(Full code: /snippets/hough_lines.cpp)

../_images/staff_lines.png ../_images/staff_hough_lines.png

Input image

Hough lines detected (super-imposed)