opencv - How to improve invert + grayscale operator in Java? -


i want invert , gray scale image. here original image:
enter image description here

here final result want achieve (produced paint.net):
enter image description here

however using (basic?) java code found on internet, picture below: enter image description here

here code used:

private static final byte[] inverttable;  static {     inverttable = new byte[256];     (int = 0; < 256; i++) {         inverttable[i] = (byte) (255 - i);     } }  private static bufferedimage grayscale(bufferedimage source) {     colorconvertop grayscale = new colorconvertop(colorspace.getinstance(colorspace.cs_gray), null);      return grayscale.filter(source,null); }  private static bufferedimage invertimage(final bufferedimage src) {     final int w = src.getwidth();      final int h = src.getheight();      final bufferedimage dst = new bufferedimage(w, h, bufferedimage.type_int_rgb);      final bufferedimageop invertop = new lookupop(new bytelookuptable(0, inverttable), null);      return invertop.filter(src, dst); }  // ... bufferedimage sourceimage = ... bufferedimage convertedimage = grayscale(invertimage(sourceimage)); 

how can improve above code?

using imgproc , core, did

mat src = new mat(); mat gray = new mat(); src = highgui.imread("..."); imgproc.cvtcolor(src, gray, imgproc.color_bgr2gray); core.bitwise_not(gray, gray); highgui.imwrite("...", gray); 

and got closer, though not identical:

opencv default grayscale mapping + bitwise invert