Loading Remove_Noise.py +126 −11 Original line number Diff line number Diff line from matplotlib.image import composite_images from WorkingPyDemo import * import paramiko def setup_remote_sftpclient(): client = paramiko.SSHClient() client.load_system_host_keys() client.connect("192.168.0.107", username="elphel") sftp_client = client.open_sftp() return sftp_client def remove_noise(images, which_sensor): same_sensor_images = [] which_sensor = str(which_sensor) Loading @@ -19,18 +25,127 @@ def remove_noise(images, which_sensor): # print(np.array(image_object)[1:] + average_image) average_image = np.array(image_object)[1:] + average_image return average_image/len(same_sensor_images) scenes = file_extractor(folder_name) images = image_extractor(scenes) average_image = remove_noise(images,"7") def remote_remove_noise(images, which_sensor): sftp_client = setup_remote_sftpclient() averages = [] same_sensor_images = [] which_sensor = str(which_sensor) first_image = sftp_client.open(images[0]) average_image = np.array(Image.open(first_image))[1:] for i, image_name in enumerate(images): if int(which_sensor) > 9: if image_name[-7:-5] == which_sensor: same_sensor_images.append(image_name) else: if image_name[-7:-5] == "_" + which_sensor: same_sensor_images.append(image_name) images = [] for i, image_name in enumerate(same_sensor_images): # print(image_name) image_object = sftp_client.open(image_name) image_object = Image.open(image_object) images.append(np.array(image_object)[1:]) # print(np.array(image_object).shape) # print(np.array(image_object)[1:] + average_image) if (i % 100 == 0) and i!=0: image_object = np.mean(np.array(images),axis = 0) # print(image_object.shape) averages.append(image_object) # print(average_image.shape) images = [] image_object = np.mean(np.array(images)) averages.append(image_object) sftp_client.close() return np.mean(averages,axis=0) def remote_file_extractor(headname = "/media/elphel/NVME/lwir16-proc/te0607/scenes/"): """Find all the files in the directory Parameters: dirname (str): the directory name Returns: files (list): a list of all the files in the directory """ client = paramiko.SSHClient() client.load_system_host_keys() client.connect("192.168.0.107", username="elphel") sftp_client = client.open_sftp() # sftp_client.listdir("media/elphel/NVME/lwir16-proc/te0607/scenes/") dirs_in_scenes = sftp_client.listdir("/media/elphel/NVME/lwir16-proc/te0607/scenes/") scenes = [] for i, curr_folder in enumerate(dirs_in_scenes): if "." not in curr_folder: smaller_dirs = sftp_client.listdir(headname + curr_folder) for small_folder in smaller_dirs: scenes.append(headname + curr_folder + "/" + small_folder) return scenes def remote_image_extractor(scenes): sftp_client = setup_remote_sftpclient() image_folder = [] for scene in scenes: files = sftp_client.listdir(scene) for file in files: if file[-5:] != ".tiff" or file[-7:] == "_6.tiff": continue else: image_folder.append(os.path.join(scene, file)) sftp_client.close() return image_folder #returns a list of file paths to .tiff files in the specified directory given in file_extractor def remove_the_noise(new_image, average_image): original_image_min = np.min(newimage) original_image_max = np.max(new_image) adjusted_image = new_image - average_image adjusted_image = adjusted_image - np.min(adjusted_image) adjusted_image = adjusted_image*original_image_max/np.max(adjusted_image) adjusted_image = adjusted_image + original_image_min return adjusted_image def color_adjust(visual_array): min_of_errors = np.min(visual_array) adjusted_array = visual_array - min_of_errors adjusted_array = np.round(adjusted_array*255/np.max(adjusted_array)) adjusted_array = adjusted_array/np.max(adjusted_array) # print(adjusted_array) # print(np.max(adjusted_array)) return adjusted_array print(np.max(average_image)) print(np.min(average_image)) plt.imshow(color_adjust(average_image),cmap='gray',vmin = 0, vmax=255) if __name__ == "__main__": scenes = remote_file_extractor("/media/elphel/NVME/lwir16-proc/te0607/scenes/") # images = remote_image_extractor(np.random.choice(scenes,10000,replace = False)) images = remote_image_extractor(scenes) # average_image = remote_remove_noise(images,"10") average_image = np.array(Image.open("hopefullyaverage.tiff")) # average_savable_image = Image.fromarray(average_image) # average_savable_image.save("hopefullyaverage.tiff") # print(np.max(average_image)) # print(np.min(average_image)) # average_image = color_adjust(average_image) plt.imshow(color_adjust(average_image),cmap='gray',vmin = 0, vmax=1) plt.show() # print(len(images)) sftp_client = setup_remote_sftpclient() print(len(images)) for i, item in enumerate(images[22000:22030]): if item[-7:-5] == "10": print(i) print(item) print(images[22016]) test_image = sftp_client.open(images[22016]) test_image = Image.open(test_image) test_image = np.array(test_image)[1:] newimage = Image.fromarray(test_image - average_image) newimage.save("NoInterference.tiff") plt.subplot(121) plt.imshow(color_adjust(test_image),cmap='gray',vmin = 0, vmax=1) plt.subplot(122) plt.imshow(color_adjust(test_image-average_image),cmap='gray',vmin = 0, vmax=1) plt.show() # print(np.linalg.inv(np.array([[3,0,-1],[0,3,3],[1,-3,-4]]))) print(np.linalg.pinv(np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]]))) No newline at end of file # print(np.linalg.pinv(np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]]))) No newline at end of file Loading
Remove_Noise.py +126 −11 Original line number Diff line number Diff line from matplotlib.image import composite_images from WorkingPyDemo import * import paramiko def setup_remote_sftpclient(): client = paramiko.SSHClient() client.load_system_host_keys() client.connect("192.168.0.107", username="elphel") sftp_client = client.open_sftp() return sftp_client def remove_noise(images, which_sensor): same_sensor_images = [] which_sensor = str(which_sensor) Loading @@ -19,18 +25,127 @@ def remove_noise(images, which_sensor): # print(np.array(image_object)[1:] + average_image) average_image = np.array(image_object)[1:] + average_image return average_image/len(same_sensor_images) scenes = file_extractor(folder_name) images = image_extractor(scenes) average_image = remove_noise(images,"7") def remote_remove_noise(images, which_sensor): sftp_client = setup_remote_sftpclient() averages = [] same_sensor_images = [] which_sensor = str(which_sensor) first_image = sftp_client.open(images[0]) average_image = np.array(Image.open(first_image))[1:] for i, image_name in enumerate(images): if int(which_sensor) > 9: if image_name[-7:-5] == which_sensor: same_sensor_images.append(image_name) else: if image_name[-7:-5] == "_" + which_sensor: same_sensor_images.append(image_name) images = [] for i, image_name in enumerate(same_sensor_images): # print(image_name) image_object = sftp_client.open(image_name) image_object = Image.open(image_object) images.append(np.array(image_object)[1:]) # print(np.array(image_object).shape) # print(np.array(image_object)[1:] + average_image) if (i % 100 == 0) and i!=0: image_object = np.mean(np.array(images),axis = 0) # print(image_object.shape) averages.append(image_object) # print(average_image.shape) images = [] image_object = np.mean(np.array(images)) averages.append(image_object) sftp_client.close() return np.mean(averages,axis=0) def remote_file_extractor(headname = "/media/elphel/NVME/lwir16-proc/te0607/scenes/"): """Find all the files in the directory Parameters: dirname (str): the directory name Returns: files (list): a list of all the files in the directory """ client = paramiko.SSHClient() client.load_system_host_keys() client.connect("192.168.0.107", username="elphel") sftp_client = client.open_sftp() # sftp_client.listdir("media/elphel/NVME/lwir16-proc/te0607/scenes/") dirs_in_scenes = sftp_client.listdir("/media/elphel/NVME/lwir16-proc/te0607/scenes/") scenes = [] for i, curr_folder in enumerate(dirs_in_scenes): if "." not in curr_folder: smaller_dirs = sftp_client.listdir(headname + curr_folder) for small_folder in smaller_dirs: scenes.append(headname + curr_folder + "/" + small_folder) return scenes def remote_image_extractor(scenes): sftp_client = setup_remote_sftpclient() image_folder = [] for scene in scenes: files = sftp_client.listdir(scene) for file in files: if file[-5:] != ".tiff" or file[-7:] == "_6.tiff": continue else: image_folder.append(os.path.join(scene, file)) sftp_client.close() return image_folder #returns a list of file paths to .tiff files in the specified directory given in file_extractor def remove_the_noise(new_image, average_image): original_image_min = np.min(newimage) original_image_max = np.max(new_image) adjusted_image = new_image - average_image adjusted_image = adjusted_image - np.min(adjusted_image) adjusted_image = adjusted_image*original_image_max/np.max(adjusted_image) adjusted_image = adjusted_image + original_image_min return adjusted_image def color_adjust(visual_array): min_of_errors = np.min(visual_array) adjusted_array = visual_array - min_of_errors adjusted_array = np.round(adjusted_array*255/np.max(adjusted_array)) adjusted_array = adjusted_array/np.max(adjusted_array) # print(adjusted_array) # print(np.max(adjusted_array)) return adjusted_array print(np.max(average_image)) print(np.min(average_image)) plt.imshow(color_adjust(average_image),cmap='gray',vmin = 0, vmax=255) if __name__ == "__main__": scenes = remote_file_extractor("/media/elphel/NVME/lwir16-proc/te0607/scenes/") # images = remote_image_extractor(np.random.choice(scenes,10000,replace = False)) images = remote_image_extractor(scenes) # average_image = remote_remove_noise(images,"10") average_image = np.array(Image.open("hopefullyaverage.tiff")) # average_savable_image = Image.fromarray(average_image) # average_savable_image.save("hopefullyaverage.tiff") # print(np.max(average_image)) # print(np.min(average_image)) # average_image = color_adjust(average_image) plt.imshow(color_adjust(average_image),cmap='gray',vmin = 0, vmax=1) plt.show() # print(len(images)) sftp_client = setup_remote_sftpclient() print(len(images)) for i, item in enumerate(images[22000:22030]): if item[-7:-5] == "10": print(i) print(item) print(images[22016]) test_image = sftp_client.open(images[22016]) test_image = Image.open(test_image) test_image = np.array(test_image)[1:] newimage = Image.fromarray(test_image - average_image) newimage.save("NoInterference.tiff") plt.subplot(121) plt.imshow(color_adjust(test_image),cmap='gray',vmin = 0, vmax=1) plt.subplot(122) plt.imshow(color_adjust(test_image-average_image),cmap='gray',vmin = 0, vmax=1) plt.show() # print(np.linalg.inv(np.array([[3,0,-1],[0,3,3],[1,-3,-4]]))) print(np.linalg.pinv(np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]]))) No newline at end of file # print(np.linalg.pinv(np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]]))) No newline at end of file