File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #coding:utf-8
2+ #By:Eastmount CSDN 2021-02-05
3+ import cv2
4+ import numpy as np
5+ import matplotlib .pyplot as plt
6+
7+ #函数: 获取图像的灰度平均值
8+ def fun_mean (img , height , width ):
9+ sum_img = 0
10+ for i in range (height ):
11+ for j in range (width ):
12+ sum_img = sum_img + int (img [i ,j ])
13+ mean = sum_img / (height * width )
14+ return mean
15+
16+ #函数: 获取中位数
17+ def fun_median (data ):
18+ length = len (data )
19+ data .sort ()
20+ if (length % 2 )== 1 :
21+ z = length // 2
22+ y = data [z ]
23+ else :
24+ y = (int (data [length // 2 ]) + int (data [length // 2 - 1 ])) / 2
25+ return y
26+
27+ #读取图像
28+ img = cv2 .imread ('lena.bmp' )
29+
30+ #图像灰度转换
31+ grayImage = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
32+
33+ #获取图像高度和宽度
34+ height = grayImage .shape [0 ]
35+ width = grayImage .shape [1 ]
36+
37+ #计算图像的灰度平均值
38+ mean = fun_mean (grayImage , height , width )
39+ print ("灰度平均值:" , mean )
40+
41+ #计算图像的灰度中位数
42+ value = grayImage .ravel () #获取所有像素值
43+ median = fun_median (value )
44+ print ("灰度中值:" , median )
45+
46+ #计算图像的灰度标准差
47+ std = np .std (value , ddof = 1 )
48+ print ("灰度标准差" , std )
You can’t perform that action at this time.
0 commit comments