1 """!@brief Manage data (opening/saving raster, get ROI...)"""
8 """!@brief The function open and load the image given its name.
9 The function open and load the image given its name.
10 The type of the data is checked from the file and the scipy array is initialized accordingly.
12 filename: the name of the file
14 data : the opened data with gdal.Open() method
15 im : empty table with right dimension (array)
18 data = gdal.Open(filename,gdal.GA_ReadOnly)
20 print 'Impossible to open '+filename
27 gdal_dt = data.GetRasterBand(1).DataType
28 if gdal_dt == gdal.GDT_Byte:
30 elif gdal_dt == gdal.GDT_Int16:
32 elif gdal_dt == gdal.GDT_UInt16:
34 elif gdal_dt == gdal.GDT_Int32:
36 elif gdal_dt == gdal.GDT_UInt32:
38 elif gdal_dt == gdal.GDT_Float32:
40 elif gdal_dt == gdal.GDT_Float64:
42 elif gdal_dt == gdal.GDT_CInt16
or gdal_dt == gdal.GDT_CInt32
or gdal_dt == gdal.GDT_CFloat32
or gdal_dt == gdal.GDT_CFloat64 :
45 print 'Data type unkown'
49 im = sp.empty((nl,nc),dtype=dt)
53 Old function that open all the bands
65 '''!@brief Write an empty image on the hard drive.
68 outname: the name of the file to be written
70 GeoTransform: the geotransform information
71 Projection: the projection information
78 driver = gdal.GetDriverByName(
'GTiff')
81 if dt ==
'bool' or dt ==
'uint8':
83 elif dt ==
'int8' or dt ==
'int16':
84 gdal_dt=gdal.GDT_Int16
86 gdal_dt=gdal.GDT_UInt16
88 gdal_dt=gdal.GDT_Int32
90 gdal_dt=gdal.GDT_UInt32
91 elif dt ==
'int64' or dt ==
'uint64' or dt ==
'float16' or dt ==
'float32':
92 gdal_dt=gdal.GDT_Float32
94 gdal_dt=gdal.GDT_Float64
95 elif dt ==
'complex64':
96 gdal_dt=gdal.GDT_CFloat64
98 print 'Data type non-suported'
101 dst_ds = driver.Create(outname,nc,nl, d, gdal_dt)
102 dst_ds.SetGeoTransform(GeoTransform)
103 dst_ds.SetProjection(Projection)
108 Old function that cannot manage to write on each band outside the script
122 '''!@brief Get the set of pixels given the thematic map.
123 Get the set of pixels given the thematic map. Both map should be of same size. Data is read per block.
125 raster_name: the name of the raster file, could be any file that GDAL can open
126 roi_name: the name of the thematic image: each pixel whose values is greater than 0 is returned
128 X: the sample matrix. A nXd matrix, where n is the number of referenced pixels and d is the number of variables. Each
129 line of the matrix is a pixel.
130 Y: the label of the pixel
131 Written by Mathieu Fauvel.
135 raster = gdal.Open(raster_name,gdal.GA_ReadOnly)
137 print 'Impossible to open '+raster_name
141 roi = gdal.Open(roi_name,gdal.GA_ReadOnly)
143 print 'Impossible to open '+roi_name
147 if (raster.RasterXSize != roi.RasterXSize)
or (raster.RasterYSize != roi.RasterYSize):
148 print 'Images should be of the same size'
152 band = raster.GetRasterBand(1)
153 block_sizes = band.GetBlockSize()
154 x_block_size = block_sizes[0]
155 y_block_size = block_sizes[1]
159 d = raster.RasterCount
160 nc = raster.RasterXSize
161 nl = raster.RasterYSize
164 X = sp.array([]).reshape(0,d)
165 Y = sp.array([]).reshape(0,1)
166 for i
in range(0,nl,y_block_size):
167 if i + y_block_size < nl:
171 for j
in range(0,nc,x_block_size):
172 if j + x_block_size < nc:
179 ROI = roi.GetRasterBand(1).ReadAsArray(j, i, cols, lines)
184 Y = sp.concatenate((Y,ROI[t].reshape((t[0].shape[0],1)).astype(
'uint8')))
186 Xtp = sp.empty((t[0].shape[0],d))
188 band = raster.GetRasterBand(k+1).ReadAsArray(j, i, cols, lines)
191 X = sp.concatenate((X,Xtp))
193 print 'Impossible to allocate memory: ROI too big'
204 """!@brief Classify the whole raster image, using per block image analysis
205 The classifier is given in classifier and options in kwargs.
214 Nothing but raster written on disk
215 Written by Mathieu Fauvel.
221 raster = gdal.Open(raster_name,gdal.GA_ReadOnly)
223 print 'Impossible to open '+raster_name
227 if mask_name
is None:
230 mask = gdal.Open(mask_name,gdal.GA_ReadOnly)
232 print 'Impossible to open '+mask_name
235 if (raster.RasterXSize != mask.RasterXSize)
or (raster.RasterYSize != mask.RasterYSize):
236 print 'Image and mask should be of the same size'
240 d = raster.RasterCount
241 nc = raster.RasterXSize
242 nl = raster.RasterYSize
245 GeoTransform = raster.GetGeoTransform()
246 Projection = raster.GetProjection()
249 x_block_size = block_sizes
250 y_block_size = block_sizes
253 driver = gdal.GetDriverByName(
'GTiff')
254 dst_ds = driver.Create(classif_name, nc,nl, 1, gdal.GDT_UInt16)
255 dst_ds.SetGeoTransform(GeoTransform)
256 dst_ds.SetProjection(Projection)
257 out = dst_ds.GetRasterBand(1)
260 if classifier[
'name']
is 'NPFS':
262 model = classifier[
'model']
263 ids = classifier[
'ids']
265 elif classifier[
'name']
is 'GMM':
266 model = classifier[
'model']
269 for i
in range(0,nl,y_block_size):
270 if i + y_block_size < nl:
274 for j
in range(0,nc,x_block_size):
275 if j + x_block_size < nc:
281 if classifier[
'name']
is 'NPFS':
283 X = sp.empty((cols*lines,nv))
284 for ind,v
in enumerate(ids):
285 X[:,ind] = raster.GetRasterBand(int(v+1)).ReadAsArray(j, i, cols, lines).reshape(cols*lines)
289 yp = model.predict_gmm(X)[0].astype(
'uint16')
291 mask_temp=mask.GetRasterBand(1).ReadAsArray(j, i, cols, lines).reshape(cols*lines)
292 t = sp.where(mask_temp!=0)[0]
293 yp=sp.zeros((cols*lines,))
294 yp[t]= model.predict_gmm(X[t,:])[0].astype(
'uint16')
296 elif classifier[
'name']
is 'GMM':
298 X = sp.empty((cols*lines,d))
299 for ind
in xrange(d):
300 X[:,ind] = raster.GetRasterBand(int(ind+1)).ReadAsArray(j, i, cols, lines).reshape(cols*lines)
304 yp = model.predict_gmm(X)[0].astype(
'uint16')
306 mask_temp=mask.GetRasterBand(1).ReadAsArray(j, i, cols, lines).reshape(cols*lines)
307 t = sp.where(mask_temp!=0)[0]
308 yp=sp.zeros((cols*lines,))
309 yp[t]= model.predict_gmm(X[t,:])[0].astype(
'uint16')
312 out.WriteArray(yp.reshape(lines,cols),j,i)
321 """!@brief Apply a smoothing filter on all the pixels of the input image
324 raster_name: the name of the originale SITS
325 mask_name: the name of the mask. In that file, every pixel with value greater than 0 is masked.
326 output_name: the name of the smoothed image
329 - check the input file format (uint16 or float)
332 Written by Mathieu Fauvel
335 import smoother
as sm
337 raster = gdal.Open(raster_name,gdal.GA_ReadOnly)
339 print 'Impossible to open '+raster_name
343 mask = gdal.Open(mask_name,gdal.GA_ReadOnly)
345 print 'Impossible to open '+mask_name
349 if (raster.RasterXSize != mask.RasterXSize)
or (raster.RasterYSize != mask.RasterYSize)
or (raster.RasterCount != mask.RasterCount):
350 print 'Image and mask should be of the same size'
354 d = raster.RasterCount
355 nc = raster.RasterXSize
356 nl = raster.RasterYSize
359 GeoTransform = raster.GetGeoTransform()
360 Projection = raster.GetProjection()
363 band = raster.GetRasterBand(1)
364 block_sizes = band.GetBlockSize()
365 x_block_size = block_sizes[0]
366 y_block_size = block_sizes[1]
370 driver = gdal.GetDriverByName(
'GTiff')
371 dst_ds = driver.Create(output_name, nc,nl, d, gdal.GDT_Float64)
372 dst_ds.SetGeoTransform(GeoTransform)
373 dst_ds.SetProjection(Projection)
375 for i
in xrange(0,nl,y_block_size):
376 if i + y_block_size < nl:
380 for j
in xrange(0,nc,x_block_size):
381 if j + x_block_size < nc:
387 X = sp.empty((cols*lines,d))
388 M = sp.empty((cols*lines,d),dtype=
'int')
389 for ind
in xrange(d):
390 X[:,ind] = raster.GetRasterBand(int(ind+1)).ReadAsArray(j, i, cols, lines).reshape(cols*lines)
391 M[:,ind] = mask.GetRasterBand(int(ind+1)).ReadAsArray(j, i, cols, lines).reshape(cols*lines)
396 Xf = sp.empty((cols*lines,d))
397 for ind
in xrange(cols*lines):
398 smoother = sm.Whittaker(x=X[ind,:],t=t,w=1-M[ind,:],order=2)
399 Xf[ind,:] = smoother.smooth(l)
402 for ind
in xrange(d):
403 out = dst_ds.GetRasterBand(ind+1)
404 out.WriteArray(Xf[:,ind].reshape(lines,cols),j,i)
def predict_image
Classify the whole raster image, using per block image analysis The classifier is given in classifier...
def create_empty_tiff
Write an empty image on the hard drive.
def smooth_image
Apply a smoothing filter on all the pixels of the input image.
def open_data_band
The function open and load the image given its name.
def get_samples_from_roi
Get the set of pixels given the thematic map.