簡體   English   中英

色彩圖-Python / Matplotlib

[英]Colormap - Python / 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),以獲取邊緣。 因此,我遵循模式1 = true(對於is_edge )和0 = False(對於not_edge) ,並期望獲得具有白色邊緣和黑色背景的圖像。 但是我注意到我得到了相反的結果,白色代表0,黑色代表1。 我驗證了此打印值。 由於邊緣比背景少,因此邊緣數小於背景數。 I為e >> n,如下圖所示,我的邊緣為白色,背景為黑色。

在此處輸入圖片說明

這是我的繪圖方法:

 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')

_r后綴可反轉任何顏色圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM