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 import org.openimaj.image.pixel.statistics.HistogramModel;
34
35 /**
36 * An {@link MBFPixelClassificationModel} that classifies an individual pixel by
37 * comparing it to a joint (colour) histogram. The histogram is learnt from the
38 * positive pixel samples given in training. The probability returned by the
39 * classification is determined from the value of the histogram bin in which the
40 * pixel being classified falls.
41 *
42 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
43 */
44 public class HistogramPixelModel extends MBFPixelClassificationModel {
45 private static final long serialVersionUID = 1L;
46
47 /**
48 * The model histogram; public for speed.
49 */
50 public HistogramModel model;
51
52 /**
53 * Construct with the given number of histogram bins per dimension.
54 *
55 * @param nbins
56 * number of bins per dimension.
57 */
58 public HistogramPixelModel(int... nbins) {
59 super(nbins.length);
60 model = new HistogramModel(nbins);
61 }
62
63 @Override
64 protected float classifyPixel(Float[] pix) {
65 int bin = 0;
66
67 for (int i = 0; i < ndims; i++) {
68 int b = (int) (pix[i] * (model.histogram.nbins[i]));
69 if (b >= model.histogram.nbins[i])
70 b = model.histogram.nbins[i] - 1;
71
72 int f = 1;
73 for (int j = 0; j < i; j++)
74 f *= model.histogram.nbins[j];
75
76 bin += f * b;
77 }
78
79 return (float) model.histogram.values[bin];
80 }
81
82 @Override
83 public String toString() {
84 return model.toString();
85 }
86
87 @Override
88 public HistogramPixelModel clone() {
89 final HistogramPixelModel newmodel = new HistogramPixelModel();
90 newmodel.model = model.clone();
91 newmodel.ndims = ndims;
92 return newmodel;
93 }
94
95 @Override
96 public void learnModel(MBFImage... images) {
97 model.estimateModel(images);
98 }
99 }