Search code examples
pythonnumpyopencvimage-processingscikit-image

Python/numpy points list to black/white image area


I'm trying to convert a continuous list points (between 0 and 1) into black and white image, representing area under/over list points.

plt.plot(points)
plt.ylabel('True val')
plt.show()
print("Points shape-->", points.shape)

result

I can save the image produced by matplotlib but i think this could be a nasty workaround

At the end i would like to obtain and image with shape of (224,224) where white zone represent area under line and black zone represent are over line...

image_area = np.zeros((points.shape[0],points.shape[0],))
# ¿?

Any ideas or suggestions how to approach it are welcome! Thanks experts


Solution

  • Here is a basic example of how you could do it. Since the slicing requires integers, you may have to scale your raw data first.

    import numpy as np
    import matplotlib.pyplot as plt
    
    # your 2D image
    image_data = np.zeros((224, 224))
    
    # your points. Here I am just using a random list of points
    points = np.random.choice(224, size=224)
    
    # loop over each column in the image and set the values
    # under "points" equal to 1
    for col in range(len(image_data[0])):
        image_data[:points[col], col] = 1
    
    # show the final image
    plt.imshow(image_data, cmap='Greys')
    plt.show()
    
    

    enter image description here