Cedric
Cedric

Reputation: 899

Boolean Not in OpenCV 3.1

I would like to know if there's a simple way of doing a boolean "not" operation on a cv::Mat. This does not work:

cv::Mat mat = cv::Mat::ones(3,3, CV_8U);
cv::Mat mat_not = !mat;

As such, is there an effective or simple way to do this? Should I resort to using something like this:

cv::Mat mat_not = mat < cv::Mat::ones(3,3,CV_8U);

Thanks a lot!

EDIT: I confused the "not" operator between MATLAB and C++ (since I'm translating the first one to the other). This works fine:

cv::Mat map2 = ~map1;

Upvotes: 2

Views: 1760

Answers (1)

Jeremy Gamet
Jeremy Gamet

Reputation: 165

Edit: 12:30pm 7/20/2016

I see the op wants a regular NOT and I'm used to that being different for things like IDL and MatLab etc..

As @cxyzs7, @Cedric, and @Miki mentioned the operator in c++ is ~ so ...

mat = ~mat;

However if you ever want to do something else element wise (for example a bitwise) most of the time there's already a whole function for that. IE...

bitwise_not

cv::Mat src;
src = stuff;
cv::Mat dst;

//then call it
bitwise_not(src,dst);

If the function you want to do element by element is non-existent in the library or isn't an overloaded operator you can always do it the brute force way...

for(...) {
     for (...) {
        dst.at<int>(i,j) = ! src.at<int>(i,j);
     }
}

Upvotes: 3

Related Questions