Ask Your Question
7

subtract from white - invert

asked Oct 31 '12

aliasofmike gravatar image

updated Oct 29 '0

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...

Preview: (hide)

2 answers

Sort by » oldest newest most voted
11

answered Nov 1 '12

unxnut gravatar image

The follwoing should do it:

bitwise_not ( src, dst );
Preview: (hide)

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 (Nov 2 '12)edit
11

answered Oct 31 '12

AlexanderShishkov gravatar image
cv::Mat src = ...
cv::Mat invSrc =  cv::Scalar::all(255) - src;
Preview: (hide)

Question Tools

1 follower

Stats

Asked: Oct 31 '12

Seen: 78,774 times

Last updated: Oct 31 '12