opencvsharp icon indicating copy to clipboard operation
opencvsharp copied to clipboard

MatExpr operators - how do I translate this?

Open replaysMike opened this issue 3 years ago • 2 comments

Hello all - I see that the greaterthan/lessthan operators aren't implemented in OpenCvSharp, and a very old issue regarding it.

If they aren't implemented - how does one translate this C++ syntax over to C#?

cv::Mat myMat;
var exp = 255 - ( myMat > 0 );
var myMat = new Mat();
var exp = 255 - myMat; // this works fine, but I don't know what the > 0 is supposed to do!

can anyone help?

replaysMike avatar Apr 06 '22 22:04 replaysMike

ok the greater than operator in this case is producing a boolean result where every pixel greater than 0 will have a value of 255 - or so I have come to understand.

Best alternative I came up with is the following, which probably isn't as efficient:

for(var x = 0; x < myMat.Cols; x++)
{
    for (var y = 0; y < myMat.Rows; y++)
    {
        var val = myMat.At<byte>(x, y);
        if (val > 0)
            myMat.At<byte>(x, y) = 255;
        else
            myMat.At<byte>(x, y) = 0;
    }
}
// then invert the result
var exp = 255 - myMat;

The 255 - myMat operator just inverts that result after which works fine in OpenCvSharp.

replaysMike avatar Apr 06 '22 22:04 replaysMike

@replaysMike

you can try this , It should be more efficient compared with loops.

var myMat = new Mat();
// there's an operator GreaterThan 
var exp = 255 - myMat.GreaterThan(0)

AvenSun avatar Apr 10 '22 16:04 AvenSun

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 12 '22 05:10 stale[bot]