0

I am attempting to calculate the mean RGB values for the an image using python. I found an acceptable method using numpy here.

How to find the average colour of an image in Python with OpenCV?

I am able to calculate the mean RGB values using the example image in the above answer, but I am not able to calculate the mean RGB values of my image (bellow).

enter image description here

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)

I get the following error.

    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

I found this answer to a similar question on asymmetric arrays, but I am unsure how to implement the solution in my particular case, or if I am actually dealing with an asymmetric array.

Means of asymmetric arrays in numpy

1 Answers1

0

You may want to ensure a file exists at the location you're looking. To verify,

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}")
Wes Doyle
  • 2,199
  • 3
  • 17
  • 32