计算灰度图的均值和方差

计算灰度图的均值和方差

文章目录

  • 计算灰度图的均值和方差
    • 均值
      • 代码实现
    • 方差
      • 代码实现

均值

图片的灰度均值是各个像素点的灰度值的平均值

代码实现

# 计算灰度均值
import cv2 as cv

img = cv.imread('t0.jpg', 0)
height, width = img.shape
size = img.size

average = 0
for i in range(height):
    for j in range(width):
        average += img[i][j]/size

print(average)

方差

计算灰度图的均值和方差_第1张图片

代码实现

# 计算灰度方差

import cv2 as cv

img = cv.imread('t0.jpg', 0)
height, width = img.shape
size = img.size

p = [0]*256

for i in range(height):
    for j in range(width):
        p[img[i][j]] += 1

m = 0
for i in range(256):
    p[i] /= 256
    m += i*p[i]

s = 0
for i in range(256):
    s += (i-m)*(i-m)*p[i]

print(s)

你可能感兴趣的:(数字图像处理)