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.feature.dense.binarypattern;
31  
32  import gnu.trove.list.array.TIntArrayList;
33  
34  import org.openimaj.citation.annotation.Reference;
35  import org.openimaj.citation.annotation.ReferenceType;
36  import org.openimaj.feature.FloatFV;
37  
38  /**
39   * Class for extracting histograms of Local Uniform Binary Patterns. Uniform
40   * patterns have less than one 01 transition and one 01 transition when viewed
41   * as a circular buffer.
42   * 
43   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
44   */
45  @Reference(
46  		type = ReferenceType.Article,
47  		author = { "Ojala, T.", "Pietikainen, M.", "Maenpaa, T." },
48  		title = "Multiresolution gray-scale and rotation invariant texture classification with local binary patterns",
49  		year = "2002",
50  		journal = "Pattern Analysis and Machine Intelligence, IEEE Transactions on",
51  		pages = { "971 ", "987" },
52  		month = "jul",
53  		number = "7",
54  		volume = "24",
55  		customData = {
56  				"doi", "10.1109/TPAMI.2002.1017623",
57  				"ISSN", "0162-8828"
58  		})
59  public class LocalUniformBinaryPatternHistogram {
60  	protected int blocksize_x;
61  	protected int blocksize_y;
62  	FloatFV[][] histograms;
63  
64  	/**
65  	 * Construct the extractor with the given block size for each local patch.
66  	 * 
67  	 * @param blocksize_x
68  	 *            the width of the blocks
69  	 * @param blocksize_y
70  	 *            the height of the blocks
71  	 */
72  	public LocalUniformBinaryPatternHistogram(int blocksize_x, int blocksize_y) {
73  		this.blocksize_x = blocksize_x;
74  		this.blocksize_y = blocksize_y;
75  	}
76  
77  	/**
78  	 * Compute the histograms for the given pattern image, encoded with the
79  	 * given number of bits.
80  	 * 
81  	 * @param patternImage
82  	 *            the pattern data
83  	 * @param nbits
84  	 *            the number of bits used to encode the patterns
85  	 */
86  	public void calculateHistograms(int[][] patternImage, int nbits) {
87  		final int height = patternImage.length;
88  		final int width = patternImage[0].length;
89  		final TIntArrayList uniformPatterns = UniformBinaryPattern.getUniformPatterns(nbits);
90  
91  		histograms = new FloatFV[(int) Math.ceil((double) height / (double) blocksize_y)][(int) Math.ceil((double) width
92  				/ (double) blocksize_x)];
93  
94  		for (int y = 0, j = 0; y < height; y += blocksize_y, j++) {
95  			for (int x = 0, i = 0; x < width; x += blocksize_x, i++) {
96  				histograms[j][i] = new FloatFV(uniformPatterns.size() + 1);
97  
98  				for (int yy = y; yy < Math.min(height, y + blocksize_y); yy++) {
99  					for (int xx = x; xx < Math.min(width, x + blocksize_x); xx++) {
100 						final int idx = uniformPatterns.indexOf(patternImage[yy][xx]);
101 
102 						histograms[j][i].values[idx + 1]++;
103 					}
104 				}
105 			}
106 		}
107 	}
108 
109 	/**
110 	 * Get the histograms
111 	 * 
112 	 * @return the histograms
113 	 */
114 	public FloatFV[][] getHistograms() {
115 		return histograms;
116 	}
117 
118 	/**
119 	 * Get the histograms as a single {@link FloatFV}.
120 	 * 
121 	 * @return the histograms
122 	 */
123 	public FloatFV getHistogram() {
124 		final int len = histograms[0][0].length();
125 		final FloatFV h = new FloatFV(histograms.length * histograms[0].length * len);
126 
127 		for (int j = 0; j < histograms.length; j++) {
128 			for (int i = 0; i < histograms[0].length; i++) {
129 				final int blkid = i + j * histograms[0].length;
130 				System.arraycopy(histograms[j][i].values, 0, h.values, blkid * len, len);
131 			}
132 		}
133 
134 		return h;
135 	}
136 }