Matplotlib: What is the cmap function in imshow?

I am trying to learn opencv using python and came across this code below:

import cv2 import numpy as np from matplotlib import pyplot as plt BLUE = [255,0,0] img1 = cv2.imread('opencv_logo.png') replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE) plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL') plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE') plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT') plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101') plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP') plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT') plt.show() 

source: http://docs.opencv.org/master/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#exercises

What does plt.imshow (img1, 'gray') do? I tried searching on Google, and all I could understand was that the β€œgray” argument was the color map. But my image (pic is present on the site, see the Link) does not appear in shades of gray. I tried to remove the second argument. So the code was similar to plt.imshow (img1). He is running. The image remains the same as before. Then what makes the second argument β€œgray”? Can someone explain all this to me? Any help appreciated. Thanks.

PS. I am completely new to Matplotlib

+6
source share
1 answer

When img1 is of the form (M,N,3) or (M,N,4) , the values ​​in img1 interpreted as RGB or RGBA values. In this case, cmap is ignored. Per help(plt.imshow) docstring :

cmap: ~matplotlib.colors.Colormap , optional, default: None

If None, the default value is rc image.cmap . cmap ignored when X has RGB (A) information

However, if img was an array of form (M,N) , then cmap controls the color used to display the values.


 import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.axes_grid1 as axes_grid1 np.random.seed(1) data = np.random.randn(10, 10) fig = plt.figure() grid = axes_grid1.AxesGrid( fig, 111, nrows_ncols=(1, 2), axes_pad = 0.5, cbar_location = "right", cbar_mode="each", cbar_size="15%", cbar_pad="5%",) im0 = grid[0].imshow(data, cmap='gray', interpolation='nearest') grid.cbar_axes[0].colorbar(im0) im1 = grid[1].imshow(data, cmap='jet', interpolation='nearest') grid.cbar_axes[1].colorbar(im1) plt.savefig('/tmp/test.png', bbox_inches='tight', pad_inches=0.0, dpi=200,) 

enter image description here

+9
source

Source: https://habr.com/ru/post/974694/


All Articles