国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Python實現(xiàn)計算圖像RGB均值方式

瀏覽:2日期:2022-07-23 08:36:55

要求

存在一個文件夾內(nèi)有若干張圖像,需要計算每張圖片的RGB均值,并計算全部圖像的RGB均值。

代碼

# -*- coding: utf-8 -*-'''Created on Thu Nov 1 10:43:29 2018@author: Administrator'''import osimport cv2import 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問題。

注意

路徑不能出現(xiàn)中文,不然容易出錯。

錯誤如下:

TypeError: ’NoneType’ object is not subscriptable

結(jié)果

Python實現(xiàn)計算圖像RGB均值方式

補充知識:Image得到圖片像素的RGB

我就廢話不多說了,大家還是直接看代碼吧!

from PIL import Imageimage = Image.open(’./3.png’)print(image)#查看mode是否等于RGB,image_rgb = image.convert('RGB')image_rgb.getcolors()

打印結(jié)果

<PIL.PngImagePlugin.PngImageFile image mode=P size=500x332 at 0x7F53383FADA0>[(10990, (192, 0, 128)),#10990表示像素總數(shù),(192, 0, 128)表示RGB值 (7589, (224, 224, 192)), (5706, (192, 128, 128)), (3913, (0, 64, 0)), (137802, (0, 0, 0))]

以上這篇Python實現(xiàn)計算圖像RGB均值方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 库车县| 遂昌县| 古蔺县| 大理市| 正蓝旗| 普陀区| 临武县| 长治县| 介休市| 昆明市| 阿鲁科尔沁旗| 红安县| 双鸭山市| 九龙县| 金门县| 岳西县| 金昌市| 桑日县| 河间市| 罗城| 精河县| 铜川市| 长治市| 澎湖县| 古浪县| 景泰县| 金塔县| 青州市| 正镶白旗| 大连市| 灵石县| 利津县| 太保市| 通许县| 湟中县| 临江市| 孟津县| 南丰县| 西林县| 凤庆县| 芒康县|