如何计算图像的平均灰度轮廓?

时间:2019-01-23 02:22:34

标签: python matplotlib

我想计算图像的平均灰度轮廓。 在我的下一个代码中,我将了解图像所有像素的灰度级变化,但是如何求平均值呢?要获得一条曲线,则为所有其他曲线的平均值。谢谢

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()      

1 个答案:

答案 0 :(得分:1)

如果我理解正确,则希望沿图像的两个方向都有灰度图像的轮廓。

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