Colormap-Python/Matplotlib

Colormap-Python/Matplotlib,python,colors,matplotlib,Python,Colors,Matplotlib,我正在做一些图像处理,应用一些过滤器,比如sobel算子,来寻找边缘。因此,在应用运算符后,我执行以下操作: def classify_edges(magnitude, threshold=.75): """ Classifies a pixel as edge or not based on its magnitude, which is generated after the appliance of an operator :param ma

我正在做一些图像处理,应用一些过滤器,比如sobel算子,来寻找边缘。因此,在应用运算符后,我执行以下操作:

def classify_edges(magnitude, threshold=.75):
        """
        Classifies a pixel as edge or not based on its magnitude, which is generated after the appliance of an operator
        :param magnitude : the gradient magnitude of the pixel
        :return : the pixel classification
        """
        classification = Dt.Data()
        e = 0
        n = 0
        for x, y in product(range(classification.rows), range(classification.cols)):
            # Not edge
            if magnitude[x][y] > threshold:
                classification.data[x][y] = 1.0
                n += 1
            # Edge
            else:
                classification.data[x][y] = 0.0
                e += 1
        print e, n
        return classification
我根据像素的大小为其赋值(b或w),以获得边缘。因此,我遵循了模式1=真,对于is_edge,0=假,对于not_edge,我希望得到一个带有白色边和黑色背景的图像。但是我注意到我得到了相反的结果,0代表白色,1代表黑色。我通过打印值验证了这一点。因为我的边比背景少,所以边的数量比背景的数量少。如下图所示,我的边缘为白色,背景为黑色

这是我的绘图方法:

 def generate_data_black_and_white_heat_map(data, x_axis_label, y_axis_label, plot_title, file_path, box_plot=False):
        """
        Generate a heat map of the data
        :param data : the data to be saved
        :param x_axis_label : the x axis label of the data
        :param y_axis_label : the y axis label of the data
        :param plot_title : the title of the data
        :param file_path : the name of the file
        """
        plt.figure()
        plt.title(plot_title)
        plt.imshow(data.data, extent=[0, data.cols, data.rows, 0], cmap='binary')
        plt.xlabel(x_axis_label)
        plt.ylabel(y_axis_label)
        plt.savefig(file_path + '.png')
        plt.close()
我想知道matplotlib的黑色是1,白色是0,还是我做错了什么。我想这是由于我的cmpa='binary'。有一些关于如何进行转换的文档吗

先谢谢你

我想知道matplotlib的黑色是否为1,白色是否为0

的确如此

试试这个:

plt.imshow(data.data, extent=[0, data.cols, data.rows, 0], cmap='binary_r')
\u r
后缀可反转任何颜色映射。

很高兴知道!非常感谢。(: