import cv2 as cv import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams defgraybar(img,win_num,img_name,flag): num=np.zeros((256)) img_shape=img.shape if flag: for i inrange(img_shape[0]): for j inrange(img_shape[1]): num[img[i][j][0]]+=1 else: for i inrange(img_shape[0]): for j inrange(img_shape[1]): if img[i][j][0]==0: img[i][j][0]=255 num[img[i][j][0]]+=1 fig=plt.figure(win_num) rects=plt.bar(range(256),num,0.2) plt.title('gray_bar_'+img_name) plt.show() defflag(str): if'1'instr: return0 return1 filename=['citywall.bmp','citywall1.bmp','citywall2.bmp','elain.bmp','elain1.bmp','elain2.bmp','elain3.bmp','lena.bmp','lena1.bmp','lena2.bmp','lena3.bmp','woman.BMP','woman1.bmp','woman2.bmp'] for i in filename: img=cv.imread(i) graybar(img,0,i,flag(i))
import cv2 as cv import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams defgraybar_trans(img,flag): num=np.zeros((256)) img_shape=img.shape if flag: for i inrange(img_shape[0]): for j inrange(img_shape[1]): num[img[i][j][0]]+=1 else: for i inrange(img_shape[0]): for j inrange(img_shape[1]): if img[i][j][0]==0: img[i][j][0]=255 num[img[i][j][0]]+=1 fre=num/np.sum(num) trans=np.zeros((256)) trans[0]=fre[0]*256 for i inrange(256): if i: trans[i]=trans[i-1]+fre[i]*255 trans=trans.astype(np.uint8) new_img=np.zeros((img_shape[0],img_shape[1])) for i inrange(img_shape[0]): for j inrange(img_shape[1]): new_img[i][j]=trans[img[i][j][0]] return new_img.astype(np.uint8) defflag(str): if'1'instr: return0 return1 filename=['citywall.bmp','citywall1.bmp','citywall2.bmp','elain.bmp','elain1.bmp','elain2.bmp','elain3.bmp','lena.bmp','lena1.bmp','lena2.bmp','lena3.bmp','woman.BMP','woman1.bmp','woman2.bmp'] for i in filename: img=cv.imread(i) result=graybar_trans(img,flag(i)) # cv.namedWindow(i,cv.WINDOW_FREERATIO) # cv.imshow(i,result) # cv.waitKey(0) cv.imwrite('bar_'+i,result)
import cv2 as cv import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams deffind(a,x): shape_tmp=a.shape for i inrange(shape_tmp[0]): if x-a[i]<=0: return i return shape_tmp[0]-1 defgraybar_trans(img,cnt,flag): file_tem=['citywall.bmp','elain.bmp','lena.bmp','woman.bmp'] img_tem=cv.imread(file_tem[cnt]) num,num_temp=np.zeros((256)),np.zeros((256)) img_shape=img.shape if flag: for i inrange(img_shape[0]): for j inrange(img_shape[1]): num[img[i][j][0]]+=1 num_temp[img_tem[i][j][0]]+=1 else: for i inrange(img_shape[0]): for j inrange(img_shape[1]): if img[i][j][0]==0: img[i][j][0]=255 num[img[i][j][0]]+=1 num_temp[img_tem[i][j][0]]+=1 fre=num/np.sum(num) fre_temp=num_temp/np.sum(num_temp) z,z_temp=np.zeros((256)),np.zeros((256)) z[0],z_temp[0]=fre[0]*256,fre_temp[0]*256 for i inrange(256): if i: z[i]=z[i-1]+fre[i]*255 z_temp[i]=z_temp[i-1]+fre_temp[i]*255 trans=np.zeros((256)) for i inrange(256): trans[i]=find(z_temp,z[i]) trans=trans.astype(np.uint8) new_img=np.zeros((img_shape[0],img_shape[1])) for i inrange(img_shape[0]): for j inrange(img_shape[1]): new_img[i][j]=trans[img[i][j][0]] return new_img.astype(np.uint8) defcnt(str): if'citywall'instr: return0 if'elain'instr: return1 if'lena'instr: return2 if'woman'instr: return3 defflag(str): if'1'instr: return0 return1 filename=['citywall.bmp','citywall1.bmp','citywall2.bmp','elain.bmp','elain1.bmp','elain2.bmp','elain3.bmp','lena.bmp','lena1.bmp','lena2.bmp','lena3.bmp','woman.BMP','woman1.bmp','woman2.bmp'] for i in filename: img=cv.imread(i) result=graybar_trans(img,cnt(i),flag(i)) # cv.namedWindow(i,cv.WINDOW_FREERATIO) # cv.imshow(i,result) # cv.waitKey(0) cv.imwrite("aim_bar_"+i,result)
deftrans(imname): img=cv.imread(imname) shape=img.shape cnt_i=shape[0]//7+1 cnt_j=shape[0]//7+1 result=np.zeros((shape[0],shape[1])) for i inrange(cnt_i): for j inrange(cnt_j): num=np.zeros(256) for point_i inrange(i*7,i*7+7): for point_j inrange(j*7,j*7+7): if point_i<shape[0] and point_j<shape[1]: num[img[point_i][point_j][0]]+=1 fre=num/np.sum(num) T=np.zeros((256)) T[0]=fre[0]*256 for k inrange(256): if k: T[k]=T[k-1]+fre[k]*255 for point_i inrange(i*7,i*7+7): for point_j inrange(j*7,j*7+7): if point_i<shape[0] and point_j<shape[1]: result[point_i][point_j]=T[img[point_i][point_j][0]] result=result.astype(np.uint8) cv.imwrite("trans_"+imname,result)