Remove_Noise.py 13.1 KB
Newer Older
1
from WorkingPyDemo import *
Bryce Hepner's avatar
Bryce Hepner committed
2
from scipy.ndimage.filters import gaussian_filter
3
import paramiko
Bryce Hepner's avatar
Bryce Hepner committed
4
from PIL import ImageFilter
5 6 7 8
from scipy.signal import convolve2d
from scipy.stats import multivariate_normal

from skimage.restoration import wiener
9
def setup_remote_sftpclient():
Bryce Hepner's avatar
Bryce Hepner committed
10 11 12 13 14
    """
    Setup the SFTP client
    Returns:
        sftp_client (paramiko.SFTPClient): the SFTP client
    """
15 16 17 18 19
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect("192.168.0.107", username="elphel")
    sftp_client = client.open_sftp()
    return sftp_client
20

Bryce Hepner's avatar
Bryce Hepner committed
21 22 23 24 25 26 27 28 29
def create_average(images, which_sensor):
    """
    Create an average image from a list of images
    Parameters:
        images (list): a list of images
        which_sensor (int): the sensor number
    Returns:   
        average_image (np.array): the average image
    """
30 31 32 33 34 35 36 37 38 39 40 41 42
    same_sensor_images = []
    which_sensor = str(which_sensor)
    average_image = np.zeros_like(np.array(Image.open(images[0]))[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)
    for i, image_name in enumerate(same_sensor_images):
        image_object = Image.open(image_name)
        average_image = np.array(image_object)[1:] + average_image
Bryce Hepner's avatar
Bryce Hepner committed
43
        
44
    return average_image/len(same_sensor_images)
45

Bryce Hepner's avatar
Bryce Hepner committed
46
def remote_create_average(images, which_sensor):
Bryce Hepner's avatar
Bryce Hepner committed
47
    """
48
    Create an average image from a list of remote images
Bryce Hepner's avatar
Bryce Hepner committed
49 50 51 52 53 54
    Parameters:
        images (list): a list of images
        which_sensor (int): the sensor number
    Returns:
        average_image (np.array): the average image
    """
55 56
    sftp_client = setup_remote_sftpclient()
    averages = []
Bryce Hepner's avatar
Bryce Hepner committed
57 58
    
    same_sensor_images = images
59
    which_sensor = str(which_sensor)
Bryce Hepner's avatar
Bryce Hepner committed
60 61
    small_set_images = []
    
62 63 64 65
    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)
Bryce Hepner's avatar
Bryce Hepner committed
66
        small_set_images.append(np.array(image_object)[1:])
67 68 69
        # print(np.array(image_object).shape)
        # print(np.array(image_object)[1:] + average_image)
        if (i % 100 == 0) and i!=0:
Bryce Hepner's avatar
Bryce Hepner committed
70
            image_object = np.mean(np.array(small_set_images),axis = 0)
71
            averages.append(image_object)
Bryce Hepner's avatar
Bryce Hepner committed
72 73 74 75 76
            small_set_images = []
            print(i)
    if len(small_set_images) > 1:
        image_object = np.mean(np.array(small_set_images),axis = 0)
        averages.append(image_object)
77
    sftp_client.close()
Bryce Hepner's avatar
Bryce Hepner committed
78 79 80 81
    if len(averages) > 1:
        return np.mean(averages,axis=0)
    else:
        return np.array(averages[0])
82 83 84 85 86 87 88 89 90 91

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
    """
Bryce Hepner's avatar
Bryce Hepner committed
92
    sftp_client = setup_remote_sftpclient()
93
    # sftp_client.listdir("media/elphel/NVME/lwir16-proc/te0607/scenes/")
Bryce Hepner's avatar
Bryce Hepner committed
94
    dirs_in_scenes = sftp_client.listdir(headname)
95 96 97 98 99 100
    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)
Bryce Hepner's avatar
Bryce Hepner committed
101
    sftp_client.close()
102 103 104
    return scenes

def remote_image_extractor(scenes):
105
    """Find all the remote files in the directory
Bryce Hepner's avatar
Bryce Hepner committed
106 107 108 109 110
    Parameters:
        dirname (str): the directory name
    Returns:
       image_folder (list): a list of all the file paths in the directory
    """
111 112 113 114 115 116 117 118 119 120 121
    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
Bryce Hepner's avatar
Bryce Hepner committed
122
def find_only_in_channel(images, channel_name = "10"):
Bryce Hepner's avatar
Bryce Hepner committed
123 124 125 126 127 128 129 130
    """
    Find the images that are only in the specified channel
    Parameters:
        images (list): a list of images
        channel_name (str): the channel name
    Returns:
        same_sensor_images (list): a list of images that are only in the specified channel
    """
Bryce Hepner's avatar
Bryce Hepner committed
131 132 133 134 135 136 137 138 139 140
    same_sensor_images = []
    for i, image_name in enumerate(images):
        if int(channel_name) > 9:
            if image_name[-7:-5] == channel_name:
                same_sensor_images.append(image_name)
        else:
            if image_name[-7:-5] == "_" + channel_name:
                same_sensor_images.append(image_name)
    return same_sensor_images
def adjust_to_original(new_image, average_image):
Bryce Hepner's avatar
Bryce Hepner committed
141 142 143 144 145 146 147 148
    """
    Adjust the image to the original image
    Parameters:
        new_image (np.array): the new image
        average_image (np.array): the average image
    Returns:
        adjusted_image (np.array): the adjusted image
    """
149 150
    original_image_min = np.min(new_image)
    original_image_max = np.max(new_image)
151
    average_image = average_image - np.mean(average_image)
152 153 154 155 156
    # adjusted_image = average_image - new_image
    # adjusted_image = gaussian_filter(new_image,sigma=1)

    adjusted_image = new_image - (average_image - gaussian_filter(average_image,sigma=1))
    # adjusted_image = gaussian_filter(adjusted_image,sigma=20)
157
    # adjusted_image = new_image + (average_image - np.array(Image.fromarray(average_image).convert("L").filter(ImageFilter.GaussianBlur(radius=4))))
158 159 160 161 162
    # plt.subplot(121)
    # plt.imshow(color_adjust((adjusted_image)),cmap='gray',vmin = 0, vmax=1)
    # plt.subplot(122)
    # plt.imshow(color_adjust(new_image),cmap='gray',vmin = 0, vmax=1)
    # plt.show()
163 164 165 166 167


    adjusted_image = adjusted_image - np.min(adjusted_image)
    adjusted_image = adjusted_image*(original_image_max-original_image_min)/np.max(adjusted_image)
    adjusted_image = adjusted_image + original_image_min
168
    # print(adjusted_image.dtype)
Bryce Hepner's avatar
Bryce Hepner committed
169
    return adjusted_image.astype(np.uint16)
170

171
def color_adjust(visual_array):
Bryce Hepner's avatar
Bryce Hepner committed
172 173 174 175 176 177 178
    """
    Adjust the image to the original image
    Parameters:
        visual_array (np.array): the visual array
    Returns:
        adjusted_array (np.array): the adjusted image
    """
179 180
    min_of_errors = np.min(visual_array)
    adjusted_array = visual_array - min_of_errors
181
    adjusted_array = adjusted_array/np.max(adjusted_array)
182
    return adjusted_array
Bryce Hepner's avatar
Bryce Hepner committed
183 184

def save_new_average(quantity_of_images = 1500, channel = "10", head_dir = "/media/elphel/NVME/lwir16-proc/te0607/scenes/"):
Bryce Hepner's avatar
Bryce Hepner committed
185 186 187 188 189 190 191
    """
    Save the new average image
    Parameters:
        quantity_of_images (int): the number of images to use in the average
        channel (str): the channel name
        head_dir (str): the head directory
    """
Bryce Hepner's avatar
Bryce Hepner committed
192 193 194
    scenes = remote_file_extractor(head_dir)
    images = find_only_in_channel(remote_image_extractor(scenes),channel)
    if quantity_of_images != "all":
195 196
        images = np.random.choice(images,quantity_of_images,replace = False)
        # images = images[:quantity_of_images]
Bryce Hepner's avatar
Bryce Hepner committed
197 198 199 200 201
    average_image = remote_create_average(images,channel)
    # average_image = average_image.astype(int)
    average_savable_image = Image.fromarray(average_image)
    average_savable_image.save("Average_On_Channel(" + channel + ").tiff")

Bryce Hepner's avatar
Bryce Hepner committed
202 203 204 205 206 207 208 209
def save_testable_images(images, selected_channel, quantity_of_images):
    """
    Saves the testable images
    Parameters:
        images (list): a list of images
        selected_channel (str): the channel name
        quantity_of_images (int): the number of images to use in the average
    """
Bryce Hepner's avatar
Bryce Hepner committed
210 211 212
    sftp_client = setup_remote_sftpclient()
    images = find_only_in_channel(images, selected_channel)

213
    # image_locations = np.random.choice(len(images), quantity_of_images, replace=False)
214
    image_locations = np.arange(quantity_of_images) + 10000
Bryce Hepner's avatar
Bryce Hepner committed
215 216 217

    selected_images = np.array(images)[image_locations]

218 219
    average_image = np.array(Image.open("Average_On_Channel(" + selected_channel + ").tiff"))
    images_in_each_direction = 80
Bryce Hepner's avatar
Bryce Hepner committed
220
    for i, item in enumerate(selected_images):
221 222 223 224 225 226
        # if image_locations[i] < images_in_each_direction:
        #     average_image = remote_create_average(images[image_locations[i] - image_locations[i]: image_locations[i] + images_in_each_direction + image_locations[i]], selected_channel)
        # elif image_locations[i] > (len(images) - images_in_each_direction):
        #     average_image = remote_create_average(images[image_locations[i] - images_in_each_direction - (len(images) - image_locations[i]): image_locations[i] + (len(images) - image_locations[i])], selected_channel)
        # else:
        #     average_image = remote_create_average(images[image_locations[i] - images_in_each_direction: image_locations[i] + images_in_each_direction], selected_channel)
Bryce Hepner's avatar
Bryce Hepner committed
227 228 229 230
        image = Image.open(sftp_client.open(item))
        wherelastslash = item.rfind("/")
        image = np.array(image)[1:]
        savable_original = Image.fromarray(image)
231
        savable_original.save("original_images(" + selected_channel + ")/innerfolder/original" + item[wherelastslash + 1:])
Bryce Hepner's avatar
Bryce Hepner committed
232 233 234
        altered_image = adjust_to_original(image, average_image)
        altered_image = Image.fromarray(altered_image)
        
235
        altered_image.save("averaged_images(" + selected_channel + ")/innerfolder" + item[wherelastslash:])
Bryce Hepner's avatar
Bryce Hepner committed
236 237 238
        # average_image = Image.fromarray(average_image)

    sftp_client.close()
239

240
def save_new_gauss():
Bryce Hepner's avatar
Bryce Hepner committed
241 242
    """
    Save the new gauss image
243
    """
244
    
245
    # x,y = np.mgrid[-1:1:.003125, -1:1:.003125]
246 247
    x,y = np.mgrid[-1:1:.44, -1:1:.44]
    print(x.shape)
248 249 250
    pos = np.dstack((x,y))
    # grid = np.zeros((l,l))
    # gauss = np.exp(-0.5 * np.square(ax) / np.square(sig))
251
    
252
    # normal_grid = multivariate_normal.pdf(grid, mean = [0]*l, cov = [5]*l)
253
    normal_grid = multivariate_normal([0, 0], [[1.1, 5], [2, 1]]).pdf(pos)
254 255 256 257 258 259 260 261 262
    normal_grid = normal_grid/np.sum(normal_grid)
    end_image = Image.fromarray(normal_grid)
    # fig2 = plt.figure()
    # ax2 = fig2.add_subplot(111)
    # ax2.contourf(x, y, normal_grid)
    # plt.show()
    # print(np.sum(np.array(end_image)))
    end_image.save("gaussian_kernel.tiff")

263
if __name__ == "__main__":
264
    # save_new_average(350,"11")
265 266
    # save_new_gauss()
    
267
    gaussian_kernel = np.array(Image.open("gaussian_kernel.tiff"))
268
    # gaussian_kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
269 270
    scenes = remote_file_extractor("/media/elphel/NVME/lwir16-proc/te0607/scenes/")
    images = remote_image_extractor(scenes)
Bryce Hepner's avatar
Bryce Hepner committed
271 272
    images = find_only_in_channel(images, "11")
    # average_image = np.array(Image.open("Average_On_Channel(" + "11" + ").tiff"))
273
    save_testable_images(images,"11",5)
Bryce Hepner's avatar
Bryce Hepner committed
274 275
    # plt.imshow(color_adjust(average_image),cmap='gray',vmin = 0, vmax=1)
    # plt.show()
276
    
277 278
    print(len(images))
    average_image = remote_create_average(images[5000 - 10: 5000 + 10], "11")
279 280
    # plt.imshow(color_adjust((average_image - gaussian_filter(average_image,sigma=5))),cmap='gray',vmin = 0, vmax=1)
    # plt.show()
281
    sftp_client = setup_remote_sftpclient()
282
    # print(len(images))
Bryce Hepner's avatar
Bryce Hepner committed
283
    # print(images[10000])
284
    test_image = sftp_client.open(images[10000])
285 286
    test_image = Image.open(test_image)
    test_image = np.array(test_image)[1:]
Bryce Hepner's avatar
Bryce Hepner committed
287
    # print(test_image)
288
    plt.subplot(121)
289
    plt.imshow(color_adjust(adjust_to_original(test_image,average_image)),cmap='gray',vmin = 0, vmax=1)
290
    # plt.subplot(122)
291 292
    # plt.show()
    # print(gaussian_kernel)
293
    # little_more_blurred = convolve2d(np.pad(test_image,2, mode = 'edge'),gaussian_kernel, "valid")
294
    # print(little_more_blurred)
295
    # print(little_more_blurred.shape)
296
    # print(little_inverter(gaussian_kernel)@gaussian_kernel)
297
    # altered_image = Image.fromarray(little_more_blurred.astype(np.uint16))
298

299 300
    # altered_image.save("averaged_images(" + "11" + ")/innerfolder" + "/testable.tiff")
    # plt.imshow(color_adjust(little_more_blurred[3:-3,3:-3]),cmap='gray',vmin = 0, vmax=1)
301 302 303 304
    # plt.show()
    # renewed_array = 
    # H = fft(kernel)
	# deconvolved = np.real(ifft(fft(signal)*np.conj(H)/(H*np.conj(H) + lambd**2)))
305
    # back_to_normal = wiener(little_more_blurred,gaussian_kernel,.02,clip=False)
306 307 308 309
    # print(back_to_normal)
    # print(back_to_normal.shape)
    
    # plt.imshow(color_adjust(back_to_normal), cmap='gray',vmin = 0, vmax=1)
310
    # plt.show()
Bryce Hepner's avatar
Bryce Hepner committed
311 312 313
    # newimage = Image.fromarray(test_image - average_image)
    # newimage.save("NoInterference.tiff")
    
314 315 316 317 318
    # plt.subplot(121)
    # plt.imshow(color_adjust(test_image),cmap='gray',vmin = 0, vmax=1)
    # plt.subplot(122)
    # plt.imshow(color_adjust(adjust_to_original(test_image,average_image)),cmap='gray',vmin = 0, vmax=1)
    # plt.show()
Bryce Hepner's avatar
Bryce Hepner committed
319
    sftp_client.close()
320
    # print(np.linalg.inv(np.array([[3,0,-1],[0,3,3],[1,-3,-4]])))
321 322
    # print(np.linalg.pinv(np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]])))

323
    # last_image = Image.open("NoInterference.tiff")
324
    # plt.imshow(color_adjust(np.array(last_image)),cmap='gray',vmin = 0, vmax=1)
325
    # plt.show()