Search code examples
contourfdigitization

Digitize a filled contour plot


I have an RGB bitmap. Actually it is the contour plot of some scalar field plotted with the 'jet' colormap. I need to reverse the bitmap and obtain the source data. Is there a ready-to-use ond open-source tool for that? Python module is OK too.


Solution

  • Well, as nobody did it, this is a lasy algorithm that do the job:

    import numpy as np
    import scipy.misc
    import matplotlib.pyplot as plt
    
    ## The digitized field will be scaled to range (0,1)
    scale = np.linspace(0.0, 1.0, 300)
    ## Palette is a curve in RGB space
    jet = plt.cm.get_cmap('jet')
    palette = 255.0 * np.array([ jet(s)[:3] for s in scale ])
    ## Read the field as RGB image
    field_0 = scipy.misc.imread('field.png')[:,:,:3]
    ny, nx, _ = field_0.shape
    ## Use Euclidian norm to find a closest point in the palette
    dist = lambda v : np.array([ np.linalg.norm(p - v) for p in palette ])
    field = np.array([ [ scale[np.argmin(dist(field_0[i,j]))]
                         for j in range(nx) ]
                       for i in range(ny)[::-1] ])
    
    ## Plot
    fig, ax = plt.subplots(1, 2)
    ax[0].imshow(field_0)
    ax[1].contourf(field, cmap='gray')
    plt.show()
    

    Thanks to everyone who cared.