001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.image.feature.dense.binarypattern;
031
032import gnu.trove.list.array.TIntArrayList;
033
034import org.openimaj.citation.annotation.Reference;
035import org.openimaj.citation.annotation.ReferenceType;
036import org.openimaj.feature.FloatFV;
037
038/**
039 * Class for extracting histograms of Local Uniform Binary Patterns. Uniform
040 * patterns have less than one 01 transition and one 01 transition when viewed
041 * as a circular buffer.
042 * 
043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 */
045@Reference(
046                type = ReferenceType.Article,
047                author = { "Ojala, T.", "Pietikainen, M.", "Maenpaa, T." },
048                title = "Multiresolution gray-scale and rotation invariant texture classification with local binary patterns",
049                year = "2002",
050                journal = "Pattern Analysis and Machine Intelligence, IEEE Transactions on",
051                pages = { "971 ", "987" },
052                month = "jul",
053                number = "7",
054                volume = "24",
055                customData = {
056                                "doi", "10.1109/TPAMI.2002.1017623",
057                                "ISSN", "0162-8828"
058                })
059public class LocalUniformBinaryPatternHistogram {
060        protected int blocksize_x;
061        protected int blocksize_y;
062        FloatFV[][] histograms;
063
064        /**
065         * Construct the extractor with the given block size for each local patch.
066         * 
067         * @param blocksize_x
068         *            the width of the blocks
069         * @param blocksize_y
070         *            the height of the blocks
071         */
072        public LocalUniformBinaryPatternHistogram(int blocksize_x, int blocksize_y) {
073                this.blocksize_x = blocksize_x;
074                this.blocksize_y = blocksize_y;
075        }
076
077        /**
078         * Compute the histograms for the given pattern image, encoded with the
079         * given number of bits.
080         * 
081         * @param patternImage
082         *            the pattern data
083         * @param nbits
084         *            the number of bits used to encode the patterns
085         */
086        public void calculateHistograms(int[][] patternImage, int nbits) {
087                final int height = patternImage.length;
088                final int width = patternImage[0].length;
089                final TIntArrayList uniformPatterns = UniformBinaryPattern.getUniformPatterns(nbits);
090
091                histograms = new FloatFV[(int) Math.ceil((double) height / (double) blocksize_y)][(int) Math.ceil((double) width
092                                / (double) blocksize_x)];
093
094                for (int y = 0, j = 0; y < height; y += blocksize_y, j++) {
095                        for (int x = 0, i = 0; x < width; x += blocksize_x, i++) {
096                                histograms[j][i] = new FloatFV(uniformPatterns.size() + 1);
097
098                                for (int yy = y; yy < Math.min(height, y + blocksize_y); yy++) {
099                                        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}