li450813298
燢皌
采纳率40%
2019-08-24 16:54 阅读 870

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

网上用python实现计算图像RGB均值都是批量的,而我只想得到一张图片的rgb均值(即整张图片三个通道的三个均值)如下的代码是一位大神的代码,对于刚学python的小白不会修改,求大神帮助。总之就是将下面代码从批量变为只分析一张图片的。问题应该简单,原谅我的无知。这是源码地址:https://blog.csdn.net/yql_617540298/article/details/83617512

-*- coding: utf-8 -*-

"""
Created on Thu Nov 1 10:43:29 2018
@author: Administrator
"""

import os
import cv2
import numpy as np

path = 'C:/Users/Administrator/Desktop/rgb'
def compute(path):
file_names = os.listdir(path)
per_image_Rmean = []
per_image_Gmean = []
per_image_Bmean = []
for file_name in file_names:
img = cv2.imread(os.path.join(path, file_name), 1)
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)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    li450813298 燢皌 2019-08-25 15:02

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

    image_Rmean = []
    image_Gmean = []
    image_Bmean = []
    img = cv2.imread(path, 1)
    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)))

    点赞 评论 复制链接分享
  • JonathanYan JonathanYan 2019-08-24 18:20
    import os
    import cv2
    import numpy as np
    
    path = 'C:/Users/Administrator/Desktop/rgb/file.jpg'
    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:
    
        img = cv2.imread(os.path.join(path, file_name), 1)
        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)
    
    点赞 评论 复制链接分享

相关推荐