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.model.pixel;
31  
32  import org.openimaj.image.MBFImage;
33  
34  /**
35   * Model of an orthotope/hyperrectangle/box in space. Everything inside classifies 
36   * as positive, everything outside as negative.
37   * 
38   * @author Jonathon Hare
39   */
40  public class OrthotopePixelModel extends MBFPixelClassificationModel {
41  	private static final long serialVersionUID = 1L;
42  	protected float [] min;
43  	protected float [] max;
44  	
45  	/**
46  	 * Construct with the given number of dimensions. This 
47  	 * should be equal to the number of bands in the {@link MBFImage}s
48  	 * you wish to classify.
49  	 * @param ndims number of dimensions
50  	 */
51  	public OrthotopePixelModel(int ndims) {
52  		super(ndims);
53  		
54  		min = new float[ndims];
55  		max = new float[ndims];
56  	}
57  
58  	/**
59  	 * Construct with the given number box. The number of dimensions for
60  	 * each coordinate should be equal to the number of bands in the {@link MBFImage}s
61  	 * you wish to classify.
62  	 * 
63  	 * @param minCoords coordinates of the corner of the box with the smallest coordinates.
64  	 * @param maxCoords coordinates of the corner of the box with the largest coordinates.
65  	 */
66  	public OrthotopePixelModel(float [] minCoords, float [] maxCoords) {
67  		super(minCoords.length);
68  		
69  		if (minCoords.length != maxCoords.length)
70  			throw new IllegalArgumentException("minimum and maximum coordinates must have the same number of dimensions.");
71  		
72  		min = minCoords;
73  		max = maxCoords;
74  	}
75  	
76  	@Override
77  	protected float classifyPixel(Float[] pix) {
78  		
79  		for (int i=0; i<ndims; i++) {
80  			if (pix[i] > max[i] || pix[i] < min[i])
81  				return 0;
82  		}
83  		
84  		return 1;
85  	}
86  
87  	@Override
88  	public OrthotopePixelModel clone() {
89  		OrthotopePixelModel newModel = new OrthotopePixelModel(ndims);
90  		
91  		newModel.min = min.clone();
92  		newModel.max = max.clone();
93  
94  		return newModel;
95  	}
96  
97  	@Override
98  	public void learnModel(MBFImage... images) {
99  		//Iterate through all the pixels and learn the bounding box
100 		
101 		for (int i=0; i<ndims; i++) {
102 			min[i] = Float.MAX_VALUE;
103 			max[i] = Float.MIN_VALUE;
104 		}
105 		
106 		for (MBFImage image : images) {
107 			for (int y=0; y<image.getHeight(); y++) {
108 				for (int x=0; x<image.getWidth(); x++) {
109 					Float [] pixel = image.getPixel(x, y);
110 					
111 					for (int i=0; i<ndims; i++) {
112 						if (pixel[i] > max[i]) max[i] = pixel[i];
113 						if (pixel[i] < min[i]) min[i] = pixel[i];
114 					}
115 				}
116 			}
117 		}
118 	}
119 }