我正在尝试使用python计算图像的平均RGB值。我在这里找到了使用numpy的可接受方法。
How to find the average colour of an image in Python with OpenCV?
我可以使用以上答案中的示例图像来计算RGB平均值,但是无法计算我的图像(波纹管)的RGB平均值。
import os
import cv2
import numpy as np
path = ('C:/images')
img = cv2.imread(path + '/1049.jpg', 0)
img = np.array(img)
average = img.mean(axis=0).mean(axis =0)
print(average)
我收到以下错误。
Traceback (most recent call last):
File "c:/Users/isaac_madsen/Google Drive/Rhizoc_2018/image_stats.py", line 21, in <module>
average = img.mean(axis=0).mean(axis =0)
File "C:\Users\isaac_madsen\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\_methods.py", line 57, in _mean
rcount = _count_reduce_items(arr, axis)
File "C:\Users\isaac_madsen\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\_methods.py", line 50, in _count_reduce_items
items *= arr.shape[ax]
IndexError: tuple index out of range
我在非对称数组上找到了类似问题的答案,但是我不确定如何在我的特殊情况下实现解决方案,或者不确定我是否实际上在处理非对称数组。
答案 0 :(得分:0)
您可能要确保文件位于您要查找的位置。要验证,
BASE_PATH = 'C:/images'
FILE_PATH = os.path.join(BASE_PATH, '1049.jpg')
try:
fh = open(FILE_PATH, 'r')
img = cv2.imread(FILE_PATH, 0)
img = np.array(img)
average = img.mean(axis=0).mean(axis =0)
print(average)
except FileNotFoundError:
print(f"No file here: {FILE_PATH}")