Commit 3f078248 authored by luroth's avatar luroth
Browse files

.

parent 198053a8
'''
Created on 15.03.2017
@author: luroth
'''
#######################################
import math as math
import numpy as np
import geojson
from osgeo import ogr, gdal
from joblib import Parallel, delayed
import multiprocessing
from multiprocessing import Process, Queue, Manager
from itertools import repeat
import csv
import os
import sys
import getopt
import matplotlib.pyplot as plt
from matplotlib import path
from osgeo import gdalnumeric
import time
from progressbar import ProgressBar
from Common import *
def mask_worker(work_queue, result_queue, metadata_folder, output_directory, sub_plots):
for job in iter(work_queue.get, 'STOP'):
PhotoID = job['PhotoID']
create_mask(metadata_folder, output_directory, sub_plots, PhotoID)
result_queue.put(PhotoID)
def create_mask(metadata_folder, output_directory, sub_plots, PhotoID):
photo_meta_x_ds = gdal.Open(os.path.join(metadata_folder, PhotoID + '_metadata_coord_x.tif'))
photo_meta_x = np.array(photo_meta_x_ds.GetRasterBand(1).ReadAsArray(), dtype=np.float64)
photo_meta_x += DELTA_x
min_x = np.min(photo_meta_x.flat)
max_x = np.max(photo_meta_x.flat)
photo_meta_y_ds = gdal.Open(os.path.join(metadata_folder, PhotoID + '_metadata_coord_y.tif'))
photo_meta_y = np.array(photo_meta_y_ds.GetRasterBand(1).ReadAsArray(), dtype=np.float64)
photo_meta_y += DELTA_y
min_y = np.min(photo_meta_y.flat)
max_y = np.max(photo_meta_y.flat)
photo_meta_xy = np.stack((photo_meta_x, photo_meta_y), axis=2)
photo_meta_xy = photo_meta_xy.reshape(-1,2)
mask_subplots = np.empty(photo_meta_xy.shape[0:1], dtype=int)
found = False
for subplot in sub_plots:
coords = subplot['geometry']['coordinates'][0]
plot_id = subplot['properties']['plot_number']
subsample = subplot['properties']['subsample']
if(min_x < coords[0][0] < max_x and min_y < coords[0][1] < max_y):
path_subplot = path.Path(coords)
mask_subplot = path_subplot.contains_points(photo_meta_xy, radius=0.02)
if not (~mask_subplot).all():
found = True
mask_subplots = mask_subplots + mask_subplot*(plot_id*100 + subsample)
#print(".", flush=True, end="")
if found:
mask_subplots = mask_subplots.reshape(photo_meta_y.shape)
gdalnumeric.SaveArray(mask_subplots, os.path.join(output_directory, PhotoID + '_mask_subsamples.tif'), format='GTiff')
def main(argv):
camera_position_file = ''
metadata_folder = ''
output_directory = ''
try:
opts, args = getopt.getopt(argv,"hc:m:p:o:z:")
except getopt.GetoptError:
print('test.py -c <camera position file> -m <metadata folder> -p <lot> -s <training file soil> -o <Output directory>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('test.py -c <camera position file> -m <metadata folder> -p <lot> -s <training file soil> -o <Output directory>')
sys.exit()
elif opt == "-c":
camera_position_file = arg
elif opt == "-m":
metadata_folder = arg
elif opt == "-p":
lot_dim = arg
elif opt == "-o":
output_directory = arg
elif opt == "-z":
sub_plots_file = arg
print('Metdadata folder is', metadata_folder)
with open(sub_plots_file, 'r') as f:
sub_plots_tmp = geojson.load(f)
sub_plots = sub_plots_tmp['features']
with open(camera_position_file, 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
next(spamreader, None)
next(spamreader, None)
m = Manager()
jobs = m.Queue()
results = m.Queue()
processes = []
max_jobs = 0
count = 0
for row in spamreader:
PhotoID = os.path.splitext(row[0])[0]
if os.path.isfile(os.path.join(metadata_folder, PhotoID + '_metadata_coord_x.tif')):
job = dict()
job['PhotoID'] = PhotoID
jobs.put(job)
max_jobs += 1
progress = ProgressBar(min_value=0, max_value=max_jobs)
for w in range(multiprocessing.cpu_count()-2):
p = Process(target=mask_worker, args=(jobs, results, metadata_folder, output_directory, sub_plots))
p.daemon = True
p.start()
processes.append(p)
jobs.put('STOP')
print("jobs all started")
progress.update(0)
while count < (max_jobs):
results.get()
progress.update(count)
count+=1
progress.finish()
for p in processes:
p.join()
if __name__ == "__main__":
main(sys.argv[1:])
print("\n============\nAll masks prepared, END\n")
......@@ -62,16 +62,16 @@ def create_mask(metadata_folder, output_directory, lot, soils, PhotoID):
output_mask = np.array(1*mask_soils, dtype=int)
gdalnumeric.SaveArray(output_mask, os.path.join(output_directory, PhotoID + '_mask_soil.tif'), format='GTiff')
def main(argv):
camera_position_file = ''
metadata_folder = ''
training_plant = ''
training_soil = ''
output_directory = ''
try:
opts, args = getopt.getopt(argv,"hc:m:p:s:o:")
opts, args = getopt.getopt(argv,"hc:m:p:s:o:z:")
except getopt.GetoptError:
print('test.py -c <camera position file> -m <metadata folder> -p <lot> -s <training file soil> -o <Output directory>')
sys.exit(2)
......@@ -88,7 +88,7 @@ def main(argv):
elif opt == "-s":
training_soil = arg
elif opt == "-o":
output_directory = arg
output_directory = arg
print('Metdadata folder is', metadata_folder)
......@@ -100,6 +100,7 @@ def main(argv):
with open(lot_dim, 'r') as f:
lot_polygons = geojson.load(f)
lot = lot_polygons['features'][0]['geometry']['coordinates'][0]
soils = []
with open(training_soil, 'r') as f:
......
......@@ -12,32 +12,89 @@ import numpy as np
from scipy.cluster.vq import kmeans,vq
from scipy import misc
import sklearn.cluster
import matplotlib.pyplot as plt
from osgeo import gdalnumeric
import os
import sys
import getopt
import csv
from scipy import ndimage
image = misc.imread(r'E:\UAV\_Workspace_\2016_ethz_eschikon_FIP_50m_20160607\RGB\DSC02219.JPG')
image_mask_all = misc.imread(r'E:\UAV\_Workspace_\2016_ethz_eschikon_FIP_50m_20160607\output\meta\DSC02219_mask_lot.tif')
image_mask_soil = misc.imread(r'E:\UAV\_Workspace_\2016_ethz_eschikon_FIP_50m_20160607\output\meta\DSC02219_mask_soil.tif')
def main(argv):
camera_position_file = ''
metadata_folder = ''
output_directory = ''
try:
opts, args = getopt.getopt(argv,"hc:m:p:s:o:")
except getopt.GetoptError:
print('test.py -c <camera position file> -m <metadata folder> -o <Output directory>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('test.py -c <camera position file> -m <metadata folder> -o <Output directory>')
sys.exit()
elif opt == "-c":
camera_position_file = arg
elif opt == "-m":
metadata_folder = arg
elif opt == "-o":
output_directory = arg
print('Metdadata folder is', metadata_folder)
origin_shape = image.shape
image = image.reshape(-1,3)
image_mask_all = image_mask_all.reshape(-1)
image_mask_soil = image_mask_soil.reshape(-1)
with open(camera_position_file, 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
next(spamreader, None)
next(spamreader, None)
for row in spamreader:
PhotoID = os.path.splitext(row[0])[0]
image = misc.imread(os.path.join(metadata_folder, '../../RGB', PhotoID + '.JPG'))
if os.path.isfile(os.path.join(metadata_folder, PhotoID + '_mask_lot.tif')):
image_mask_all = misc.imread(os.path.join(metadata_folder, PhotoID + '_mask_lot.tif'))
image_mask_soil = misc.imread(os.path.join(metadata_folder, PhotoID + '_mask_soil.tif'))
origin_shape = image.shape
image = image.reshape(-1,3)
image_mask_all = image_mask_all.reshape(-1)
image_mask_soil = image_mask_soil.reshape(-1)
image[image_mask_all==0,:] = 0
# ExG
R = image[:,0]
G = image[:,1]
B = image[:,2]
r= np.divide(R, (np.mean(R)+np.mean(G)+np.mean(B)), dtype=float)
g= np.divide(G, (np.mean(R)+np.mean(G)+np.mean(B)), dtype=float)
b= np.divide(B, (np.mean(R)+np.mean(G)+np.mean(B)), dtype=float)
ExG = 2* g - r - b
misc.imsave(os.path.join(output_directory, PhotoID + '_ExG.tif'), ExG.reshape(origin_shape[0], origin_shape[1]))
ExG_soil = ExG[image_mask_soil==1]
print(ExG_soil.size)
if ExG_soil.size ==0:
continue
ExG_threshold = np.percentile(ExG_soil, 99.95)
soil = np.array(ExG > ExG_threshold)
gdalnumeric.SaveArray(1*(soil.reshape(origin_shape[0], origin_shape[1])), os.path.join(output_directory, PhotoID + '_segmented_ExG.tif'), format='GTiff')
#misc.imsave(os.path.join(output_directory, PhotoID + '_segmented_ExG.tif'), soil.reshape(origin_shape[0], origin_shape[1]))
print(PhotoID, "done")
image[image_mask_all==0,:] = 0
plt.imshow(image.reshape(origin_shape))
plt.show()
clf = sklearn.cluster.KMeans(n_clusters=6, n_jobs=-1)
clusters = clf.fit_predict(image)
clusters = clusters.reshape(origin_shape[0], origin_shape[1])
plt.imshow(clusters)
plt.show()
res_soil = ma.array(clusters, mask=image_mask_soil)
np.unique(res_soil)
import gdalnumeric
gdalnumeric.SaveArray(clusters, r'E:\UAV\_Workspace_\2016_ethz_eschikon_FIP_50m_general/test.tif', format='GTiff')
\ No newline at end of file
if __name__ == "__main__":
main(sys.argv[1:])
print("\n============\nAll images segmented, END\n")
'''
Created on 16.03.2017
@author: luroth
'''
from pylab import plot,show
from numpy import vstack,array
from numpy.random import rand
import numpy.ma as ma
import numpy as np
from scipy.cluster.vq import kmeans,vq
from scipy import misc
import sklearn.cluster
import matplotlib.pyplot as plt
from osgeo import gdalnumeric
import os
import sys
import getopt
import csv
from scipy import ndimage
import geojson
import json
from Common import *
from osgeo import ogr, gdal
import pandas as pd
def main(argv):
camera_position_file = ''
metadata_folder = ''
output_directory = ''
sub_plots_file = ''
try:
opts, args = getopt.getopt(argv,"hc:m:p:s:o:")
except getopt.GetoptError:
print('test.py -c <camera position file> -m <metadata folder> -o <Output directory>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('test.py -c <camera position file> -m <metadata folder> -o <Output directory>')
sys.exit()
elif opt == "-c":
camera_position_file = arg
elif opt == "-m":
metadata_folder = arg
elif opt == "-o":
output_directory = arg
elif opt == "-s":
sub_plots_file = arg
print('Metdadata folder is', metadata_folder)
all_cc = []
sub_plots_file = r'E:\UAV\_Workspace_\2016_ethz_eschikon_FIP_50m_general\design_subsamples.geojson'
with open(sub_plots_file, 'r') as f:
sub_plots_tmp = geojson.load(f)
sub_plots = sub_plots_tmp['features']
with open(camera_position_file, 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
next(spamreader, None)
next(spamreader, None)
for row in spamreader:
PhotoID = os.path.splitext(row[0])[0]
print("\n", PhotoID)
if (os.path.isfile(os.path.join(metadata_folder, PhotoID + '_mask_subsamples.tif')) and
os.path.isfile(os.path.join(metadata_folder, PhotoID + '_segmented_ExG.tif'))):
subplots_ds = gdal.Open(os.path.join(metadata_folder, PhotoID + '_mask_subsamples.tif'))
subplots = np.array(subplots_ds.GetRasterBand(1).ReadAsArray(), dtype=int)
subplots = subplots.reshape(-1)
photo_meta_x_ds = gdal.Open(os.path.join(metadata_folder, PhotoID + '_metadata_coord_x.tif'))
photo_meta_x = np.array(photo_meta_x_ds.GetRasterBand(1).ReadAsArray(), dtype=np.float64)
photo_meta_x += DELTA_x
photo_meta_y_ds = gdal.Open(os.path.join(metadata_folder, PhotoID + '_metadata_coord_y.tif'))
photo_meta_y = np.array(photo_meta_y_ds.GetRasterBand(1).ReadAsArray(), dtype=np.float64)
photo_meta_y += DELTA_y
photo_meta_xy = np.stack((photo_meta_x, photo_meta_y), axis=2)
photo_meta_xy = photo_meta_xy.reshape(-1,2)
photo_meta_x = photo_meta_x.reshape(-1)
photo_meta_y = photo_meta_y.reshape(-1)
segmented = misc.imread(os.path.join(metadata_folder, PhotoID + '_segmented_ExG.tif'))
segmented = segmented.reshape(-1)
photo_meta_z = misc.imread(os.path.join(metadata_folder, PhotoID + '_metadata_coord_z.tif'))
photo_meta_z = photo_meta_z.reshape(-1)
photo_meta_angle_z = misc.imread(os.path.join(metadata_folder, PhotoID + '_metadata_angle_z.tif'))
photo_meta_angle_z = photo_meta_angle_z.reshape(-1)
photo_meta_angle_xy = misc.imread(os.path.join(metadata_folder, PhotoID + '_metadata_angle_xy.tif'))
photo_meta_angle_xy = photo_meta_angle_xy.reshape(-1)
for sub_plot in sub_plots:
plot_id = sub_plot['properties']['plot_number']
subsample = sub_plot['properties']['subsample']
#print("calculating plot id", plot_id, ', subsample', subsample)
mask_subplot = subplots == (plot_id*100+subsample)
subplot_segmented = segmented[mask_subplot]
if subplot_segmented.size > 0:
subplot_mean_cc = np.mean(subplot_segmented)
subplot_mean_x = np.mean(photo_meta_x[mask_subplot])
subplot_mean_y = np.mean(photo_meta_y[mask_subplot])
subplot_mean_z = np.mean(photo_meta_z[mask_subplot])
subplot_mean_angle_z = np.mean(photo_meta_angle_z[mask_subplot])
subplot_mean_angle_xy = np.mean(photo_meta_angle_xy[mask_subplot])
print(subplot_mean_cc, end="", flush=True)
all_cc.append({"plot_id":plot_id, 'subsample':subsample, 'CC':subplot_mean_cc,
'glob_X':subplot_mean_x,
'glob_Y':subplot_mean_y,
'glob_Z':subplot_mean_z,
'angle_z':subplot_mean_angle_z,
'angle_xy':subplot_mean_angle_xy,
'image':PhotoID})
else:
print("-", end="", flush=True)
with open(os.path.join(metadata_folder, "CC.json"), 'w') as f:
json.dump(all_cc, f)
pd_data = pd.read_json(json.dump(all_cc), orient='records')
pd_data.to_csv(os.path.join(metadata_folder, "CC.vsc"))
if __name__ == "__main__":
main(sys.argv[1:])
print("\n============\nAll images segmented, END\n")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment