opencv4nodejs
opencv4nodejs copied to clipboard
Find something strange with Func: drawFillConvexPoly()
Background & Problem Description
I am doing Affine Transform in a Triangle Mesh. After that, I create a mask Mat so that I can copy the updated triangular region to the output image further.
The
mask.drawFillConvexPoly()
helps me determine the triangle area:
const cv = require("opencv4nodejs")
let mask = new cv.Mat(100, 99, cv.CV_32FC4)
const points = []
points.push(new cv.Point2(10, 10))
points.push(new cv.Point2(80, 80))
points.push(new cv.Point2(50, 10))
mask.drawFillConvexPoly(points, new cv.Vec(1.0, 1.0, 1.0), 16, 0)
If everything is correct (e.g. let mask = new cv.Mat(100, 100, cv.CV_32FC4)
is just okay) , the mask should be exactly a white triangle with a black background.
However, in this case,
cv.imshow("mask", mask)
return this:
I wanna know the reason and how to avoid this if this is not a bug.
Possibilities
- DataType of mask Mat? : (I tried cv.CV_8UC3 still got this problem)
- DataType of point2? : It seems we do not have point2f & point2i in opencv4nodejs
- cv.imshow(...) not accurate?
- Number of channels?
- points should be inside the Mat: I already avoid this as u can see in my sample code. ...
What I tried
I tried to print out some data value of pixels in the mask:
let maskAsArr = mask.splitChannels()[0].getDataAsArray()
console.log(maskAsArr[30].slice(0, 50))
It turns out some values are negative / extremely large such as 8388608, -2.3543017412042005e+21, 4.134773263365008e-38, instead of 0 or 1. After casting them into 0, most abnormal points disappear:
let maskAsArr = mask.splitChannels()[0].getDataAsArray()
.map(function (col) {
col = col.map(function (val) {
if (val != 0 && val != 1) {
val = 0
}
return val
})
return col
})
Environment Info
OpenCV version : 4.4.0 OS: MacOSX