public class HoughLines extends Object implements ImageAnalyser<FImage>, Iterable<Line2d>, Iterator<Line2d>
ImageAnalyser
.
The input image should have the lines to detect zeroed in the image (black).
All other values will be ignored. That means you usually need to invert
images created with edge detectors.
CannyEdgeDetector2 ced = new CannyEdgeDetector2();
FImage i = ced.process( ImageUtilities.readF( new File( 'test.jpg' ) );
HoughLines hl = new HoughLines();
i.inverse().analyse( hl );
double d = hl.calculatePrevailingAngle();
The analyser is iterable over the lines that are found within the
accumulator space. Iterated lines will be returned in strength order.
Once an iterator has been created, the object contains a copy of the
accumulator space until clearIterator()
is called.
You can use the Java 5 for construct:
int maxLines = 20; for( Line2d line: hl ) { System.out.println( "Line: "+line ); if( --maxLines == 0 ) break; } hl.clearIterator();
To convert a bin into a degree, use bin*360d/getNumberOfSegments()
.
To convert a degree into a bin, use degree/360d/getNumberOfSegments()
.
Constructor and Description |
---|
HoughLines()
Default constructor that creates an accumulator space for 360 degrees with a "on value" of 0.0f
|
HoughLines(float onValue)
Constructor that creates a default accumulator space with
a specified value for pixels considered to be "on"
|
HoughLines(int nSegments,
float onValue)
Constructor that creates an accumulator space for the given number
of segments.
|
Modifier and Type | Method and Description |
---|---|
void |
analyseImage(FImage image)
Analyse an image.
|
FImage |
calculateHorizontalProjection()
Calculates a projection across the accumulator space.
|
FImage |
calculateHorizontalProjection(FImage accum)
Calculates a projection across the given accumulator space.
|
double |
calculatePrevailingAngle()
Returns the most frequent angle that occurs within the accumulator
space by calculating a horizontal projection over the accumulator
space and returning the angle with the most votes.
|
double |
calculatePrevailingAngle(FImage accum,
int offset,
double nDegrees)
Returns the most frequent angle that occurs within the given accumulator
space by calculating a horizontal projection over the accumulator
space and returning the angle with the most votes.
|
double |
calculatePrevailingAngle(float minTheta,
float maxTheta)
Returns the most frequent angle that occurs within the given accumulator
space with a given range of angles (specified in degrees)
by calculating a horizontal projection over the given accumulator
space and returning the angle with the most votes.
|
void |
clearIterator()
Remove the temporary objects created during iteration.
|
FImage |
getAccumulator()
Returns the accumulator space.
|
Line2d |
getBestLine()
Returns the top line in the accumulator space.
|
Line2d |
getBestLine(FImage accumulatorSpace,
int offset)
Returns the top line in the given accumulator space.
|
Line2d |
getBestLine(float minTheta,
float maxTheta)
Returns the best line within a certain angular range.
|
List<Line2d> |
getBestLines(int n)
Returns the top n lines from the accumulator space.
|
List<Line2d> |
getBestLines(int n,
FImage accumulatorSpace,
int offset)
Returns the top n lines from the given accumulator space.
|
List<Line2d> |
getBestLines(int n,
float minTheta,
float maxTheta)
Returns the top n lines from the given accumulator space within the range.
|
Line2d |
getLineFromParams(int theta,
int dist,
int x1,
int x2)
From a r,theta parameterisation of a line, this returns a
Line2d
with endpoints at the given x coordinates. |
int |
getNumberOfSegments()
Get the number of directional accumulator bins.
|
boolean |
hasNext() |
Iterator<Line2d> |
iterator() |
Line2d |
next() |
void |
remove() |
void |
setNumberOfSegments(int numberOfSegments)
Set the number of segments used in the accumulator space.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
forEachRemaining
public HoughLines()
public HoughLines(float onValue)
onValue
- value of pixels considered onpublic HoughLines(int nSegments, float onValue)
nSegments
- The number of segments.onValue
- value of pixels considered onpublic void analyseImage(FImage image)
analyseImage
in interface ImageAnalyser<FImage>
image
- The image to process in place.ImageAnalyser.analyseImage(org.openimaj.image.Image)
public FImage getAccumulator()
FImage
public FImage calculateHorizontalProjection()
getNumberOfSegments()
and height of 1. Effectively sums across the distances from origin
in the space such that you end up with a representation that gives you
the strength of the angles in the image irrespective of where those
lines occur.getNumberOfSegments()
x 1public FImage calculateHorizontalProjection(FImage accum)
accum
- The accumulator space to projectpublic double calculatePrevailingAngle()
public double calculatePrevailingAngle(FImage accum, int offset, double nDegrees)
accum
- The accumulator space to useoffset
- The offset into the accumulator of the 0 degree binnDegrees
- The number of degrees covered by the accumulator spacepublic double calculatePrevailingAngle(float minTheta, float maxTheta)
minTheta
- The minimum angle (degrees)maxTheta
- The maximum angle (degrees)public Line2d getBestLine()
public Line2d getBestLine(FImage accumulatorSpace, int offset)
accumulatorSpace
- The accumulator space to look withinoffset
- The number of bins offset from zero degreespublic Line2d getBestLine(float minTheta, float maxTheta)
minTheta
- Minimum angle of the best linemaxTheta
- Maximum angle of the best linepublic List<Line2d> getBestLines(int n, float minTheta, float maxTheta)
n
- The number of lines to returnminTheta
- The minimum angle (degrees)maxTheta
- The maximum angle (degrees)public List<Line2d> getBestLines(int n)
n
- The number of lines to returnpublic List<Line2d> getBestLines(int n, FImage accumulatorSpace, int offset)
n
- The number of lines to returnaccumulatorSpace
- The space to look withinoffset
- The offset into the accumulator of 0 in this spacepublic Line2d getLineFromParams(int theta, int dist, int x1, int x2)
Line2d
with endpoints at the given x coordinates. If theta is 0 this will return
a vertical line between -2000 and 2000 with the x-coordinate the appopriate
distance from the origin.theta
- The angle bin in which the line lies (x in the accumulator space)dist
- The distance bin in which the line lies (y in the accumulator space)x1
- The x-coordinate of the start of the linex2
- The x-coordinate of the end of the lineLine2d
public Iterator<Line2d> iterator()
iterator
in interface Iterable<Line2d>
Iterable.iterator()
public boolean hasNext()
hasNext
in interface Iterator<Line2d>
Iterator.hasNext()
public Line2d next()
next
in interface Iterator<Line2d>
Iterator.next()
public void remove()
remove
in interface Iterator<Line2d>
Iterator.remove()
public void clearIterator()
public void setNumberOfSegments(int numberOfSegments)
numberOfSegments
- Set the number of directional bins in
the accumulator spacepublic int getNumberOfSegments()