Python实现计算图像RGB均值,怎么只读一张图片？

# -*- coding: utf-8 -*-

"""
Created on Thu Nov 1 10:43:29 2018
"""

import os
import cv2
import numpy as np

def compute(path):
file_names = os.listdir(path)
per_image_Rmean = []
per_image_Gmean = []
per_image_Bmean = []
for file_name in file_names:
per_image_Bmean.append(np.mean(img[:,:,0]))
per_image_Gmean.append(np.mean(img[:,:,1]))
per_image_Rmean.append(np.mean(img[:,:,2]))
R_mean = np.mean(per_image_Rmean)
G_mean = np.mean(per_image_Gmean)
B_mean = np.mean(per_image_Bmean)
return R_mean, G_mean, B_mean

if name == '__main__':
R, G, B= compute(path)
print(R, G ,B) 8 个月之前 回复

2个回答

path = '16.png'
def compute(path):

``````image_Rmean = []
image_Gmean = []
image_Bmean = []
image_Bmean.append(np.mean(img[:,:,0]))
image_Gmean.append(np.mean(img[:,:,1]))
image_Rmean.append(np.mean(img[:,:,2]))
R_mean = np.mean(image_Rmean)
G_mean = np.mean(image_Gmean)
B_mean = np.mean(image_Bmean)
return R_mean, G_mean, B_mean
``````

if name == '__main__':
R, G, B= compute(path)
Y = 0.299*R+0.587*G+0.114*B
print("目标图片亮度为"+str(round(Y,2)))

``````import os
import cv2
import numpy as np

def compute(path):
file_names = os.listdir(path)
#   per_image_Rmean = []
#   per_image_Gmean = []
#   per_image_Bmean = []
image_mean = []

#   for file_name in file_names:

image_mean.append(np.mean(img[:,:,0]))
image_mean.append(np.mean(img[:,:,1]))
image_mean.append(np.mean(img[:,:,2]))

image_mean = np.mean(image_mean)
#   R_mean = np.mean(per_image_Rmean)
#   G_mean = np.mean(per_image_Gmean)
#   B_mean = np.mean(per_image_Bmean)

return image_mean

if __name__ == '__main__':
imageMean = compute(path)
print(imageMean)
`````` JonathanYan 回复燢皌: 我都没注意，你原程序就没写对，是__name__。是均值
8 个月之前 回复 8 个月之前 回复 8 个月之前 回复 