How do I invert the colors of an image stored in Mat image
in the Java API of OpenCV? Using image.inv()
gets me an error.
inv()
is used to calculate inverse matrix; use bitwise_not instead:
Core.bitwise_not( image, image );
inv()
method will try to take inverse of the matrix that's why it is failing (most probably your image matrix is not invertible).
You can subtract two images from each other, so you can create an image with all values are 255 and then extract original one from it, if that is what you mean by invert the colors.
Just in case:
Mat invertcolormatrix= new Mat(image.rows(),image.cols(), image.type(), new Scalar(255,255,255));
Core.subtract(invertcolormatrix, image, image);