Opencv4nodejs: Help with bitwiseAnd method

Created on 3 Dec 2018  路  2Comments  路  Source: justadudewhohacks/opencv4nodejs

Hello, I'm having trouble with getting colors from an image, I get the mask but I can't filter separate the colours, here is my code:

const cv = require('opencv4nodejs');

let img = cv.imread('./cart.jpg');

let boundaries = [
    {
        lower: [17, 15, 100],
        upper: [50, 56, 200]
    },
    {
        lower: [86, 31, 4],
        upper: [220, 88, 50]
    },
    {
        lower: [25, 146, 190],
        upper: [62, 174, 250]
    },
    {
        lower: [103, 86, 65],
        upper: [145, 133, 128]
    }
]

for (const boundary of boundaries) {
    let lower = new cv.Vec3(...boundary.lower);
    let upper = new cv.Vec3(...boundary.upper);
    let mask = img.inRange(lower, upper);
    let output = img.bitwiseAnd(mask);
    cv.imwrite('source' + Date.now() + '.jpg', output);
}

the error I'm getting is libc++abi.dylib: terminating with uncaught exception of type std::runtime_error Abort trap: 6 produced by this line let output = img.bitwiseAnd(mask);, I've already tried changing the order of the params like this mask.bitwiseAnd(img); but I get the same error.

I'm running it under node v8.12.0 and opencv4nodejs ^4.11.0. Thanks in advance

Most helpful comment

Those are usually failed assertions in the OpenCV library itself, often because of invalid arguments. In this case, it might be because you're trying to bitwise AND a 3-channel and 1-channel image together.

You could AND each channel with your mask independently and then join them:

let channels = img.splitChannels();
let maskedChannels = channels.map(c => c.bitwiseAnd(mask));
let output = new cv.Mat(maskedChannels);

All 2 comments

Those are usually failed assertions in the OpenCV library itself, often because of invalid arguments. In this case, it might be because you're trying to bitwise AND a 3-channel and 1-channel image together.

You could AND each channel with your mask independently and then join them:

let channels = img.splitChannels();
let maskedChannels = channels.map(c => c.bitwiseAnd(mask));
let output = new cv.Mat(maskedChannels);

thank you @andrewwalters, it works!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ryandons picture ryandons  路  4Comments

sinitsyn-alex picture sinitsyn-alex  路  4Comments

SatoshiKawabata picture SatoshiKawabata  路  5Comments

seanquijote picture seanquijote  路  6Comments

developer239 picture developer239  路  5Comments