1

I'm using Python and Scipy to perform some basic image manipulation. I've made the image greyscale and subtracted it from a gaussian blur of itself as a form of edge detection. Now I'd like to make it look pretty when running the .imshow() command. If I use one of the default colormaps, for instance,

matplotlib.pyplot.imshow(lena, cmap='binary')

where lena is a matrix representing my image in question, the image appears washed out with a grey background. It looks quite a lot like this.

I would like the image to appear sharper, with only two real colors, white and black, and very little (or no) grey in between.

Since none of the other default colormaps in SciPy can do this, I figured I should make my own. But I'm afraid I don't fully grasp the documentation provided by scipy.

So let's say I have the colormap from the tutorial:

cdict = {'red': ((0.0, 0.0, 0.0),
                 (0.5, 1.0, 0.7),
                 (1.0, 1.0, 1.0)),
         'green': ((0.0, 0.0, 0.0),
                   (0.5, 1.0, 0.0),
                   (1.0, 1.0, 1.0)),
         'blue': ((0.0, 0.0, 0.0),
                  (0.5, 1.0, 0.0),
                  (1.0, 0.5, 1.0))}

my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', cdict, 256)
matplotlib.pyplot.imshow(lena, cmap=my_cmap)

How should this look if I want the colormap to be exclusively white from range 0 to .5, and exclusively black from range .5 to 1? Thanks to everyone for the help!

  • See: http://matplotlib.org/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap You are also a bit confused about the boundaries between matplotlib, scipy, and numpy. – tacaswell Apr 14 '14 at 00:00
  • See: http://stackoverflow.com/questions/9707676/defining-a-discrete-colormap-for-imshow-in-matplotlib, exactly what you want. – CT Zhu Apr 14 '14 at 00:24

1 Answers1

1

I would expect something like this:

cdict = {'red': ((0.0, 0.0, 0.0),
                 (0.5, 0.0, 0.0),
                 (0.5, 1.0, 1.0),
                 (1.0, 1.0, 1.0)),
         'green': ((0.0, 0.0, 0.0),
                 (0.5, 0.0, 0.0),
                 (0.5, 1.0, 1.0),
                 (1.0, 1.0, 1.0)),
         'blue': ((0.0, 0.0, 0.0),
                 (0.5, 0.0, 0.0),
                 (0.5, 1.0, 1.0),
                 (1.0, 1.0, 1.0))}

See also: http://matplotlib.org/examples/pylab_examples/custom_cmap.html
Please excuse me if I'm wrong. I never created jumps like the one you want.

frits
  • 327
  • 2
  • 15
  • Dead on. I didn't catch the other stackoverflow question;, I think that's a different way to do what I want. But this answer is a whole lot simpler. Thanks to everyone for the help! – Jacob Fauber Apr 15 '14 at 02:02