Kimmeridgian
Kimmeridgian

Reputation: 11

Plot the mean values of pixels on a single graph

I have a series of images (each 2 seconds a new image is taken) and want to calculate the mean pixel brightness for each image.

How can I plot the means on a single graphic, so that I have the evolution of the mean?

Below is the simple code I wrote to retrieve the mean values.

data = glob.glob ("D:/Documents/597/04/*.png")
data.sort()
    
for i in range (0, len(data)):
    img = cv.imread(data[i])
    if img is not None:
        img = np.array(img[:, :, 0])
        mean = format(img.mean())
        print(mean)

Upvotes: 1

Views: 416

Answers (1)

Nils Werner
Nils Werner

Reputation: 36765

Stack the images in one common array and do the mean in one go:

files = sorted(glob.glob("D:/Documents/597/04/*.png"))
imgs = [cv.imread(f) for f in files]     # list of 67 color images with 256x256 pixels each
imgs = np.array(imgs)                    # array of shape 67x256x256x3
plt.plot(imgs.mean(axis=(1, 2, 3))       # array of shape 67

Upvotes: 1

Related Questions