head	1.1;
access;
symbols;
locks; strict;
comment	@// @;


1.1
date	95.01.31.07.17.28;	author rjj;	state Exp;
branches;
next	;


desc
@An implementation of the hough line object, which is essentiall
  a wrapping of a value from the accumulator image and a plotting
  algorithm based on knowledge of the Hough transform.
@


1.1
log
@Initial revision
@
text
@// hough-line.c         Section: 10.3, Line Detection using the Hough Transform
//      Copyright 1995, Center for Computer Vision and Visualization,
//      University of Florida.  All rights reserved.
//
// Define a Hough line, given sufficient information.  Support plotting
//    the line on an image.

#include "hough-line.h"

HoughLine::HoughLine( const IA_Point<int> & hough_point,
		      unsigned int  positive_rows,
		      unsigned int  columns,
		      double max_R ) {
    
    // Assign the private variables their values
    hp  = hough_point;
    rho = (hough_point(0) * 2.0 * max_R / double((2*positive_rows+1)-1))
	  - max_R;
    theta = hough_point(1) * M_PI  / double(columns-1);
}


void
HoughLine::plot( IA_Image<IA_Point<int>,IA_Bit> & image ) {

    // To get the best possible plot it is necessary to consider
    //   three cases:
    //      0     <= theta <  pi/4   : plot as function of y
    //      pi/4  <= theta < 3pi/4   : plot as function of x
    //      3pi/4 <= theta <  pi     : plot as function of y

    // Determine which of the three cases we have and
    //    plot accordingly.

    if ( theta < M_PI/4.0 ) {
	// Plot as a function of second coordinate.
//	plot_indexed( image, 1, tan( theta ) );
	plot_indexed( image, 1 );;

    } else if ( theta < 3.0*M_PI/4.0 ) {
	// Plot as a function of the first cooridnate.
//	plot_indexed( image, 0, tan( theta + M_PI/2.0 ) );
	plot_indexed( image, 0 );
    } else {
	// Plot as a function of second coordinate.
//	plot_indexed( image, 1, tan( theta ) );
	plot_indexed( image, 1 );
    }
}



void
HoughLine::plot_indexed( IA_Image<IA_Point<int>,IA_Bit> & image,
			 unsigned int i_coord ) {

    unsigned int d_coord = (i_coord + 1) % 2;

    double trig[2];
    trig[0] = cos( theta );
    trig[1] = sin( theta );

    IA_Point<int> p(0,0),  // 2d point
	p_inf = image.domain().inf(),
	p_sub = image.domain().sup();

    IA_Bit  one_bit = 1;
    for( int iv = p_inf( i_coord ); iv <= p_sub( i_coord ); iv++ ) {

	p[i_coord] = iv;
	p[d_coord] = nint( (rho - double(iv)*trig[i_coord]) / trig[d_coord] );

	if ( image.domain().contains( p ) ) {
	    image[ p ] = one_bit;
	}
    }
}
@
