-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_speckle.py
More file actions
executable file
·41 lines (29 loc) · 1.57 KB
/
add_speckle.py
File metadata and controls
executable file
·41 lines (29 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from PIL import Image
import numpy as np
# Load the images
clean_image_path = 'camera_man_clean.png' # Replace with the path to your clean image
noisy_image_path = 'Noisy_image.png' # Replace with the path to your noisy image
clean_image = Image.open(clean_image_path).convert('L')
clean_ult_image = Image.open('Image01.jpg').convert('L')
noisy_image = Image.open(noisy_image_path).convert('L')
# Resize the clean image to match the noisy image's dimensions
noisy_image_resized = noisy_image.resize(clean_ult_image.size)
clean_image_resized = clean_image.resize(clean_ult_image.size)
noisy_image_array_resized = np.array(noisy_image_resized)
# Convert the noisy image to a numpy array
clean_image_array = np.array(clean_image_resized)
clean_ult_image_array = np.array(clean_ult_image)
# Calculate the noise by subtracting the clean image from the noisy image
noise = noisy_image_array_resized - clean_image_array
# Add the extracted noise back to the clean image
synthetic_noisy_image_array = clean_ult_image_array + noise
# Clip values to ensure they're within the valid range for image data (0-255)
synthetic_noisy_image_array = np.clip(synthetic_noisy_image_array, 0, 255).astype(np.uint8)
# Convert the resulting array back to an image
synthetic_noisy_image = Image.fromarray(synthetic_noisy_image_array)
# Save the synthetic noisy image
output_path = 'noisy_ultrasound.png' # Replace with your desired output path
synthetic_noisy_image.save(output_path)
# Optionally, display the synthetic noisy image
synthetic_noisy_image.show()
print(f"Synthetic noisy image saved to: {output_path}")