|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Tue Oct 30 13:13:10 2018 |
| 4 | +
|
| 5 | +@author: 75129 |
| 6 | +""" |
| 7 | + |
| 8 | +import scipy.io as sio |
| 9 | +from scipy import misc |
| 10 | +import numpy as np |
| 11 | +import matplotlib as plt |
| 12 | +from numpy.matlib import repmat |
| 13 | +from sklearn.decomposition import PCA |
| 14 | +from scipy import stats |
| 15 | +import pandas as pd |
| 16 | + |
| 17 | +#import arcpy |
| 18 | +#totaldata读取 |
| 19 | +yinzi_name=['altitude','aspect','fault','ndvi','plan','profile','rainfall','river','road','slope','spi','sti','twi','soil_envi','landuse_envi','lithology_envi'] |
| 20 | +yinzi_type_ori=['float','float','float','float','float','float','float','float','float','float','float','float','float','str','str','str'] |
| 21 | +#load_fn = 'ysxp_lable.mat' |
| 22 | +#load_data = sio.loadmat(load_fn) |
| 23 | +#lable = load_data['lable'] |
| 24 | +#load_fn = 'img_yx.mat' |
| 25 | +#load_data = sio.loadmat(load_fn) |
| 26 | +#lable = load_data['lable'] #假设文件中存有字符变量是matrix,例如matlab中save(load_fn, 'matrix');当然可以保存多个save(load_fn, 'matrix_x', 'matrix_y', ...); |
| 27 | +lable = misc.imread('C:/Users/75129/Desktop/mypy/demo_all/SUyushan.tif') |
| 28 | +row=len(lable) |
| 29 | +col=len(lable[0]) |
| 30 | +nums_factors=len(yinzi_name) |
| 31 | +img=np.zeros((row,col,nums_factors)) |
| 32 | +i=0 |
| 33 | +yinzi_type=[] |
| 34 | +for i in range(nums_factors): |
| 35 | + name=yinzi_name[i] |
| 36 | + PATH='C:/Users/75129/Desktop/mypy/demo_all/tif_yushan/' |
| 37 | + img[:,:,i]=misc.imread(PATH+name+'.tif') |
| 38 | +# if yinzi_type_ori[i]=='str': |
| 39 | +# plt.pyplot.figure() |
| 40 | +# plt.pyplot.imshow(img[:,:,i]) |
| 41 | + ttt=img[:,:,i] |
| 42 | + ttt[ttt==-32768]=0 |
| 43 | +# ttt[ttt==-3.402823e+038]=0 |
| 44 | + img[:,:,i]=ttt |
| 45 | +# img_i=load_data[name] |
| 46 | +# ma=np.max(img_i) |
| 47 | +# mi=np.min(img_i) |
| 48 | +# img[:,:,i]=(img_i-mi)/(ma-mi) |
| 49 | + i=i+1 |
| 50 | +#pca = PCA(n_components=3) |
| 51 | +#img1=np.reshape(img,(2636*2206,16)) |
| 52 | +#img_pca=pca.fit_transform(img1) |
| 53 | +#img_pca=np.reshape(img_pca,(2636,2206,3)) |
| 54 | +#img=img_pca |
| 55 | +#plt.pyplot.imshow(img_pca[:,:,0]) |
| 56 | +xfanwei=[] |
| 57 | +yfanwei=[] |
| 58 | +print('开始裁剪 。') |
| 59 | +empty_index=[] |
| 60 | +max_id=np.max(lable[lable!=65536]) |
| 61 | +yinzi_1d=np.zeros([max_id+1,nums_factors]) |
| 62 | +for it in range(max_id,max_id+1):#4890 |
| 63 | + fanwei=(lable==it) |
| 64 | + fanwei=fanwei+np.zeros((row,col)) |
| 65 | + img_it=np.zeros((row,col,nums_factors)) |
| 66 | + for i in range(nums_factors): |
| 67 | + img_it[:,:,i]=fanwei*img[:,:,i] |
| 68 | + |
| 69 | + index_fanwei=np.argwhere(lable==it) |
| 70 | + if len(index_fanwei)!=0: |
| 71 | + x_min=min(index_fanwei[:,0]) |
| 72 | + x_max=max(index_fanwei[:,0]) |
| 73 | + y_min=min(index_fanwei[:,1]) |
| 74 | + y_max=max(index_fanwei[:,1]) |
| 75 | + img_it_resize=img_it[x_min:(x_max+1),y_min:(y_max+1),:] |
| 76 | +# for i in range(nums_factors): |
| 77 | +# plt.pyplot.figure() |
| 78 | +# plt.pyplot.imshow(img_it_resize[:,:,i]) |
| 79 | + filename='C:/Users/75129/Desktop/mypy/demo_all/yushan_all_OriCard/'+str(it) |
| 80 | + np.save(filename,img_it_resize) |
| 81 | + xfanwei.append(x_max-x_min+1) |
| 82 | + yfanwei.append(y_max-y_min+1) |
| 83 | + |
| 84 | + for f in range(nums_factors): |
| 85 | + x,y,z=img_it_resize.shape |
| 86 | + values=np.reshape(img_it_resize,[x*y,z]) |
| 87 | + values_f=values[:,f] |
| 88 | + values_f=values_f[values_f!=0] |
| 89 | + if yinzi_type_ori[f]=='str': |
| 90 | + if values_f.size!=0: |
| 91 | + yinzi_1d[it,f]=stats.mode(values_f)[0][0] |
| 92 | + else: |
| 93 | + yinzi_1d[it,f]=np.mean(values_f) |
| 94 | + print(str(it)+' is done'+' x:'+str(x_max-x_min+1)+' y:'+str(y_max-y_min+1)) |
| 95 | + print(yinzi_1d[it,15]) |
| 96 | + else: |
| 97 | + empty_index.append(it) |
| 98 | + filename='C:/Users/75129/Desktop/mypy/demo_all/yushan_all_OriCard/'+str(it) |
| 99 | + np.save(filename,np.zeros([1,1,nums_factors])) |
| 100 | + print(str(it)+' is done,but it is empty') |
| 101 | + |
| 102 | +df = pd.DataFrame(yinzi_1d,columns=yinzi_name) |
| 103 | +df.to_csv('C:/Users/75129/Desktop/mypy/demo_all/yushan_all_OriCard/yinzi1d.csv') |
| 104 | + |
| 105 | + |
| 106 | +# plt.pyplot.imshow(img_it_resize[:,:,0]) |
| 107 | +print('裁剪结束') |
| 108 | + |
| 109 | +#band=len(totaldata[0][0]) |
| 110 | +#totaldata=np.reshape(totaldata,(row*col,band)) |
| 111 | + |
| 112 | + |
| 113 | +#假如这是pro,显示最后的概率分布图 |
| 114 | +#pro=totaldata[:,4] |
| 115 | +#pro_map=np.reshape(pro,(row,col)) |
| 116 | +#plt.pyplot.imshow(pro_map,cmap='bone') |
| 117 | + |
| 118 | +#如果要输入到arcgis当中,就把pro_map保存为tif |
0 commit comments