0

I would like to take a colour image and convert it to a binary image where what was close to either black or white return False, and all the intermediate values return True.

What is the proper syntax to impose the two conditions below simultaneously?

binary = color.rgb2gray(img) > 0.05 
binary = color.rgb2gray(img) < 0.95

If I used this:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from skimage import color
import requests
from PIL import Image
from StringIO import StringIO

url = 'https://mycarta.files.wordpress.com/2014/03/spectrogram_jet.png'
r = requests.get(url)
img = np.asarray(Image.open(StringIO(r.content)).convert('RGB'))

and then:

binary = color.rgb2gray(img)  < 0.95

I would get a proper binary image that I can plot with:

fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
plt.imshow(binary, cmap='gray')
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
plt.show()

Similarly with this:

color.rgb2gray(img) < 0.95

But if I tried them together like this:

binary = color.rgb2gray(img) > 0.05 and color.rgb2gray(img) < 0.95

I got this message:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

3
  • What code did you run to "try them together"? Commented Mar 6, 2016 at 23:29
  • Same as suggested in @caenyon's answer: binary = color.rgb2gray(img) > 0.05 and color.rgb2gray(img) < 0.95 Commented Mar 6, 2016 at 23:33
  • what is the img variable? an complete example would be easier (imports etc) Commented Mar 6, 2016 at 23:48

2 Answers 2

2

Your code isn't working because the rgb2gray method of skimage.color returns an array. The skimage module utilizes the numpy module which refuses to perform boolean comparisons on arrays. This is where your ValueError comes from.

When using a comparison operator like and on arrays, numpy will not comply. Instead, you should use np.logical_and or the binary operator &.

Sign up to request clarification or add additional context in comments.

1 Comment

your suggestion worked, thank you for the explanation too
-1

you can use 'and' to combine these two conditions

binary = color.rgb2gray(img) > 0.05 and color.rgb2gray(img) < 0.95

or you can just write them as one condition

binary = 0.05 < color.rgb2gray(img) < 0.95

6 Comments

When I try those two options you suggest, in both cases (the first one is what I tried before) I get the error message
what exactly does the color.rgb2gray function return? are you sure that it's a number and not a list?
how does the rest of your program look like? i'd assume that the problem is somewhere else...
color.rgb2gray(img) just applies the formula Y = (0.2125 R + 0.7154 G + 0.0721 B) to convert to gray
The code is very simple. This works: binary = color.rgb2gray(img) < 0.95 fig = plt.figure(figsize=(10,10)) ax1 = fig.add_subplot(111) plt.imshow(binary, cmap='gray') ax1.xaxis.set_ticks([]) ax1.yaxis.set_ticks([]) plt.show()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.