diff --git a/openbr/plugins/quantize.cpp b/openbr/plugins/quantize.cpp index 5493326..b16bd0c 100644 --- a/openbr/plugins/quantize.cpp +++ b/openbr/plugins/quantize.cpp @@ -219,19 +219,41 @@ private: // Common::KernelDensityEstimation(impostorScores, lut->at(0,j*256+k), hImpostor))); } + int getStep(int cols) const + { + if (n > 0) return n; + if (n == 0) return cols; + return ceil(float(cols)/abs(n)); + } + + int getOffset(int cols) const + { + if (n >= 0) return 0; + const int step = getStep(cols); + return (step - cols%step) % step; + } + + int getDims(int cols) const + { + const int step = getStep(cols); + if (n >= 0) return cols/step; + return ceil(float(cols)/step); + } + void train(const TemplateList &src) { Mat data = OpenCVUtils::toMat(src.data()); - if (data.cols % n != 0) qFatal("Expected dimensionality to be divisible by n."); + const int step = getStep(data.cols); const QList labels = src.labels(); Mat &lut = ProductQuantizationLUTs[index]; - lut = Mat(data.cols/n, 256*256, CV_32FC1); + lut = Mat(getDims(data.cols), 256*256, CV_32FC1); QList subdata, subluts; + const int offset = getOffset(data.cols); for (int i=0; i(0,i) = getIndex(m.colRange(i*n, (i+1)*n), centers[i]); + dst.m().at(0,i) = getIndex(m.colRange(max(0, i*step-offset), (i+1)*step-offset), centers[i]); } void store(QDataStream &stream) const