如何用Numpy取多张图片RGB单通道的平均值Python?
How to get the average value of RGB single channel of multiple images with Numpy Python?
我正在尝试提取位于特定文件夹 ('image') 中的多个图像的特征。
我想使用 tabulate 包将此功能(灰度、R、G、B、alpha、高度和宽度)插入到 table 中。
主要问题是遍历文件夹并提取单个红色、绿色、蓝色通道的平均值,以及获取灰色值(如果该比例中存在图像)。之后,我想将所有信息插入 table。我无法执行任何此操作。
我试过这个代码,但它给了我很多像素值。
我想得到平均值,所以每个通道只有一个数字。
拜托,有人可以帮忙吗?
```
import os
import numpy as np
import cv2
for img in os.listdir(image_path):
img = cv2.imread(os.path.join(image_path, img))
r,g,b = cv2.split(img)
rgb_img = cv2.merge((r,g,b))
x,y,z = np.shape(img)
red = np.zeros((x,y,z),dtype=int)
green = np.zeros((x,y,z),dtype=int)
blue = np.zeros((x,y,z),dtype=int)
for i in range(0,x):
for j in range(0,y):
red[i][j][0] = rgb_img[i][j][0]
green[i][j][1]= rgb_img[i][j][1]
blue[i][j][2] = rgb_img[i][j][2] ```
我不知道还有什么方法,有人可以推荐一个吗?我真的很感激。
谢谢
如果您想要通道(R、G、B)的平均值,并最终希望使用制表将其存储在 table 中,您可以执行以下操作:
我将使用 OpenCV(加载图像)和 numpy(因为为什么不)
import cv2
import numpy as np
import os
from tabulate import tabulate
images = os.listdir('./Test')
num_images = len(images)
data = [] # using an array is more convenient for tabulate.
for i in range(num_images):
img = cv2.imread('./Test/' + images[i])
avgR = np.mean(img[:,:,2])
avgG = np.mean(img[:,:,1])
avgB = np.mean(img[:,:,0])
data.append([images[i], avgR, avgG, avgB])
print(tabulate(data, headers=['img_name','R', 'G', 'B'], tablefmt='fancy_grid'))
'''
╒════════════╤══════════╤══════════╤══════════╕
│ img_name │ R │ G │ B │
╞════════════╪══════════╪══════════╪══════════╡
│ test1.jpg │ 49.3213 │ 112.408 │ 145.949 │
├────────────┼──────────┼──────────┼──────────┤
│ test2.jpeg │ 93.0038 │ 94.4466 │ 95.0824 │
├────────────┼──────────┼──────────┼──────────┤
│ test3.jpg │ 100.181 │ 71.6575 │ 66.4233 │
╘════════════╧══════════╧══════════╧══════════╛
'''
这是我的目录结构:
代码存在于 movie scraper 目录中。因此我使用了路径“./Test”。点'.'代表当前目录。
我正在尝试提取位于特定文件夹 ('image') 中的多个图像的特征。 我想使用 tabulate 包将此功能(灰度、R、G、B、alpha、高度和宽度)插入到 table 中。 主要问题是遍历文件夹并提取单个红色、绿色、蓝色通道的平均值,以及获取灰色值(如果该比例中存在图像)。之后,我想将所有信息插入 table。我无法执行任何此操作。 我试过这个代码,但它给了我很多像素值。 我想得到平均值,所以每个通道只有一个数字。 拜托,有人可以帮忙吗?
```
import os
import numpy as np
import cv2
for img in os.listdir(image_path):
img = cv2.imread(os.path.join(image_path, img))
r,g,b = cv2.split(img)
rgb_img = cv2.merge((r,g,b))
x,y,z = np.shape(img)
red = np.zeros((x,y,z),dtype=int)
green = np.zeros((x,y,z),dtype=int)
blue = np.zeros((x,y,z),dtype=int)
for i in range(0,x):
for j in range(0,y):
red[i][j][0] = rgb_img[i][j][0]
green[i][j][1]= rgb_img[i][j][1]
blue[i][j][2] = rgb_img[i][j][2] ```
我不知道还有什么方法,有人可以推荐一个吗?我真的很感激。 谢谢
如果您想要通道(R、G、B)的平均值,并最终希望使用制表将其存储在 table 中,您可以执行以下操作:
我将使用 OpenCV(加载图像)和 numpy(因为为什么不)
import cv2
import numpy as np
import os
from tabulate import tabulate
images = os.listdir('./Test')
num_images = len(images)
data = [] # using an array is more convenient for tabulate.
for i in range(num_images):
img = cv2.imread('./Test/' + images[i])
avgR = np.mean(img[:,:,2])
avgG = np.mean(img[:,:,1])
avgB = np.mean(img[:,:,0])
data.append([images[i], avgR, avgG, avgB])
print(tabulate(data, headers=['img_name','R', 'G', 'B'], tablefmt='fancy_grid'))
'''
╒════════════╤══════════╤══════════╤══════════╕
│ img_name │ R │ G │ B │
╞════════════╪══════════╪══════════╪══════════╡
│ test1.jpg │ 49.3213 │ 112.408 │ 145.949 │
├────────────┼──────────┼──────────┼──────────┤
│ test2.jpeg │ 93.0038 │ 94.4466 │ 95.0824 │
├────────────┼──────────┼──────────┼──────────┤
│ test3.jpg │ 100.181 │ 71.6575 │ 66.4233 │
╘════════════╧══════════╧══════════╧══════════╛
'''
这是我的目录结构:
代码存在于 movie scraper 目录中。因此我使用了路径“./Test”。点'.'代表当前目录。