简体   繁体   中英

opencv.js Mat multiplication with Scalar

I am new to OpenCV and trying to do some detection on a camera with opencv.js I was following some tutorials based on python code and adapting it to javascript/typescript (my page is running in an angular application).

I capture frames from a camera in fullhd resolution, then in order to increase the performance and have a more smooth selection, I scale the frame to a smaller size. I then change the image to grayscale, add a gaussian blur and apply a threshold to it. At this stage I call the findContours function to detect the contours of the shapes detected by the camera.

In the sample I am following the next step is to iterate on each found contour, then multiply the Mat with the ratio from the original frame and small frame in order to display the contour on the original image.

cnt *= ratio

unfortunately doing so changes my Mat object cnt into NaN. I assume the * operator is not working on Mat with opencv.js and am trying to find another solution.

I did several experimentation, with no avail. I found the mul() function and got somewhat results with a basic object. the function requires another Mat, then a ratio.

I did the following test.

let mat1 = cv.matFromArray(3,3,cv.CV_32FC1,[1,2,3,4,5,6,7,8,9]);
const temp = new cv.Mat.ones(c.rows, c.cols, cv.CV_32FC1);
let dst = mat1.mul(temp,10); 
console.log("dst::" + dst.data32F);//dst::10,20,30,40,50,60,70,80,90

so it looks like promising. but when trying to apply it to my contour, I have the following problem. In order to generate the dummy temp Mat, I need to specify the type of the matrix. In the test I set it to cv.CV_32FC1 But when trying with my contour object

cnt.type() returns 12 Checking the documentation it seems it translates to CV_8UC130, as it is not a defined value, in C++ or python you would have to use the function CV_8UC(130) but it doesn't seems to be in the opencv.js version. Thus I am unable to create my Mat and continue my process.

Is there something I am missing? I feel a lot of steps are necessary for an operation which should be simple. What would be the easiest step to scale my contour?

With the comment above, I was able to solve my problem with the following code. Posting it here if someone else is having trouble.

...
let cnt = contours.get(i);
let ones = new cv.Mat(cnt.rows, cnt.cols, cv.CV_32SC2, new cv.Scalar(1,1,1,1));
let cnt2 = cnt.mul(ones,ratio);
ones.delete();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM