How to create heatmap over an image using coordinate points?

前端 未结 1 1020
独厮守ぢ
独厮守ぢ 2021-01-16 09:33

I have a pathology image of size 1024 x 1360. I have probability values of some areas and coordinate points. How can I write a code for heatmap generation using the coordina

相关标签:
1条回答
  • 2021-01-16 09:56

    Clean your data generating the heatmap

    First, if you are not comfortable with deep-nested data, you should clean your data in your CSV file (they are not uniform and have duplicates - they are also error-prone if you want rectangles).

    The simplest example being the following:

     x, y, x1, y1, Probability value
     0, 0, 5, 10, 0.5
     50, 45, 55, 65, 0.9
     100, 150, 120, 200, 0.3
     1000, 1005, 1005, 1010, 1
    

    The answer below has been written with this clean CSV dataset in mind.

    Use Pandas to process CSV data files

    Seeing what your use case is, I recommend using pandas in order to process your CSV data files.

    You can store data from a CSV file in a pandas DataFrame this way:

    df = pd.read_csv("data.csv")
    

    and iterate over rows, using the first row as keys for each column value, as following:

    for index, row in df.iterrows():
        print(row["x"], row["y"], row["x1"], row["y1"], 
          row["Probability value"]         
    

    Full working snippet

    This snippet is not very pretty but it works for the dummy dataset you've provided and is intended to be pretty self-explanatory with the above introduction. Some tweaking might be needed, especially for the plotting part.

    #!/usr/bin/python3
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    from skimage import io
    from skimage.color import rgb2gray
    import matplotlib as mpl
    # Read original image
    img = io.imread('img.jpg')
    
    # Get the dimensions of the original image
    x_dim, y_dim, z_dim = np.shape(img)
    
    # Create heatmap
    heatmap = np.zeros((x_dim, y_dim), dtype=float)
    
    # Read CSV with a Pandas DataFrame
    df = pd.read_csv("data.csv")
    
    # Set probabilities values to specific indexes in the heatmap
    for index, row in df.iterrows():
        x = np.int(row["x"])
        y = np.int(row["y"])
        x1 = np.int(row["x1"])
        y1 = np.int(row["y1"])
        p = row["Probability value"]
        heatmap[x:x1,y:y1] = p
    
    # Plot images
    fig, axes = plt.subplots(1, 2, figsize=(8, 4))
    ax = axes.ravel()
    
    ax[0].imshow(img)
    ax[0].set_title("Original")
    fig.colorbar(ax[0].imshow(img), ax=ax[0])
    
    ax[1].imshow(img, vmin=0, vmax=1)
    ax[1].imshow(heatmap, alpha=.5, cmap='jet')
    ax[1].set_title("Original + heatmap")
    
    # Specific colorbar
    norm = mpl.colors.Normalize(vmin=0,vmax=2)
    N = 11
    cmap = plt.get_cmap('jet',N)
    sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
    sm.set_array([])
    plt.colorbar(sm, ticks=np.linspace(0,1,N), 
                 boundaries=np.arange(0,1.1,0.1)) 
    
    fig.tight_layout()
    plt.show()
    
    0 讨论(0)
提交回复
热议问题