1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| 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 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()
|