如何解决每当我试图找到多个图像的平均RGB值时发生的轴误差

  • 本文关键字:图像 RGB 误差 解决 何解决 python numpy
  • 更新时间 :
  • 英文 :


我一直在创建一个程序,该程序将以列表的形式返回存储在文件夹中的所有图像的平均RGB值。

我已经编写了代码来查找单个图像的平均RGB值,如下所示。

import cv2 
import numpy as np 
myimg2 = cv2.imread('/Users/farzeent.farooqui/Desktop/colors.jpg') 
avg_color = np.array(myimg2).mean(axis=(0,1))
avg_rgb = avg_color[::-1]
print(avg_rgb)

然后我使用for循环来获得所有图像的RGB值

import cv2  
import numpy as np
import os                      
def test():
path_of_images = "/Users/farzeent.farooqui/Frames"
list_of_images = os.listdir(path_of_images)
for image in list_of_images:
myimg2 = cv2.imread(os.path.join(path_of_images, image) )
avg_color = np.array(myimg2).mean(axis=(0,1))
avg_rgb = avg_color[::-1]
print(avg_rgb)
test()

但当我尝试运行程序时,我不断收到以下错误:

---------------------------------------------------------------------------
AxisError                                 Traceback (most recent call last)
<ipython-input-124-fbd55f77ab7c> in <module>
----> 1 test()
<ipython-input-123-ed7d4e9de700> in test()
10 
11         myimg2 = cv2.imread(os.path.join(path_of_images, image) )
---> 12         avg_color = np.array(myimg2).mean(axis=(0,1))
13         avg_rgb = avg_color[::-1]
14         print(avg_rgb)
~/opt/anaconda3/lib/python3.8/site-packages/numpy/core/_methods.py in _mean(a,         axis, dtype, out, keepdims, where)
164     is_float16_result = False
165 
--> 166     rcount = _count_reduce_items(arr, axis, keepdims=keepdims, where=where)
167     if rcount == 0 if where is True else umr_any(rcount == 0):
168         warnings.warn("Mean of empty slice.", RuntimeWarning, stacklevel=2)
~/opt/anaconda3/lib/python3.8/site-packages/numpy/core/_methods.py in    _count_reduce_items(arr, axis, keepdims, where)
73         items = nt.intp(1)
74         for ax in axis:
---> 75             items *= arr.shape[mu.normalize_axis_index(ax, arr.ndim)]
76     else:
77         # TODO: Optimize case when `where` is broadcast along a non-reduction
AxisError: axis 0 is out of bounds for array of dimension 0

正如@nighthobbit在评论中指出的那样,您的目录可能包含非图像文件,这导致您的代码出现错误。

下面的代码将帮助您在特定目录中查找具有已知图像文件扩展名的文件。

from os import path, walk
def get_image_files(directory_of_images):
"""
This function is designed to traverse a directory tree and extract all
the image names contained in the directory.
:param directory_of_images: the name of the target directory containing
the images to be trained on.
:return: list of images to be processed.
"""
images_to_process = []
accepted_extensions = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.svg', '.tiff')
for (dirpath, dirnames, filenames) in walk(directory_of_images):
for filename in filenames:
if filename.endswith(accepted_extensions):
images_to_process.append(path.join(dirpath, filename))
return images_to_process

list_of_images = get_image_files('/Users/user_name/Python_Projects/scratch_pad')
for image_to_process in list_of_images:
myimg2 = cv2.imread(image_to_process)
avg_color = np.array(myimg2).mean(axis=(0, 1))
avg_rgb = avg_color[::-1]
print(avg_rgb)
# output
[239.08779842 239.08779842 239.08779842]
[186.64632099 163.64886914 150.54838519]

最新更新