Skip to content

Commit

Permalink
add: deprecation wrapper to support old calcHist API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
legraphista committed Nov 14, 2019
1 parent 2b67164 commit ed1cace
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/src/deprecations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

const assert = require('assert');

module.exports = function (cv) {

// deprecate wrapper for the old calcHist API
const _calcHist = cv.calcHist;
cv.calcHist = function calcHist(img, histAxes, mask) {
assert(img instanceof cv.Mat, 'Imgproc::CalcHist - Error: expected argument 0 to be of type Mat');
assert(Array.isArray(histAxes), 'Imgproc::CalcHist - Error: expected argument 1 to be of type array of HistAxes');

histAxes = histAxes.slice();

let warningThrown = false;
const len = histAxes.length;

for (let i = 0; i < len; ++i) {
const entry = histAxes[i];
if (!(entry instanceof cv.HistAxes)) {
if (!warningThrown) {
warningThrown = true;
console.warn(`Imgproc::CalcHist - Deprecated support for object in argument 1 at index ${i}. Please switch to using HistAxes instances.`);
}
histAxes[i] = new cv.HistAxes(entry);
}
}

if (mask) {
return _calcHist(img, histAxes, mask);
}
return _calcHist(img, histAxes);
};

};
4 changes: 4 additions & 0 deletions lib/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const makeDrawUtils = require('./drawUtils')
const deprecations = require('./deprecations')

module.exports = function(cv) {
const {
Expand All @@ -8,5 +9,8 @@ module.exports = function(cv) {

cv.drawTextBox = drawTextBox
cv.drawDetection = drawDetection

deprecations(cv)

return cv
}
2 changes: 2 additions & 0 deletions lib/typings/cv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export function blobFromImagesAsync(image: Mat[], scaleFactor?: number, size?: S
export function blur(mat: Mat, kSize: Size, anchor?: Point2, borderType?: number): Mat;
export function blurAsync(mat: Mat, kSize: Size, anchor?: Point2, borderType?: number): Promise<Mat>;
export function NMSBoxes(bboxes: Rect[], scores: number[], scoreThreshold: number, nmsThreshold: number): number[];
/** @deprecated */
export function calcHist(img: Mat, histAxes: { channel: number, bins: number, ranges: [number, number] }[], mask?: Mat): Mat;
export function calcHist(img: Mat, histAxes: HistAxes[], mask?: Mat): Mat;
export function calcHistAsync(img: Mat, histAxes: HistAxes[], mask?: Mat): Promise<Mat>;
export function calibrateCamera(objectPoints: Point3[], imagePoints: Point2[], imageSize: Size, cameraMatrix: Mat, distCoeffs: number[], flags?: number, criteria?: TermCriteria): { returnValue: number, rvecs: Vec3[], tvecs: Vec3[], distCoeffs: number[] };
Expand Down
12 changes: 12 additions & 0 deletions test/tests/imgproc/imgprocTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ module.exports = ({ cv, utils, getTestImg }) => {
expect(() => cv.calcHist(getTestImg())).to.throw('Imgproc::CalcHist - Error: expected argument 1 to be of type array of HistAxes');
});

it('should return when using the deprecated API', () => {
const histAxes = [
{
channel: 0,
bins: 8,
ranges: [0, 256]
}
];
const hist1D = cv.calcHist(getTestImg(), histAxes);
assertPropsWithValue(hist1D)({ rows: 8, cols: 1, dims: 2 });
});

it('should return 1 dimensional hist', () => {
const histAxes = [
{
Expand Down

0 comments on commit ed1cace

Please sign in to comment.