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 의 평균 값 을 계산 하 는 방식 을 실현 하 는 것 이 바로 편집장 이 여러분 에 게 공유 하 는 모든 내용 입 니 다.여러분 께 참고 가 되 고 저 희 를 많이 사랑 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.