summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/data/images/ComputeHSBColorHistogram.java
blob: e9b428f13444a476c17624cd295f832bc47cf67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package de.lmu.ifi.dbs.elki.data.images;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2012
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;
import java.util.List;

import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.GreaterEqualConstraint;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ListEachConstraint;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ListSizeConstraint;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.IntListParameter;

/**
 * Compute color histograms in a Hue-Saturation-Brightness model.
 * 
 * @author Erich Schubert
 */
public class ComputeHSBColorHistogram extends AbstractComputeColorHistogram {
  /**
   * Parameter that specifies the number of bins (per plane) to use.
   * 
   * <p>
   * Key: {@code -rgbhist.bpp}
   * </p>
   */
  public static final OptionID BINSPERPLANE_ID = new OptionID("hsbhist.bpp", "Bins per plane for HSV/HSB histogram. This will result in bpp ** 3 bins.");

  /**
   * Number of bins in hue to use.
   */
  int quanth;

  /**
   * Number of bins in saturation to use.
   */
  int quants;

  /**
   * Number of bins in brightness to use.
   */
  int quantb;

  /**
   * Constructor.
   * 
   * @param quanth Hue bins
   * @param quants Saturation bins
   * @param quantb Brightness bins
   */
  public ComputeHSBColorHistogram(int quanth, int quants, int quantb) {
    super();
    this.quanth = quanth;
    this.quants = quants;
    this.quantb = quantb;
  }

  @Override
  protected int getBinForColor(int rgb) {
    int r = (rgb & 0xFF0000) >> 16;
    int g = (rgb & 0x00FF00) >> 8;
    int b = (rgb & 0x0000FF);

    float[] hsbvals = Color.RGBtoHSB(r, g, b, null);
    // The values returned by RGBtoHSB are all in [0:1]
    int h = (int) Math.floor(quanth * hsbvals[0]);
    int s = (int) Math.floor(quants * hsbvals[1]);
    int v = (int) Math.floor(quantb * hsbvals[2]);
    // Guard against the value of 1.0
    if(h >= quanth) {
      h = quanth - 1;
    }
    if(s >= quants) {
      s = quants - 1;
    }
    if(v >= quantb) {
      v = quantb - 1;
    }
    return h * quants * quantb + s * quantb + v;
  }

  @Override
  protected int getNumBins() {
    return quanth * quants * quantb;
  }

  /**
   * Parameterization class.
   * 
   * @author Erich Schubert
   * 
   * @apiviz.exclude
   */
  public static class Parameterizer extends AbstractParameterizer {
    int quanth = 0;

    int quants = 0;

    int quantb = 0;

    @Override
    protected void makeOptions(Parameterization config) {
      super.makeOptions(config);
      final IntListParameter param = new IntListParameter(BINSPERPLANE_ID);
      param.addConstraint(new ListSizeConstraint(3));
      param.addConstraint(new ListEachConstraint<Integer>(new GreaterEqualConstraint(1)));

      if(config.grab(param)) {
        List<Integer> quant = param.getValue();
        if(quant.size() != 3) {
          config.reportError(new WrongParameterValueException(param, "I need exactly three values for the bpp parameter."));
        }
        else {
          quanth = quant.get(0);
          quants = quant.get(1);
          quantb = quant.get(2);
        }
      }
    }

    @Override
    protected ComputeHSBColorHistogram makeInstance() {
      return new ComputeHSBColorHistogram(quanth, quants, quantb);
    }
  }
}