Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# get class numbers from names
class_list = image.metadata.get('class names')
class_nums = [
class_list.index(className) if className in class_list else -1 for
className in self.class_names]
# copy pixels of the desired classes
for y in range(n):
for x in range(m):
pixel = data[x, y]
if pixel[0] in class_nums:
classified[x, y] = 1 + class_nums.index(pixel[0])
# save the classified image to a file
spectral.io.envi.save_classification(classified_file_name, classified,
class_names=['No data'] +
self.class_names,
metadata={'data ignore value': 0,
'description': 'COAL '
'' + pycoal.version +
' mining ' +
'classified image.',
'map info':
image.metadata.get(
'map info')})
end = time.time()
seconds_elapsed = end - start
m, s = divmod(seconds_elapsed, 60)
h, m = divmod(m, 60)
logging.info(
# allocate a copy for reindexed pixels
copy = numpy.zeros(shape=(M, N), dtype=numpy.uint16)
# find classes actually present in the image
classes = sorted(set(classified.asarray().flatten().tolist()))
lookup = [classes.index(i) if i in classes else 0 for i in
range(int(classified.metadata.get('classes')))]
# reindex each pixel
for x in range(M):
for y in range(N):
copy[x, y] = lookup[data[x, y, 0]]
# overwrite the file
spectral.io.envi.save_classification(classified_file_name, copy,
force=True,
class_names=[
classified.metadata.get(
'class names')[i]
for i in classes],
metadata=classified.metadata)
Args:
source_filename (str): filename of the source image
destination_filename (str): filename of the destination image
"""
logging.info(
"Creating an empty copy of classified image '%s' with the same "
"size. Saving to '%s'", source_filename, destination_filename)
# open the source image
source = spectral.open_image(source_filename)
# create an empty array of the same dimensions
destination = numpy.zeros(shape=source.shape, dtype=numpy.uint16)
# save it with source metadata
spectral.io.envi.save_classification(destination_filename, destination,
class_names=['No data', 'Data'],
metadata={'data ignore value': 0,
'map info':
source.metadata.get(
'map info')})