Replace your current code …
void getHistogram(const Mat& src, MatND& hist, float range[]=NULL) {
if(range==NULL) {
double maxPixel=0;
minMaxLoc(src, 0, &maxPixel, 0, 0);
range = { 0, maxPixel +1 };
}
// now calculate histogram with the right range
// something something
}
with this:
void getHistogram(const Mat& src, MatND& hist, float range[] ) {
assert( range != 0 );
// now calculate histogram with the right range
// something something
}
void getHistogram(const Mat& src, MatND& hist ) {
double maxPixel=0;
minMaxLoc(src, 0, &maxPixel, 0, 0);
float range[] = { 0, maxPixel +1 };
getHistogram( src, hist, range );
}
That said, why are you using float
instead of double
?
EDIT: the OP explains that the array of float
is required by OpenCV.
He further explains in his answer that he's resolved the problem as follows:
void imHist(const Mat& src, MatND& hist, float range[]=NULL) {
if(range==NULL) {
double maxPixel=0;
minMaxLoc(src, 0, &maxPixel, 0, 0);
range = new float[2];
range[0] = 0;
range[1] = maxPixel +1;
}
}
This leaks memory, and is needlessly inefficient.
EDIT 2: the reason that the above code leaks memory is because there is a new
(which allocates memory), but no corresponding delete
expression (which frees the memory), and there is no indication of whether the memory was allocated by new
or provided by the caller.
The reason that it's inefficient is that a dynamic memory allocation typically is orders of magnitude slower than e.g. a basic assignment or stack allocation, because it has to do a search for a suitable small free chunk of memory.
The stack allocation (C++ “automatic memory”) avoids that inefficiency by always deallocating in reverse order of allocation, so that it can always use the start of the free stack memory area for the next allocation (modulo direction: in practice the stack grows downward in memory on all machines I have known about).