Ask Your Question
7

subtract from white - invert

asked 2012-10-31 15:52:57 -0600

aliasofmike gravatar image

updated 2020-10-29 16:54:24 -0600

I'm looking to invert an image, such that black would become white.

I'm using the C++ OpenCV. The documentation seems to suggest that:

cv::subtract(cv::Scalar:all(255),src,dst);

would work. It doesn't, though it compiles.

I know I can go through pixel by pixel, but I am writing a application that processes live video and need to take advantage of whatever optimizations I can.

Any suggestions are appreciated! I feel like I'm missing something, because the subtract function doesn't appear to be overloaded from looking at the header file...

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
11

answered 2012-10-31 21:29:11 -0600

unxnut gravatar image

The follwoing should do it:

bitwise_not ( src, dst );
edit flag offensive delete link more

Comments

3

Thanks! That does to it, and seems to me like the best way.

Interestingly, I found out that my originally posted solution:

cv::subtract(cv::Scalar:all(255),src,dst);

works just fine as well.

My problem was actually that my destination mat was not the same size as the source. oops!

aliasofmike gravatar imagealiasofmike ( 2012-11-02 12:44:57 -0600 )edit
11

answered 2012-10-31 16:39:44 -0600

AlexanderShishkov gravatar image
cv::Mat src = ...
cv::Mat invSrc =  cv::Scalar::all(255) - src;
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2012-10-31 15:52:57 -0600

Seen: 78,591 times

Last updated: Oct 31 '12