View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.processing.extraction;
31  
32  import org.openimaj.image.FImage;
33  import org.openimaj.image.processing.transform.ProjectionProcessor;
34  import org.openimaj.image.processor.ImageProcessor;
35  import org.openimaj.math.geometry.point.Point2dImpl;
36  import org.openimaj.math.geometry.shape.Ellipse;
37  import org.openimaj.math.geometry.shape.Polygon;
38  import org.openimaj.math.geometry.transforms.TransformUtilities;
39  
40  import Jama.Matrix;
41  
42  /**
43   * Extract a polygon from an image into a new image.
44   * The ellipse fitting the polygon is extracted.
45   * The rotation of the ellipse is used to orientate the polygon.
46   * The major and minor axis of the ellipse form the dimensions of the output FImage
47   * The {@link ProjectionProcessor} is used to perform the extraction. Concretely,
48   * the projection processor is set up with a transform matrix combining the rotation of
49   * the ellipse and a translation using the COG of the ellipse.
50   *
51   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
52   *
53   */
54  public class OrientedPolygonExtractionProcessor implements ImageProcessor<FImage>{
55  
56  	private final float background;
57  	private final Ellipse polygonEllipse;
58  
59  	/**
60  	 * @param polygon
61  	 * @param background
62  	 */
63  	public OrientedPolygonExtractionProcessor(final Polygon polygon, final float background) {
64  		this.polygonEllipse = polygon.toEllipse();
65  		this.background = background;
66  	}
67  
68  	@Override
69  	public void processImage(final FImage image) {
70  		image.internalAssign(this.orientedBoundingBoxProjection(image));
71  	}
72  
73  	private FImage orientedBoundingBoxProjection(final FImage image) {
74  		final ProjectionProcessor<Float, FImage> pp = new ProjectionProcessor<Float,FImage>();
75  		Matrix trans = Matrix.identity(3, 3);
76  		trans = trans.times(TransformUtilities.rotationMatrix(-this.polygonEllipse.getRotation()));
77  		trans = trans.times(
78  			TransformUtilities.translateToPointMatrix(
79  					this.polygonEllipse.calculateCentroid(),
80  					new Point2dImpl(0,0))
81  		);
82  		pp.setMatrix(trans);
83  		pp.accumulate(image);
84  		return pp.performProjection(
85  				(int)-this.polygonEllipse.getMajor(),(int)this.polygonEllipse.getMajor(),
86  				(int)-this.polygonEllipse.getMinor(),(int)this.polygonEllipse.getMinor(),
87  				this.background
88  		);
89  	}
90  
91  }