File: hough_transform_circle.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (61 lines) | stat: -rw-r--r-- 2,655 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Copyright 2020 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
//
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#include <boost/gil.hpp>
#include <boost/gil/extension/io/png.hpp>
#include <boost/gil/extension/rasterization/circle.hpp>

#include <iostream>
#include <limits>
#include <vector>

namespace gil = boost::gil;

// Demonstrates how to use a Hough transform to identify a circle

// Note this relies on the brute force approach, which today is the only one available in GIL
// The function hough_circle_transform_brute, defined in include/boost/gil/image_processing/hough_transform.cpp,
// accepts a greyscale edge map, the three Hough parameters allowing to do the drawing and the voting,
// an accumulator in the form of an iterator of views of the parameter space and a utility rasterizer to produce the points.
// The example outputs the voting cell of the centre of a circle drawn programmatically.
// See also:
// hough_transform_line.cpp - Hough transform to detect lines

int main()
{
    const std::size_t size = 128;
    gil::gray8_image_t input_image(size, size);
    auto input = gil::view(input_image);

    const std::ptrdiff_t circle_radius = 16;
    const gil::point_t circle_center = {64, 64};
    const auto rasterizer = gil::midpoint_circle_rasterizer{circle_center, circle_radius};
    gil::apply_rasterizer(input, rasterizer, gil::gray8_pixel_t{255});

    const auto radius_parameter =
        gil::hough_parameter<std::ptrdiff_t>::from_step_count(circle_radius, 3, 3);
    const auto x_parameter =
        gil::hough_parameter<std::ptrdiff_t>::from_step_count(circle_center.x, 3, 3);
    const auto y_parameter =
        gil::hough_parameter<std::ptrdiff_t>::from_step_count(circle_center.x, 3, 3);

    std::vector<gil::gray16_image_t> parameter_space_images(
        radius_parameter.step_count,
        gil::gray16_image_t(x_parameter.step_count, y_parameter.step_count));
    std::vector<gil::gray16_view_t> parameter_space_views(parameter_space_images.size());
    std::transform(parameter_space_images.begin(), parameter_space_images.end(),
                   parameter_space_views.begin(),
                   [](gil::gray16_image_t& img)
                   {
                       return gil::view(img);
                   });

    gil::hough_circle_transform_brute(input, radius_parameter, x_parameter, y_parameter,
                                      parameter_space_views.begin(), rasterizer);
    std::cout << parameter_space_views[3](3, 3) << '\n';
}