18

I read an image with ndimage, which results in a binary image like this:

enter image description here

I would like to invert the image such that white turns into black, and vice versa.

Help is appreciated.

normanius
  • 8,629
  • 7
  • 53
  • 83
Pamungkas Jayuda
  • 1,194
  • 2
  • 13
  • 31

5 Answers5

33
numpy.invert(close_img)

I use invert array. It works for me.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Pamungkas Jayuda
  • 1,194
  • 2
  • 13
  • 31
18

With the devel version of scikit-image (upcomming v0.13), you can use invert(). Example:

from skimage import util 
img = data.camera() 
inverted_img = util.invert(img)
6

Here's another method using OpenCV

import cv2

image = cv2.imread('2.png')
invert = cv2.bitwise_not(image) # OR
# invert = 255 - image

nathancy
  • 42,661
  • 14
  • 115
  • 137
4

If your image is represented with non-negative floating point values topping out at 1.0, you can use 1 - close_image

eadsjr
  • 681
  • 5
  • 20
1

I like Pamungkas Jayuda's anwer, but it works only on the assumption that your data is an integer. In my case, I just used simple inversion with a small value on the denominator to avoid division by zero:

1 / (2e5 + img)
ambodi
  • 6,116
  • 2
  • 32
  • 22