1. 程式人生 > >【預處理】計算RGB通道均值

【預處理】計算RGB通道均值

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time: 2018/8/16
# @Author: xfLi
# 計算RGB通道均值


import os
import cv2
import numpy as np


path = './datasets/train'
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_Rmean.append(np.mean(img[:,:,0]))
        per_image_Gmean.append(np.mean(img[:,:,1]))
        per_image_Bmean.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)