4

When reading a low-contrast image it automatically take this example:

In [1]: from PIL import Image
In [2]: import numpy as np
In [3]: import matplotlib.pyplot as plt
In [4]: img = Image.open('images/map.jpg')
In [5]: arr = np.asarray(img)
In [6]: plt.gray()
In [7]: plt.imshow(arr)
Out[7]: <matplotlib.image.AxesImage at 0x7f9c7e88f490>
In [8]: plt.show()

Input

input large

Plot (no change this is automatic by matplotlib.)

plot large

Because this input is different from the plot without, modifying anything.

I need low-contrast image to implement an algorithm to contrast stretching

Reading the book amazon Digital Image Processing (Rafael C. Gonzalez, Richard E. Woods)

PS: matplotlib is converting the automatic. I do not need.

neiesc
  • 633
  • 1
  • 7
  • 16
  • do you know how to implement an algorithm to find a better contrast? – Saullo G. P. Castro Sep 16 '13 at 20:36
  • 1
    Hang on is the issue that you need to adjust the matplotlib output or that you want to actually implement an algorithm to do it? If the latter google "histogram equalization" – YXD Sep 16 '13 at 20:36
  • In matplotlib is automatic i do not need. I myself want to implement the algorithm – neiesc Sep 16 '13 at 20:40
  • That's great...but what is your question? Where are you stuck? – YXD Sep 16 '13 at 20:42
  • matplotlib is **converting** the automatic. I do not need. Can not implement because it converts automatic(matplotlib). – neiesc Sep 16 '13 at 20:44

1 Answers1

13

If you do not want the automatic scaling of the colormap, you can use vmin and vmax to set the range you prefer, like this:

plt.imshow(arr, vmin=0, vmax=255)

When showing a numpy array, matplotlib can only automatically know the range of the actual input data (not the range it was taken from), so it takes the full input range an maps it to the full output range. But if you know a different range of the input data, you can use vmin and vmax to specify it.

tom10
  • 67,082
  • 10
  • 127
  • 137
  • Ok.Already looking at a code that used it, but not given attention. How do I know which vmax and vmax (imagine I have several of various types). with that? arr.min(), arr.max() (144, 225) Assuming vmin is always equal to 0? – neiesc Sep 17 '13 at 03:19
  • 1
    http://stackoverflow.com/questions/12760797/imshowimg-cmap-cm-gray-shows-a-white-for-128-value http://stackoverflow.com/questions/7404116/defining-the-midpoint-of-a-colormap-in-matplotlib – neiesc Sep 17 '13 at 03:49
  • I don't quite understand your comments, but for an image, it's usually the case that `vmin=0`. – tom10 Sep 17 '13 at 05:55
  • 2
    @neiesc `imshow` defaults to using `vmin=min(arr)` and `vmax=max(arr)`. You typically know something about the input value, like the maximum and minimum _possible_ values. If your input data is unit8 then the minimum is 0, and the maximum is 255. – tacaswell Sep 17 '13 at 15:05