Image statistical characteristics
entropy
It is used to measure the amount of information an image has, which reflects the disorder or complexity of textures in the image. The larger the entropy value, the more complex the texture; the smaller the entropy value, the smoother the texture.
mean
The grayscale mean is a measure of the brightness within a region and can be used to reflect the brightness and darkness of the image.
variance
Variance is the spread (deviation from the mean) of the data.
Contrast
Reflects the clarity of the image and the depth of the texture grooves. The deeper the texture grooves, the greater the contrast and the clearer the visual effect; conversely, the smaller the contrast, the shallower the grooves and the blurred effect.
official
Test pictures-comparison based on mean and variance
For the first group of six pictures, the mean value is larger as the picture becomes larger, but the variance does not change (the same picture only changes the brightness).
For the second group of pictures, the contrast is not adjusted, and only the brightness is adjusted, the deviation from the mean is discrete. The degree will become larger
Test pictures - starting from contrast
The greater the contrast, the clearer the visual effect; the deeper the texture, the greater the contrast.
Test pictures - starting from entropy
The larger the entropy value, the more complex the texture; the smaller the entropy value, the smoother the texture.
img_1 = cv2.imread('office_1.jpg')
img_2 = cv2.imread('office_2.jpg')
img_3 = cv2.imread('office_3.jpg')
img_4 = cv2.imread('office_4.jpg')
img_5 = cv2.imread('office_5.jpg')
img_6 = cv2.imread('office_6.jpg')
img_7 = cv2.imread('dalishi.jpg')
img_8 = cv2.imread('wall.jpg')
img_9 = cv2.imread('muwen.jpg')
img_10 = cv2.imread('shuiniwen.jpg')
img_11 = cv2.imread('400x400_zhi_wenli.jpg')
img_12 = cv2.imread('400x400_zhi_zhezhouwenli.jpg')
imgs = [img_1,img_2,img_3,img_4,img_5,img_6]
titles = ['office_1.jpg','office_2.jpg','office_3.jpg','office_4.jpg','office_5.jpg','office_6.jpg']
for m in range(6):
plt.subplot(2,3,m + 1)
plt.imshow(imgs[m])
plt.title(titles[m])
def rgb2gray(img):
h = img.shape[0]
w = img.shape[1]
gray = np.uint8(np.zeros((h,w)))
for i in range(h):
for j in range(w):
gray[i,j] = 0.144 * img[i,j,0] + 0.587 * img[i,j,1] + 0.299 * img[i,j,2] # BGR
return gray
def average(img):
img1 = rgb2gray(img)
height,width = img1.shape
size = img1.size
ave = 0
for i in range(height):
for j in range(width):
ave += img1[i][j] / size
return ave
def contrast(img):
img1 = rgb2gray(img)
m,n = img1.shape
# 图片矩阵向外扩展一个像素
img1_ext=cv2.copyMakeBorder(img1,1,1,1,1,cv2.BORDER_REPLICATE) # 用边界颜色填充
height,width = img1_ext.shape
b = 0.0
for i in range(1,height - 1):
for j in range(1,width - 1):
b += (int((img1_ext[i,j]) - int(img1_ext[i,j + 1])) ** 2 + (
int(img1_ext[i,j]) - int(img1_ext[i,j - 1])) ** 2 + (
int(img1_ext[i,j]) - int(img1_ext[i + 1,j])) ** 2 + (
int(img1_ext[i,j]) - int(img1_ext[i - 1,j])) ** 2)
cg = b / (4 * (m - 2) * (n - 2) + 3 * (2 * (m - 2) + 2 * (n - 2)) + 2 * 4) #
return cg
def variance(img):
img1 = rgb2gray(img)
height,width = img1.shape
var = 0
size = img1.size
average = 0
for i in range(height):
for j in range(width):
average += img1[i][j] / size
for i in range(height):
for j in range(width):
var += img1[i,j] * (i - average) ** 2
return var
def Contrast_and_Brightness(alpha,bete,img):
blank = np.zeros(img.shape,img.dtype)
dst = cv2.addWeighted(img,alpha,blank,1 - alpha,bete)
return dst
def dec2bin(p):
floatbinstr = ""
if p == 0:
return floatbinstr
for kk in range(len(str(p)) - 2):
p *= 2
if p > 1:
floatbinstr += "1"
p = p - int(p)
else:
floatbinstr += "0"
if p == 0:
break
return str(floatbinstr)
def total_entropy(img):
n = []
P = []
lenavg = []
avg_sum = 0
grey_lvl = 0
k = 0
res = 0
# test = [[5,4,3,2,1]]
weight = img.shape[0]
height = img.shape[1]
total = weight * height
for i in range(256):
n.append(0)
for i in range(weight):
for j in range(height):
grey_lvl = img[i][j]
n[grey_lvl] = float(n[grey_lvl] + 1)
k = float(k + 1)
for i in range(256):
P.append(0)
P = n
for i in range(len(n)):
P[i] = (n[i] / k)
for i in range(256):
lenavg.append(0)
lenavg = P
for i in range(len(n)):
if P[i] == 0.0:
continue
lenavg[i] = lenavg[i] * len(dec2bin(lenavg[i]))
avg_sum = lenavg[i] + avg_sum
for i in range(len(n)):
if (P[i] == 0):
res = res
else:
res = float(res - P[i] * (math.log(P[i]) / math.log(2.0)))
return res
if __name__ == '__main__':
if input(keyboard.wait('A')):
for i in range(6):
ave_1 = average(imgs[i])
ave_1 = Decimal(ave_1).quantize(Decimal("0.000"))
print("average_office" + "_" + "123456"[i],ave_1)
with open('E:\\untitled12\\image_practice\\entropy.txt','a',encoding='utf-8') as f:
f.write('{:^30}\n'.format(str(ave_1)))
f.close()
plt.show()
if input(keyboard.wait('V')):
for i in range(6):
var_1 = variance(img_1)
var_1 = Decimal(var_1).quantize(Decimal("0.000"))
print("variance_office" + "_" + "123456"[i],var_1)
img_contrast = Contrast_and_Brightness(2.0,0,img_1)
var_orign = variance(img_1)
var_con = variance(img_contrast)
print("原图: ",var_orign)
print("原图调节对比:",var_con)
plt.subplot(1,2,1),plt.imshow(img_1),plt.title('img_1') plt.subplot(1,2,2),plt.imshow(img_contrast),plt.title('img_contrast')
plt.show()
if input(keyboard.wait('C')):
con_1 = contrast(img_1)
plt.subplot(2,2,1),plt.imshow(img_1),plt.title('bangong')
img_GAUSS = cv2.GaussianBlur(img_1,(9,9),0)
con_2 = contrast(img_GAUSS)
plt.subplot(2,2,2),plt.imshow(img_GAUSS),plt.title('bangong_GUSS')
con_3 = contrast(img_9)
plt.subplot(2,2,3),plt.imshow(img_9),plt.title('mu_wen')
con_4 = contrast(img_10)
plt.subplot(2,2,4),plt.imshow(img_10),plt.title('shui_ni_wen')
con_1 = Decimal(con_1).quantize(Decimal("0.000"))
con_2 = Decimal(con_2).quantize(Decimal("0.000"))
print("contrast_office" + "_oringel",con_1)
print("contrast_office" + "_GAUSS ",con_2)
con_3 = Decimal(con_3).quantize(Decimal("0.000"))
con_4 = Decimal(con_4).quantize(Decimal("0.000"))
print("木纹 ",con_3)
print("水泥纹",con_4)
plt.show()
if input(keyboard.wait('E')):
img_grey = cv2.imread('dalishi.jpg',cv2.IMREAD_GRAYSCALE)
img_grey1 = cv2.imread('wall.jpg',cv2.IMREAD_GRAYSCALE)
img_grey2 = cv2.imread('muwen.jpg',cv2.IMREAD_GRAYSCALE)
img_wenli = cv2.imread('400x400_zhi_wenli.jpg',cv2.IMREAD_GRAYSCALE)
img_wenli1 = cv2.imread('400x400_zhi_zhezhouwenli.jpg',cv2.IMREAD_GRAYSCALE)
ent_dalishi = total_entropy(img_grey)
plt.subplot(1,3,1),plt.imshow(img_7)
ent_wall = total_entropy(img_grey1)
plt.subplot(1,3,2),plt.imshow(img_8)
ent_muwen = total_entropy(img_grey2)
plt.subplot(1,3,3),plt.imshow(img_9)
ent_wenli = total_entropy(img_wenli)
plt.subplot(1,2,1),plt.imshow(img_11),plt.title('junyun_weli')
ent_wenli1 = total_entropy(img_wenli1)
plt.subplot(1,2,2),plt.imshow(img_12),plt.title('not_junrun_wen')
print("entropy_dalishi",ent_dalishi)
print("entropy_wall ",ent_wall)
print("entrop_muwen ",ent_muwen)
print("E_junrun_wenli: ",ent_wenli)
print("E_NOT_junrun_wenli1: ",ent_wenli1)
plt.show()