Python 계산 이미지 RGB 평균 값 방식 구현

요구 하 다.
하나의 폴 더 에 몇 장의 그림 이 존재 합 니 다.각 그림 의 RGB 평균 값 을 계산 하고 모든 그림 의 RGB 평균 값 을 계산 해 야 합 니 다.
코드

# -*- 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)
cv2.imread()읽 기 순 서 는 BGR 문제 입 니 다.
주의 하 다.
경 로 는 중국어 가 나 올 수 없습니다.그렇지 않 으 면 오류 가 발생 하기 쉽 습 니 다.
오 류 는 다음 과 같 습 니 다:
TypeError: 'NoneType' object is not subscriptable
결실

이미지 픽 셀 RGB
나 는 쓸데없는 말 을 더 이상 하지 않 겠 으 니,모두들 코드 를 직접 보 는 것 이 좋 겠 다.

from PIL import Image
image = Image.open('./3.png')
print(image)#  mode    RGB,
image_rgb = image.convert("RGB")
image_rgb.getcolors()  
결과 인쇄

<PIL.PngImagePlugin.PngImageFile image mode=P size=500x332 at 0x7F53383FADA0>

[(10990, (192, 0, 128)),#10990      ,(192, 0, 128)  RGB 
 (7589, (224, 224, 192)),
 (5706, (192, 128, 128)),
 (3913, (0, 64, 0)),
 (137802, (0, 0, 0))]
이 파 이 썬 은 이미지 RGB 의 평균 값 을 계산 하 는 방식 을 실현 하 는 것 이 바로 편집장 이 여러분 에 게 공유 하 는 모든 내용 입 니 다.여러분 께 참고 가 되 고 저 희 를 많이 사랑 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기