TypeError: cv.warpPerspective is not a function
Hi this is my code
const cv = require('opencv4nodejs');
var image = cv.imread('/public/staictest/cc.jpg');
var dstTri = [];
var srcTri = [];
srcTri[0] = new cv.Point2(70, 73);
srcTri[1] = new cv.Point2(312, 70);
srcTri[2] = new cv.Point2(323, 223);
srcTri[3] = new cv.Point2(63, 230);
dstTri[0] = new cv.Point2(0, 0);
dstTri[1] = new cv.Point2(260,0);
dstTri[2] = new cv.Point2(260,160);
dstTri[3] = new cv.Point2(0,160);
console.log(dstTri,srcTri);
var transform = cv.getPerspectiveTransform(srcTri, dstTri);
var drcimage;
var size = new cv.Size(160,260);
drcimage = cv.warpPerspective(transform, size);
cv.imwrite('/public/staictest/1111.jpg', drcimage);
cv.waitKey();
when i run the code
drcimage = cv.warpPerspective(transform, size);
^
TypeError: cv.warpPerspective is not a function
at Object.<anonymous> (/home/test/nodejs/helloworld.js:25:15)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMain (module.js:611:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:160:9)
at bootstrap_node.js:507:3
What should I do?
Looking at https://justadudewhohacks.github.io/opencv4nodejs/docs/Mat#warpPerspective, I'd think that you need to call warpPerspective() on your source Mat. The way you're calling it now, you're only passing the transform and a size so the function has no concept of a Mat. That means you either need to pass it as the first parm (in most cases), or it will use context if it's exposed on the Mat. I'm no pro here though :)
Here's a working snippet from my project to confirm:
// mat contains my image with page
// pageContour is an array of 4 cv.Point2 that contains the corners of my detected page
const transform = cv.getPerspectiveTransform(
[ pageContour[0], pageContour[3], pageContour[2], pageContour[1] ],
[ new cv.Point2( 0,0 ), new cv.Point2( 0, mat.rows ), new cv.Point2( mat.cols, page.rows ), new cv.Point2( mat.cols, 0 ) ]
);
const perspectiveCorrected = mat.warpPerspective( transform, new cv.Size( mat.cols, mat.rows ) );
@rainabba
Thanks for sharing your solution!
May I ask where pageContour, mat, and page came from though and what are they for?