-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageHelper.py
58 lines (53 loc) · 2.35 KB
/
ImageHelper.py
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
import os
from osgeo import gdal
import numpy
import Util
def makeFileName(prefix, suffix, ext = "tif"):
"""
Generate a new file name by appending a suffix and changing the extension of an input file name
:param prefix: string, input file name
:param suffix: string, suffix to be placed just before file extension (e.g., 'NDVI')
:param ext: string, extension. Don't put the period before the extension
"""
return prefix + '_' + suffix + '.' + ext
# Return a ndarray for every pixel in the GeoTIFF image
# e.g.: extractBand('some_dir', 'Blue_2005_DBSH.tif')
# Return format: a[x][y][i]: band i, position (x, y)
def extractBand(dir, filename):
file = os.path.join(dir, filename)
dataset = gdal.Open(file, gdal.GA_ReadOnly)
nobands = dataset.RasterCount
firstBand = dataset.GetRasterBand(1)
firstArray = firstBand.ReadAsArray()
result = numpy.empty((firstArray.shape[0], firstArray.shape[1], nobands))
for j in range(0, firstArray.shape[0]):
for k in range(0, firstArray.shape[1]):
result[j, k, 0] = firstArray[j, k]
for i in range(2, nobands + 1):
band = dataset.GetRasterBand(i)
array = band.ReadAsArray()
for j in range(0, array.shape[0]):
for k in range(0, array.shape[1]):
result[j, k, i - 1] = array[j, k]
return result
def extractBandForYear(dir, _year):
year = ''
if not isinstance(_year, basestring):
year = str(_year)
else:
year = str(_year)
ndvi = Util.normalize(extractBand(dir, makeFileName('NDVI', year + '_DBSH', 'tif')), 'NDVI')
evi = Util.normalize(extractBand(dir, makeFileName('EVI', year + '_DBSH', 'tif')), 'EVI')
lswi = Util.normalize(extractBand(dir, makeFileName('LSWI', year + '_DBSH', 'tif')), 'LSWI')
result = numpy.empty((ndvi.shape[0], ndvi.shape[1], ndvi.shape[2] + evi.shape[2] + lswi.shape[2]))
for i in range(0, result.shape[0]):
for j in range(0, result.shape[1]):
for k in range(0, ndvi.shape[2]):
result[i, j, k] = ndvi[i, j, k]
for k in range(0, evi.shape[2]):
result[i, j, ndvi.shape[2] + k] = evi[i, j, k]
for k in range(0, lswi.shape[2]):
result[i, j, ndvi.shape[2] + evi.shape[2] + k] = lswi[i, j, k]
return result
# temp = extractBand('F:/DaiHoc/2016-2017 ki 1/Advanced Topics in Computer Science/Images', 'NDVI_2015_DBSH.tif')
# temp = extractBand('F:/DaiHoc/2016-2017 ki 1/Advanced Topics in Computer Science/Images', 'EVI_2015_DBSH.tif')