1

I am working with an example of how to calculate the value of each pixel in each row and column using the following formula using Python.

Formula is enter image description here

where M represents image matrix dimension N represents total number of pixels and P(i, j) is the color pixel value at ith row and jth column

petezurich
  • 9,280
  • 9
  • 43
  • 57
KABIL
  • 35
  • 8

2 Answers2

1

You can use numpy if you re using python

import numpy as np
import cv2 

img = cv2.imread('img.png')
# in case of overall mean
np.mean(img)

#in case of channel wise mean
np.mean(img, axis=(0, 1))

This can be applied if your matrix has rectangular shape

cristian hantig
  • 1,110
  • 1
  • 12
  • 16
  • thanks for your comment can u please explain me with an example so that i can clearly understand. – KABIL Jan 11 '20 at 11:46
1

The average of the pixel channels is fairly trivial to calculate, but not necessarily very meaningful - depending on the use case it is more illustrative to find the dominant colors. This answer demonstrates an approach to finding the dominant colors.

Here is a toy example for finding the channel-wise means of the image pixels using the image

enter image description here

import cv2
import numpy as np

im = cv2.imread('opencvtest.png')

# Find the average of each channel across the image
mean = [np.mean(im[:,:,i]) for i in range(im.shape[2])]

print(mean)
[132.6709785196566, 92.74899063496903, 81.57176387455432]

So the average color the code found is

which is consistent with what I would expect from looking at the test image.

William Miller
  • 9,839
  • 3
  • 25
  • 46