7 #include "ImageHistogram.h"
10 #include "ControlNet.h"
11 #include "ControlMeasure.h"
30 ImageHistogram::ImageHistogram(
double minimum,
double maximum,
int nbins) :
57 double startSample,
double startLine,
58 double endSample,
double endLine,
59 int bins,
bool addCubeData) {
60 InitializeFromCube(cube, statsBand, progress, bins, startSample, startLine,
64 Brick cubeDataBrick((
int)(endSample - startSample + 1),
68 int startBand = statsBand;
69 int endBand = statsBand;
76 if (progress != NULL) {
77 progress->
SetText(
"Gathering histogram");
79 (
int)(endLine - startLine + 1) * (
int)(endBand - startBand + 1));
83 for (
int band = startBand; band <= endBand; band++) {
84 for (
int line = (
int)startLine; line <= endLine; line++) {
86 cube.
read(cubeDataBrick);
88 if (progress != NULL) {
96 void ImageHistogram::InitializeFromCube(
Cube &cube,
int statsBand,
97 Progress *progress,
int nbins,
double startSample,
double startLine,
98 double endSample,
double endLine) {
100 if ( (statsBand < 0) || (statsBand > cube.
bandCount() ) ) {
101 string msg =
"Cannot gather histogram for band [" +
IString(statsBand) +
108 double minDnValue =
Null;
109 double maxDnValue =
Null;
120 else if (cube.
pixelType() == UnsignedWord) {
127 else if (cube.
pixelType() == SignedWord) {
138 else if (cube.
pixelType() == UnsignedInteger ||
146 IString msg =
"Unsupported pixel type";
150 if (startSample ==
Null)
153 if (endSample ==
Null)
156 if (startLine ==
Null)
163 if (minDnValue ==
Null || maxDnValue ==
Null) {
165 Brick cubeDataBrick((
int)(endSample - startSample + 1),
172 int startBand = statsBand;
173 int endBand = statsBand;
175 if (statsBand == 0) {
180 if (progress != NULL) {
182 progress->
SetText(
"Computing min/max for histogram");
184 (
int)(endLine - startLine + 1) * (
int)(endBand - startBand + 1) );
188 for (
int band = startBand; band <= endBand; band++) {
189 for (
int line = (
int)startLine; line <= endLine; line++) {
191 cubeDataBrick.SetBasePosition(qRound(startSample), line, band);
192 cube.
read(cubeDataBrick);
193 stats.AddData(cubeDataBrick.DoubleBuffer(), cubeDataBrick.size());
195 if (progress != NULL) {
201 if (stats.ValidPixels() == 0) {
206 minDnValue = stats.Minimum();
207 maxDnValue = stats.Maximum();
229 const unsigned int count) {
232 int nbins =
p_bins.size();
234 for (
unsigned int i = 0; i < count; i++) {
236 if (BinRangeStart() == BinRangeEnd() ) {
240 index = (int) floor((
double)(nbins - 1) / (BinRangeEnd() - BinRangeStart()) *
241 (data[i] - BinRangeStart() ) + 0.5);
243 if (index < 0) index = 0;
244 if (index >= nbins) index = nbins - 1;
259 int nbins =
p_bins.size();
262 if (BinRangeStart() == BinRangeEnd() ) {
266 index = (int) floor((
double)(nbins - 1) / (BinRangeEnd() - BinRangeStart() ) *
267 (data - BinRangeStart() ) + 0.5);
269 if (index < 0) index = 0;
270 if (index >= nbins) index = nbins - 1;
285 const unsigned int count) {
288 int nbins =
p_bins.size();
290 for (
unsigned int i = 0; i < count; i++) {
293 if (BinRangeStart() == BinRangeEnd() ) {
297 index = (int) floor((
double)(nbins - 1) / (BinRangeEnd() - BinRangeStart()) *
298 (data[i] - BinRangeStart()) + 0.5);
300 if (index < 0) index = 0;
301 if (index >= nbins) index = nbins - 1;
319 double &low,
double &high)
const {
320 if ( (index < 0) || (index >= (
int)
p_bins.size() ) ) {
326 double binSize = (BinRangeEnd() - BinRangeStart()) / (
double)(
p_bins.size() - 1);
327 low = BinRangeStart() - binSize / 2.0 + binSize * (double) index;
328 high = low + binSize;