Inverting an image in OpenCV is quite easy. No need to mess around with pointers. Actually, it can be done in a single line, with a single instruction:

1 cvXorS(input, cvScalar(255), output)

Ok, this obviously wont work for floating point images.

As some readers have pointed out, however, exactly the same can be achieved using cvNot. I do not know why I've proposed the XOR solution, possibly I've been unaware of the fact that cvNot is indeed the bit-wise complement and thus equivalent to an XOR with 255.

An anonymous reader suggested the following snipplet to get the classic color-inverted look

1 IplImage *src=cvLoadImage("img.png",1);
2 IplImage *src_yuv=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,3);
3 IplImage *dst=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,3);
4 cvCvtColor(src,src_yuv,CV_BGR2YCrCb);
5 cvNot(src_yuv,dst);
6 cvCvtColor(dst,src,CV_YCrCb2BGR);
7 cvSaveImage("negative.png",src);