1

I have a script that shows several images in matplotlib:

i = cv2.imread(sys.argv[1])

def cv2np(img):
    b, g, r = cv2.split(img)
    imn = cv2.merge([r,g,b])
    return imn

img = cv2np(i)
igg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
imb = cv2.GaussianBlur(igg,(7,7),0)
LoG = nd.gaussian_laplace(imb, 2)

f0 = plt.figure()
ax0 = f0.add_subplot(111)
ax0.imshow(i)
ax0.set_title('Raw Image')

f1 = plt.figure()
ax1 = f1.add_subplot(111)
ax1.imshow(img)
ax1.set_title('Image')

f1_5 = plt.figure()
ax1_5 = f1_5.add_subplot(111)
ax1_5.imshow(igg)
ax1_5.set_title('Grey')

f2 = plt.figure()
ax2 = f2.add_subplot(111)
ax2.imshow(imb)
ax2.set_title('Blur')

f3 = plt.figure()
ax3 = f3.add_subplot(111)
ax3.imshow(LoG)
ax3.set_title('LoG')

plt.show()

But the issue is with figure 1_5 Grey. At first, I was having an issue with the RGB/BGR opencv/numpy disparity, that I fixed with the cv2np function. But even though Image is the correct color, Grey is still the jet colormap:

enter image description here I don't see how this is happening: I ran the function to turn the image black-and-white:

igg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

there shouldn't be any colors left, so why is is jet? And when I try to run the function a second time (ax1_5.imshow(cv2np(igg))), it gives me an error:

Traceback (most recent call last):
  File "LaplacianOfGaussian--01-1.py", line 35, in <module>
    ax1_5.imshow(cv2np(igg))
  File "LaplacianOfGaussian--01-1.py", line 13, in cv2np
    b, g, r = cv2.split(img)
ValueError: need more than 1 value to unpack
##########################################################################/

So finally, I assumed that I had done something completely wrong, and so I wrote a completely new script from scratch to figure out the problem:

img = cv2.imread(sys.argv[1])

def cv2np(img):
    b, g, r = cv2.split(img)
    imn = cv2.merge([r,g,b])
    return imn

a = cv2np(img)
b = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
c = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
d = cv2.cvtColor(a, cv2.COLOR_RGB2GRAY)


f0 = plt.figure()
ax0 = f0.add_subplot(111)
ax0.imshow(img)
ax0.set_title('Raw Image')

f1 = plt.figure()
ax1 = f1.add_subplot(111)
ax1.imshow(a)
ax1.set_title('A')

f2 = plt.figure()
ax2 = f2.add_subplot(111)
ax2.imshow(b)
ax2.set_title('B')

f3 = plt.figure()
ax3 = f3.add_subplot(111)
ax3.imshow(c)
ax3.set_title('C')

f4 = plt.figure()
ax4 = f4.add_subplot(111)
ax4.imshow(c)
ax4.set_title('D')

plt.show()

And now, for some reason, every single one of those images is jet:

enter image description here

I don't understand: Where is the jet color coming from? All of the image cells should be either 0 or 255. Can someone explain this?

Rich
  • 1,103
  • 1
  • 15
  • 36
  • 3
    `imshow` uses `jet` as the default [colormap](http://matplotlib.org/users/colormaps.html) for grayscale images (in matplotlib 1.5 and earlier). To change it, use something like `cmap=plt.cm.gray` in the call of `imshow`. – Warren Weckesser Oct 11 '16 at 05:01
  • But why? That makes absolutely no sense. – Rich Oct 11 '16 at 05:05
  • 6
    You are displaying photographs, and indeed, `jet` is a terrible default for a black and white photo. Don't forget, though, that matplotlib is a scientific plotting library, and a common use of `imshow` is to display scientific data (e.g. 2-d arrays of pressures, temperatures, correlations, etc). In that case, it is quite common to use a colormap. (`jet` is still a poor default for that purpose, which is why matplotlib will change the default colormap in version 2.0.) – Warren Weckesser Oct 11 '16 at 05:14
  • That makes sense. So, is there a way for me to change the default colormap to one more suitable for image manipulations, it only for that script? – Rich Oct 11 '16 at 05:33
  • 1
    See if this helps: http://stackoverflow.com/questions/33185037/how-to-set-default-colormap-in-matplotlib – Warren Weckesser Oct 11 '16 at 05:37

0 Answers0