Colormap - Python / Matplotlib

时间:2014-03-21 21:03:46

标签: 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 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)来获得边缘。因此,对于 is_edge ,我遵循模式1 = True,对于 not_edge ,我遵循0 = False,并期望获得具有白色边缘和黑色背景的图像。但我注意到我正好相反,0为白色,1为黑色。我验证了这打印的价值。由于边缘少于背景,因此边缘数量小于背景数量。我为e>> n,并且,如下图所示,我的边缘为白色,背景为黑色。

enter image description here

这是我的情节方法:

 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 ='二进制'。有关于如何完成转换的文档?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

  

我想知道黑色是1还是白色0对于matplotlib

确实如此。

试试这个:

plt.imshow(data.data, extent=[0, data.cols, data.rows, 0], cmap='binary_r')

_r后缀会反转任何色卡。

相关问题