Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problems low contrast image(contrast stretching) in matplotlib

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

inputlarge

Plot (no change this is automatic by matplotlib.)

plotlarge

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.

like image 327
neiesc Avatar asked Dec 16 '22 06:12

neiesc


1 Answers

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.

like image 109
tom10 Avatar answered Dec 29 '22 00:12

tom10