图像统计特征
熵
用于度量图像所具有的信息量,它反映了图像中纹理的紊乱度或复杂程度。熵值越大,说明纹理越复杂;熵值越小,说明纹理越平滑。
均值
灰度均值是对区域内亮度的一个度量,可以用来反应图像的明暗程度。
方差
方差就是数据的分散程度(偏离均值)。
对比度
反映了图像的清晰度和纹理沟纹深浅的程度。纹理沟纹越深,其对比度越大,视觉效果越清晰;反之,对比度小,则沟纹浅,效果模糊。
公式
测试图片-从均值、方差做对比
对于第一组六张图片,图片越辆均值越大,但方差不会改变(同一张图片只改变亮度)
对于第二组图片,对比度不做调节,只调节亮度的情况下,偏离均值的离散度会变大
测试图片-从对比度出发
对比度越大 视觉效果越清晰;纹理越深对比度越大
文章来源:https://www.toymoban.com/news/detail-593822.html
测试图片-从熵出发
熵值越大,说明纹理越复杂;熵值越小,说明纹理越平滑。
文章来源地址https://www.toymoban.com/news/detail-593822.html
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()
到了这里,关于从图像统计特征看图像内在-均值,方差,对比度,熵的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!