def ENVI_raster_binary_from_2d_array(envidata, file_out, post, image_array): ''' Converts a numpy array back to an ENVI binary - requires geotransform and projection information as imported using ENVI_raster_binary_to_2d_array() or load_ENVI(). If resampling has taken place between the initial ENVI load (for which getransform and projection info is specific) then the new posting size must also be passed in to enable rescaling of pixels accordingly ''' util.check_output_dir(file_out) original_geotransform, inDs = envidata rows, cols = image_array.shape bands = 1 # Creates a new raster data source outDs = driver.Create(file_out, cols, rows, bands, gdal.GDT_Float32) # Write metadata originX = original_geotransform[0] originY = original_geotransform[3] outDs.SetGeoTransform([originX, post, 0.0, originY, 0.0, -post]) outDs.SetProjection(inDs.GetProjection()) #Write raster datasets outBand = outDs.GetRasterBand(1) outBand.WriteArray(image_array) new_geotransform = outDs.GetGeoTransform() new_projection = outDs.GetProjection() print("Output binary saved: %s" % file_out) return new_geotransform, new_projection, file_out
def ENVI_raster_binary_from_2d_array(envidata, file_out, post, image_array): ''' Converts a numpy array back to an ENVI binary - requires geotransform and projection information as imported using ENVI_raster_binary_to_2d_array() or load_ENVI(). If resampling has taken place between the initial ENVI load (for which getransform and projection info is specific) then the new posting size must also be passed in to enable rescaling of pixels accordingly ''' util.check_output_dir(file_out) original_geotransform, inDs = envidata rows, cols = image_array.shape bands = 1 # Creates a new raster data source outDs = driver.Create(file_out, cols, rows, bands, gdal.GDT_Float32) # Write metadata originX = original_geotransform[0] originY = original_geotransform[3] outDs.SetGeoTransform([originX, post, 0.0, originY, 0.0, -post]) outDs.SetProjection(inDs.GetProjection()) #Write raster datasets outBand = outDs.GetRasterBand(1) outBand.WriteArray(image_array) new_geotransform = outDs.GetGeoTransform() new_projection = outDs.GetProjection() print("Output binary saved: %s" %file_out) return new_geotransform,new_projection,file_out
def write_stats(filename, spacings, orientations, wavelength, snrs, wave_type, noise_type, noise_scale): util.check_output_dir(filename) if os.path.exists(filename): f = open(filename, 'a') else: f = open(filename, 'w') #f.write("Run, RMS spacing approx vs obs, spacing mean, RMS orientations, SnR mean\n") f.write("wave_type, noise_type, noise_scale, wavelength, RMS spacing approx. vs obs, spacing mean, RMS orientations, SnR mean\n") f.write("%s, %s, %s, %s, %f, %f, %f, %f\n" %(wave_type, noise_type, noise_scale, wavelength, rms(spacings - wavelength), spacings.mean(), rms(orientations), snrs.mean() )) f.close() print("Stats file: %s" %(filename))
def write_stats(filename, data, date, kernel_size, stepsize): util.check_output_dir(filename) if os.path.exists(filename): f = open(filename, 'a') else: f = open(filename, 'w') f.write("Run, min, max, mean, median, std.dev., UQ, LQ, IQR\n") f.write("%s_win_%s_step_%s, %f, %f, %f, %f, %f, %f, %f, %f\n" % (date, kernel_size, stepsize, np.amin(data), np.amax(data), np.mean(data), np.median(data), np.std(data), np.percentile(data, 75), np.percentile(data, 25), (np.percentile(data, 75) - np.percentile(data, 25)))) f.close()
def plot_image(img, title, filename, envidata, post, remove_empty_cols=0): ''' Plots a given image as a png and also converts the input to a binary using raster_functions.ENVI_raster_binary_from_2d_array() - this requires geotransform info to be available (see raster_functions.load_envi()). ENVI output is hardwired. ''' util.check_output_dir(filename) #print("Inside plot image...") #print(img.shape) if remove_empty_cols == 1: img = util.trim_constant_rows_cols(img) #fig, ax = plt.subplots() #cax = ax.imshow(img, cmap=cm.coolwarm, interpolation='none') #ax.set_xticklabels(" ") #ax.set_yticklabels(" ") #ax.set_title(title) #cbar=fig.colorbar(cax) plt.imshow(img, cmap=cm.coolwarm) plt.title(title) plt.colorbar() plt.savefig(filename + '.png', dpi=300, transparent=True) plt.clf() ## put into new function.... <<<<<<<<<<<<<<<<<<<<<<<<<<< if envidata != False: print("Geospatial data provided :)") raster_functions.ENVI_raster_binary_from_2d_array(envidata, filename + '.bin', post, img) # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< PROBLEM WITH THIS CATCH STILL - ARRAYS DEVELOPING ZERO AXIS.... else: print("No geospatial data provided - saving as txt file.") ofile="%s.png" %filename save_array_as_image(img, ofile)
def find_spacings(image_array, date, kernel_size, stepsize, envidata, post, output_dir, spectrum_n=1.0, interact=False): ''' Main workhorse of the whole FT program - for a provided input image a moving window - of a prior specified size - is iterated across an FT array of the original image. This returns new arrays representing maximum peak spacing, orientation and related signal-to-noise ratio (standard deviation of a given point over the mean). These arrays are then plotted and converted to binary files with headers matching that of the original input file - the pixels are rescaled to account for the step frequency of the moving window. Requires access to: find_spacing_at_point() more_fft_functions.border_indent() plots.plot_image() util.trim_constant_rows_cols ''' startTime = time.time() nywind,nxwind = image_array.shape opath = output_dir + '/%s_win_%s_step_%s' % (date, kernel_size, stepsize) ########################### ## SET MOVING WINDOW UP ## ########################### if kernel_size % 2 == 0 or kernel_size < 3: error_msg = "kernel is even - it needs to be odd and at least of a value of 3" sys.exit(error_msg) space = np.zeros((nywind // stepsize, nxwind // stepsize)) orientation = np.zeros((nywind // stepsize, nxwind // stepsize)) SnR_imgout = np.zeros((nywind // stepsize, nxwind // stepsize)) orig = np.zeros((nywind // stepsize, nxwind // stepsize)) window_border_indent, window_border_indent_end_X, window_border_indent_end_Y = more_fft_functions.border_indent(kernel_size, nxwind, nywind) time_stamp = time.strftime("%H.%M.%S") file_name = "%s/FT_POST_log_%s_%s.txt" %(opath, date, time_stamp) util.check_output_dir(file_name) with open( file_name, 'w') as f: f.write("Post: %f\n" %post) f.write("Image dimensions (x,y): %f, %f\n" %(nywind,nxwind)) f.write("Noise value: %f\n" %(spectrum_n)) f.write("Kernel size: %f\n" %(kernel_size)) f.write("Stepsize: %f\n" %(stepsize)) f.write("%s, %s, %s\n" %("Feature orientation", "distance (frq)", "distance (m)")) for out_i, ii in enumerate(range(window_border_indent, window_border_indent_end_Y, stepsize)): for out_j, jj in enumerate(range(window_border_indent, window_border_indent_end_X, stepsize)): angle, dist, snr = find_spacing_at_point(image_array, ii, jj, kernel_size, spectrum_n, show_fft=0) space[out_i, out_j] = dist * post orientation[out_i, out_j] = angle SnR_imgout[out_i, out_j] = snr orig[out_i, out_j] = image_array[ii, jj] print("PRINTING IMAGES....") img_prefix = opath + '/%s_winsize_%i_stepsize_%i_nvalue_%f_%s' % (date, kernel_size, stepsize, spectrum_n, time_stamp) new_post = post * stepsize # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< PROBLEM inside plot function if envidata==False such as using sine_wave_TEST.py (see plots.py) plots.plot_image(space, 'Spacing (m)', img_prefix + '_spacing_m', envidata, new_post, remove_empty_cols=1) plots.plot_image(orientation, 'Orientation (degN.)', img_prefix + '_orientation', envidata, new_post, remove_empty_cols=1) plots.plot_image(SnR_imgout, 'Signal-to-noise (%s)' % date, img_prefix + '_snr', envidata, new_post, remove_empty_cols=1) plots.plot_image(orig, 'Original input image (%s)' % date, img_prefix + '_original', envidata, new_post, remove_empty_cols=1) spacing_no_zeros = util.trim_constant_rows_cols(space) orientation_no_zeros = util.trim_constant_rows_cols(orientation) FT_spacing_orientation_stats.write_stats(output_dir + '/spacing_stats.txt', spacing_no_zeros, date, kernel_size, stepsize) FT_spacing_orientation_stats.write_stats(output_dir + '/orientation_stats.txt', orientation_no_zeros, date, kernel_size, stepsize) ## CREATE QUIVER PLOT plot_title = "%s_winsize_%i_stepsize_%i" %(date, kernel_size, stepsize) quiver_plotter.quiver_plot(orientation, space, opath, plot_title, img_prefix + '_quiver', interact=interact) endTime = time.time() totalTime = endTime - startTime print("Run took %f seconds to run" %(totalTime))
#print "===========================" #print "Kernel: ", kernel #print "Spacing (pixels): ", max_dist_px #print "Spacing (m): ", max_dist_m kernel_size_LIST.append(kernel) feature_spacing_m_LIST.append(max_dist_m) snr_LIST.append(snr) ## write values out to array and/or text file kernel = 2 * int(kernel * 1.2 / 2) + 1 ## what does this bit do?? # Write file out fout = '%s/kernel_estimation_out/kernel_spacing_%s_smooth_factor_%0.1f.csv' % ( opath, aoi, spectrum_n) util.check_output_dir(fout) f = open(fout, 'w') f.write("Kernel size (m), Feature spacing (m), SnR\n") for i in range(len(kernel_size_LIST)): f.write("%f, %f, %f\n" % (kernel_size_LIST[i], feature_spacing_m_LIST[i], snr_LIST[i])) f.close() ## Plot data ofigOut = '%s/kernel_estimation_out/kernel_size_to_spacing_%s_smooth_factor_%0.1f.png' % ( opath, aoi, spectrum_n) util.check_output_dir(fout) fig = plt.figure()
#print "===========================" #print "Kernel: ", kernel #print "Spacing (pixels): ", max_dist_px #print "Spacing (m): ", max_dist_m kernel_size_LIST.append(kernel) feature_spacing_m_LIST.append(max_dist_m) snr_LIST.append(snr) ## write values out to array and/or text file kernel = 2 * int(kernel * 1.2 / 2) + 1 ## what does this bit do?? # Write file out fout='%s/kernel_estimation_out/kernel_spacing_%s_smooth_factor_%0.1f.csv' %(opath, aoi, spectrum_n) util.check_output_dir(fout) f=open(fout, 'w') f.write("Kernel size (m), Feature spacing (m), SnR\n") for i in range(len(kernel_size_LIST)): f.write("%f, %f, %f\n" %(kernel_size_LIST[i], feature_spacing_m_LIST[i], snr_LIST[i])) f.close() ## Plot data ofigOut='%s/kernel_estimation_out/kernel_size_to_spacing_%s_smooth_factor_%0.1f.png' %(opath, aoi, spectrum_n) util.check_output_dir(fout) fig=plt.figure() ax1=fig.add_subplot(111)
def find_spacings(image_array, date, kernel_size, stepsize, envidata, post, output_dir, spectrum_n=1.0, interact=False): ''' Main workhorse of the whole FT program - for a provided input image a moving window - of a prior specified size - is iterated across an FT array of the original image. This returns new arrays representing maximum peak spacing, orientation and related signal-to-noise ratio (standard deviation of a given point over the mean). These arrays are then plotted and converted to binary files with headers matching that of the original input file - the pixels are rescaled to account for the step frequency of the moving window. Requires access to: find_spacing_at_point() more_fft_functions.border_indent() plots.plot_image() util.trim_constant_rows_cols ''' startTime = time.time() nywind, nxwind = image_array.shape opath = output_dir + '/%s_win_%s_step_%s' % (date, kernel_size, stepsize) ########################### ## SET MOVING WINDOW UP ## ########################### if kernel_size % 2 == 0 or kernel_size < 3: error_msg = "kernel is even - it needs to be odd and at least of a value of 3" sys.exit(error_msg) space = np.zeros((nywind // stepsize, nxwind // stepsize)) orientation = np.zeros((nywind // stepsize, nxwind // stepsize)) SnR_imgout = np.zeros((nywind // stepsize, nxwind // stepsize)) orig = np.zeros((nywind // stepsize, nxwind // stepsize)) window_border_indent, window_border_indent_end_X, window_border_indent_end_Y = more_fft_functions.border_indent( kernel_size, nxwind, nywind) time_stamp = time.strftime("%H.%M.%S") file_name = "%s/FT_POST_log_%s_%s.txt" % (opath, date, time_stamp) util.check_output_dir(file_name) with open(file_name, 'w') as f: f.write("Post: %f\n" % post) f.write("Image dimensions (x,y): %f, %f\n" % (nywind, nxwind)) f.write("Noise value: %f\n" % (spectrum_n)) f.write("Kernel size: %f\n" % (kernel_size)) f.write("Stepsize: %f\n" % (stepsize)) f.write("%s, %s, %s\n" % ("Feature orientation", "distance (frq)", "distance (m)")) for out_i, ii in enumerate( range(window_border_indent, window_border_indent_end_Y, stepsize)): for out_j, jj in enumerate( range(window_border_indent, window_border_indent_end_X, stepsize)): angle, dist, snr = find_spacing_at_point(image_array, ii, jj, kernel_size, spectrum_n, show_fft=0) space[out_i, out_j] = dist * post orientation[out_i, out_j] = angle SnR_imgout[out_i, out_j] = snr orig[out_i, out_j] = image_array[ii, jj] print("PRINTING IMAGES....") img_prefix = opath + '/%s_winsize_%i_stepsize_%i_nvalue_%f_%s' % ( date, kernel_size, stepsize, spectrum_n, time_stamp) new_post = post * stepsize # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< PROBLEM inside plot function if envidata==False such as using sine_wave_TEST.py (see plots.py) plots.plot_image(space, 'Spacing (m)', img_prefix + '_spacing_m', envidata, new_post, remove_empty_cols=1) plots.plot_image(orientation, 'Orientation (degN.)', img_prefix + '_orientation', envidata, new_post, remove_empty_cols=1) plots.plot_image(SnR_imgout, 'Signal-to-noise (%s)' % date, img_prefix + '_snr', envidata, new_post, remove_empty_cols=1) plots.plot_image(orig, 'Original input image (%s)' % date, img_prefix + '_original', envidata, new_post, remove_empty_cols=1) spacing_no_zeros = util.trim_constant_rows_cols(space) orientation_no_zeros = util.trim_constant_rows_cols(orientation) FT_spacing_orientation_stats.write_stats(output_dir + '/spacing_stats.txt', spacing_no_zeros, date, kernel_size, stepsize) FT_spacing_orientation_stats.write_stats( output_dir + '/orientation_stats.txt', orientation_no_zeros, date, kernel_size, stepsize) ## CREATE QUIVER PLOT plot_title = "%s_winsize_%i_stepsize_%i" % (date, kernel_size, stepsize) quiver_plotter.quiver_plot(orientation, space, opath, plot_title, img_prefix + '_quiver', interact=interact) endTime = time.time() totalTime = endTime - startTime print("Run took %f seconds to run" % (totalTime))
def save_array_as_image(arr, ofile): "Saves a numpy array as an image file - type depends on extension set by user" util.check_output_dir(ofile) scipy.misc.imsave(ofile, arr)