Skip to content

Instantly share code, notes, and snippets.

@maxbeutel
Last active November 23, 2016 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxbeutel/a6b60b04bfef954caa84647e7859dc51 to your computer and use it in GitHub Desktop.
Save maxbeutel/a6b60b04bfef954caa84647e7859dc51 to your computer and use it in GitHub Desktop.
Hough Transform - Voting
// calculate the longest line that can occur in the image (hypotenuse)
let max_line_length = (img_width as f64).hypot(img_height as f64).ceil();
// Create the accumulator matrix, x-axis is angle theta, y-axis is distance rho
let mut accumulator: na::DMatrix<u64> = na::DMatrix::new_zeros(180, max_line_length as usize);
// Flip y axis when reading image, as the image has 0/0 at the top left,
// but it's easier for us to work with when the origin 0/0 is at the bottom left
for y in 0..img_height {
for x in 0..img_width {
if !detect_edge(x, y_flipped) {
continue;
}
for i in 0..3 {
let theta = (i as f64 + 1.0) * 30.0;
let rho = (x as f64) * theta.to_radians().cos() + (y_flipped as f64) * theta.to_radians().sin();
let rho_rounded = rho.round();
accumulator[(theta as usize, rho_rounded as usize)] += 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment