1

I would like to calculate the average grayscale profile of an image. In my next code I have the evolution of the grayscale of all the pixels of the image, but how to make the average? To obtain a single curve, the average of all the others. Thank you

import imageio 
import numpy as np
from matplotlib.pyplot import *
from matplotlib import pyplot as plt
img = imread("lion.jpg")
#
red = img[:,:,0]
green = img[:,:,1]
blue = img[:,:,2]
#print(np.mean(img)
line = red[:,:]
#here How to calculate the average grayscale profile?
figure(figsize=(8,4))
plot(line)
plt.show()      
F.Moab
  • 25
  • 7

1 Answers1

1

If I understand correctly, you want to have a profile of the greyscale image along both directions of the image.

import numpy as np
from matplotlib import pyplot as plt
img = plt.imread("https://i.stack.imgur.com/9qe6z.png")

# convert to grayscale
gray = img.mean(axis=2)
# or
#gray = np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

# profile along x -> mean along y
prof_x = gray.mean(axis=0)
# profile along y -> mean along x
prof_y = gray.mean(axis=1)

fig, (ax, ay) = plt.subplots(nrows=2, sharex=True, sharey=True)
ax.plot(prof_x, label="x profile")
ay.plot(prof_y, color="C1", label="y profile")
fig.legend()
plt.show() 

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • O I didn't see the problem like that, Thanx a lot !! – F.Moab Jan 23 '19 at 03:54
  • it would theoretically be possible to convert the pixel value (Y axe) to temperature? To have the temperature flow on the image. @ImportanceOfBeingErnest – F.Moab Jan 24 '19 at 00:43
  • Sure, this seems unrelated, but suppose you have a function `f` that converts pixel value to temperature; then you can apply it to `img`, `f(img)`. – ImportanceOfBeingErnest Jan 24 '19 at 13:30
  • Thank you very much, I found it. Just one last question: if I have several greyscale profiles of an image at different times. Is there a function to plot the average of these profiles over time? with your method (code) @ImportanceOfBeingErnest – F.Moab Jan 25 '19 at 03:02