TechStark / opencv-js

OpenCV JavaScript version for node.js or browser
https://npmjs.com/@techstark/opencv-js
Apache License 2.0
408 stars 36 forks source link

TypeError: RotatedRect.points is not a function #46

Open Npscholar opened 10 months ago

Npscholar commented 10 months ago

Working on implementing the camshift example listed in the 4.8.0-dev turtorial.

Can someone explain to me what I am doing wrong? Or what gap there is between this library and the opencv docs? I am using "@techstark/opencv-js": "^4.8.0-release.9"

Small excerpt from the example

        // start processing.
        cap.read(frame);
        cv.cvtColor(frame, hsv, cv.COLOR_RGBA2RGB);
        cv.cvtColor(hsv, hsv, cv.COLOR_RGB2HSV);
        cv.calcBackProject(hsvVec, [0], roiHist, dst, [0, 180], 1);

        // apply camshift to get the new location
        [trackBox, trackWindow] = cv.CamShift(dst, trackWindow, termCrit);

        // Draw it on image
        let pts = cv.rotatedRectPoints(trackBox);
        cv.line(frame, pts[0], pts[1], [255, 0, 0, 255], 3);
        cv.line(frame, pts[1], pts[2], [255, 0, 0, 255], 3);
        cv.line(frame, pts[2], pts[3], [255, 0, 0, 255], 3);
        cv.line(frame, pts[3], pts[0], [255, 0, 0, 255], 3);
        cv.imshow('canvasOutput', frame);

However, there are several modifications I have to make.

  1. cv.Camshift() returns a RotatedRect which cannot be destructured into [trackBox, trackWindow] as the example suggests
  2. cv.rotatedRectPoints() causes Property 'rotatedRectPoints' does not exist on type 'typeof import("<omitted>node_modules/@techstark/opencv-js/dist/src/index")' so in response I try to use RotatedRect.points which is a valid property in the module/ opencv docs but receive the error titled in this issue. TypeError: newLocationForRect.points is not a function

My code experiencing the issue

        // start processing.
        cap.read(frame);
        cv.cvtColor(frame, hsv, cv.COLOR_RGBA2RGB);
        cv.cvtColor(hsv, hsv, cv.COLOR_RGB2HSV);
        cv.calcBackProject(hsvVec, [0], roiHist, dst, [0, 180], 1);        

        // apply camshift to get the new location
        // actually returns an array of rectangle and points
        const newLocationForRect = cv.CamShift(dst, trackWindow, termCrit);
        console.log("trackBox", newLocationForRect);

        // Draw it on image
        let pts: any = [];
        // pass a point array to put the 4 vertices in types are bad so using as any
        const returnPts = newLocationForRect.points(pts as any); 
        console.log("after", returnPts);

        cv.line(frame, pts[0], pts[1], [255, 0, 0, 255], 3);
        cv.line(frame, pts[1], pts[2], [255, 0, 0, 255], 3);
        cv.line(frame, pts[2], pts[3], [255, 0, 0, 255], 3);
        cv.line(frame, pts[3], pts[0], [255, 0, 0, 255], 3);
        cv.imshow("canvasOutput", frame);
ttt43ttt commented 10 months ago

for issue 1, check the real return value when the code is running. It could be that OpenCV's document is out of date. for issue 2, you can try ignoring the TS type check (e.g. cast to any) and see if it could work at runtime. If the function exists, we can add it to the TS type declaration in this repository.