def convert_rgb2gray(i_info, i_sect, j_sect, n_rows, n_cols, the_sensor, stats=False): """ Converts RGB to gray scale array Args: i_info (object of ropen) j_sect (int): Starting column index. i_sect (int): Starting row index. n_cols (int) n_rows (int) the_sensor (str): The satellite sensor. stats (Optional[bool]) Equation: 0.2125 R + 0.7154 G + 0.0721 B """ utils.sensor_wavelength_check(the_sensor, ['blue', 'green', 'red']) bands2open = [ utils.SENSOR_BAND_DICT[the_sensor]['blue'], utils.SENSOR_BAND_DICT[the_sensor]['green'], utils.SENSOR_BAND_DICT[the_sensor]['red'] ] if stats: logger.info('\nCalculating image min and max ...\n') min_max = get_layer_min_max(i_info, rgb=True) im_min = min_max[0][0] im_max = min_max[0][1] # im_min = 1000000 # im_max = -1000000 # # for i_ in range(0, i_info.rows, 512): # # n_rows_ = raster_tools.n_rows_cols(i_, 512, i_info.rows) # # for j_ in range(0, i_info.cols, 512): # # n_cols_ = raster_tools.n_rows_cols(j_, 512, i_info.cols) # # im_block = i_info.read(bands2open=[1, 2, 3], # i=i_, j=j_, # rows=n_rows_, cols=n_cols_, # d_type='float32') # # luminosity = get_luminosity(im_block, n_rows_, n_cols_, rgb) # # im_min, im_max = min_max_func(luminosity, im_min, im_max) # bp = raster_tools.BlockFunc(min_max_func, [i_info], None, None, # out_attributes=['im_min', 'im_max'], # print_statement='\nGetting image statistics ...\n', # write_array=False, # close_files=False, # be_quiet=False) # # bp.run() return None, im_min, im_max else: logger.info('\nCalculating average RGB ...\n') im_block = i_info.read(bands2open=bands2open, i=i_sect, j=j_sect, rows=n_rows, cols=n_cols, d_type='float32') luminosity = get_luminosity(im_block) return luminosity, None, None
def _section_read_write(section_counter): """ Handles the section reading and writing Args: section_counter (int) """ section_pair = potsi[section_counter - 1] # this_parameter_object_ = this_parameter_object.copy() this_parameter_object_ = copy.copy(param_dict) this_parameter_object_ = sputilities.dict2class(this_parameter_object_) # Get the input image information. with raster_tools.ropen( this_parameter_object_.input_image) as this_image_info: this_parameter_object_.update_info(section_counter=section_counter) # Set the output name. this_parameter_object_ = sputilities.scale_fea_check( this_parameter_object_) # Open the status YAML file. mts_ = sputilities.ManageStatus() # Load the status dictionary mts_.load_status(this_parameter_object_.status_file) # Check file status. if os.path.isfile(this_parameter_object_.out_img): if this_parameter_object_.out_img_base in mts_.status_dict: if this_parameter_object_.trigger in mts_.status_dict[ this_parameter_object_.out_img_base]: # Check every trigger because the # entire file needs to be removed. status_list = [ mts_.status_dict[this_parameter_object_.out_img_base] ['{TR}-{BD}'.format( TR=tr, BD=this_parameter_object_.band_position)] for tr in this_parameter_object_.triggers ] if 'corrupt' in status_list: logger.info('Re-running {} ...'.format( this_parameter_object_.out_img)) # Remove the file on the first trigger # if the file is corrupt. if this_parameter_object_.trigger == this_parameter_object_.triggers[ 0]: os.remove(this_parameter_object_.out_img) mts_.status_dict[this_parameter_object_.out_img_base][ '{TR}-{BD}'.format( TR=this_parameter_object_.trigger, BD=this_parameter_object_.band_position )] = 'incomplete' mts_.dump_status(this_parameter_object_.status_file) elif ('corrupt' not in status_list) and ('incomplete' in status_list): logger.info('Re-running {} ...'.format( this_parameter_object_.out_img)) else: if this_parameter_object_.overwrite: logger.info('Re-running {} ...'.format( this_parameter_object_.out_img)) # Remove the file on the first trigger. if this_parameter_object_.trigger == this_parameter_object_.triggers[ 0]: os.remove(this_parameter_object_.out_img) mts_.status_dict[ this_parameter_object_.out_img_base][ '{TR}-{BD}'.format( TR=this_parameter_object_.trigger, BD=this_parameter_object_.band_position )] = 'incomplete' mts_.dump_status( this_parameter_object_.status_file) else: logger.info('{} is already finished ...'.format( this_parameter_object_.out_img)) return else: # Remove the file on the first trigger. if this_parameter_object_.trigger == this_parameter_object_.triggers[ 0]: os.remove(this_parameter_object_.out_img) logger.info('Re-running {} ...'.format( this_parameter_object_.out_img)) i_sect = section_pair[0] j_sect = section_pair[1] # Row and column section bounds checking n_rows = raster_tools.n_rows_cols(i_sect, this_parameter_object_.sect_row_size, this_image_info.rows) n_cols = raster_tools.n_rows_cols(j_sect, this_parameter_object_.sect_col_size, this_image_info.cols) # Open the image array. if this_parameter_object_.trigger.upper( ) in this_parameter_object_.spectral_indices: wavelengths = utils.VI_WAVELENGTHS[ this_parameter_object_.trigger.upper()] # Check if the sensor supports the spectral index utils.sensor_wavelength_check(this_parameter_object_.sat_sensor, wavelengths) # Get the band positions needed # to process the spectral index. spectral_bands = utils.get_index_bands( this_parameter_object_.trigger, this_parameter_object_.sat_sensor) sect_in = this_image_info.read(bands2open=spectral_bands, i=i_sect, j=j_sect, rows=n_rows, cols=n_cols, d_type='float32') sect_in[sect_in >= this_parameter_object_. image_max] = this_parameter_object_.image_max sect_in /= this_parameter_object_.image_max vie = VegIndicesEquations(sect_in, chunk_size=-1) sect_in = vie.compute(this_parameter_object_.trigger.upper(), out_type=1) this_parameter_object_.update_info(image_min=0, image_max=1) elif this_parameter_object_.trigger == 'saliency': sect_in = saliency(this_image_info, this_parameter_object_, i_sect, j_sect, n_rows, n_cols) this_parameter_object_.update_info(image_min=0, image_max=255) elif this_parameter_object_.trigger == 'seg': sect_in = this_image_info.read(bands2open=[1, 2, 3], i=i_sect, j=j_sect, rows=n_rows, cols=n_cols) sect_in = segment_image(sect_in, this_parameter_object_) elif this_parameter_object_.trigger == 'grad': if this_image_info.bands >= 3: sect_in = sputilities.convert_rgb2gray( this_image_info, i_sect, j_sect, n_rows, n_cols, this_parameter_object_.sat_sensor)[0] else: sect_in = this_image_info.read( bands2open=this_parameter_object_.band_position, i=i_sect, j=j_sect, rows=n_rows, cols=n_cols) sect_in = get_mag_avg(sect_in) this_parameter_object_.update_info(image_min=0, image_max=30) elif this_parameter_object_.use_rgb and this_parameter_object_.trigger \ not in this_parameter_object_.spectral_indices + ['grad', 'saliency', 'seg']: sect_in = sputilities.convert_rgb2gray( this_image_info, i_sect, j_sect, n_rows, n_cols, this_parameter_object_.sat_sensor)[0] else: sect_in = this_image_info.read( bands2open=this_parameter_object_.band_position, i=i_sect, j=j_sect, rows=n_rows, cols=n_cols) if this_parameter_object_.trigger == 'dmp': # The Differential Morphological Profile # is a [D x M x N] array # where, # D = the opening/closing derivative. sect_in = get_dmp(sect_in, this_parameter_object_.image_min, this_parameter_object_.image_max) if this_parameter_object_.trigger == 'gabor': sect_in = convolve_gabor(sect_in, this_parameter_object_.image_min, this_parameter_object_.image_max, this_parameter_object_.scales) if this_parameter_object_.trigger == 'orb': sect_in = get_orb_keypoints(sect_in, this_parameter_object_.image_min, this_parameter_object_.image_max) this_parameter_object_.update_info(i_sect_blk_ctr=1, j_sect_blk_ctr=1) if this_parameter_object_.trigger in ['dmp', 'gabor']: l_rows, l_cols = sect_in[0].shape else: l_rows, l_cols = sect_in.shape # Compute section statistics. section_stats_array = spsplit.get_section_stats( sect_in, l_rows, l_cols, this_parameter_object_, section_counter) # Get the section output rows and columns. out_rows, out_cols = spsplit.get_out_dims(l_rows, l_cols, this_parameter_object_) # Reshape the list of features into # <features x rows x columns> array. out_section_array = spreshape.reshape_feature_list( section_stats_array, out_rows, out_cols, this_parameter_object_) is_corrupt = _write_section2file(this_parameter_object_, this_image_info, out_section_array, i_sect, j_sect, out_rows, out_cols, section_counter) this_parameter_object_ = None this_image_info_ = None return is_corrupt