Matpliblib颜色图,中心为峰,边缘为零

问题描述

我正在寻找一个自定义颜色表,该颜色表突出显示中心(值1),边缘仅具有白色(值0和2)。理想情况下,应该存在从1到[0,2]的梯度。

通常的颜色图会做相反的事情:偏离中心(白色居中)。

感谢您的帮助

解决方法

您可以基于matplotlib中的可用颜色图进行创建。

import cv2

# Reading color image as grayscale
gray = cv2.imread("image.jpeg",0) # PLEASE MAKE SURE TO REPLACE WITH YOUR OWN IMAGE! AND the '0' is the code for the grey scaled image. if it is '1' then it will be back to normal i it's orginal color

# Showing grayscale image
cv2.imshow("Grayscale Image",gray)

# waiting for key event
cv2.waitKey(0)

# destroying all windows
cv2.destroyAllWindows()

enter image description here

,

您可以在matplotlib.colors模块中使用from_list中的LinearSegmentedColormap方法。

在这里,我们提供3种颜色作为列表(["white","red","white"])。可以通过更改这些颜色名称中的任何一个轻松地对其进行自定义。

例如:

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

cmap = LinearSegmentedColormap.from_list('wrw',["white","white"],N=256)

a = np.arange(0,2,0.01).reshape(20,10)
fig,ax = plt.subplots()

p = ax.pcolormesh(a,cmap=cmap,vmin=0,vmax=2)

fig.colorbar(p)

plt.show()

enter image description here