|
import numpy as np |
|
from PIL import Image |
|
import matplotlib.pyplot as plt |
|
import matplotlib.colors as mcolors |
|
from scipy.interpolate import interp1d |
|
import os |
|
import uuid |
|
|
|
|
|
def calculate_intensity(image): |
|
""" |
|
Calculate the intensity of the image by converting it to grayscale |
|
and taking the mean value. |
|
""" |
|
gray_image = image.convert('L') |
|
intensity = np.mean(np.array(gray_image)) |
|
return intensity |
|
|
|
|
|
def process_image(image_path): |
|
""" |
|
Process the image by dividing it into 32x32 grids and calculating normalized intensity. |
|
""" |
|
|
|
image = Image.open(image_path) |
|
width, height = image.size |
|
|
|
|
|
grid_width = width // 32 |
|
grid_height = height // 32 |
|
|
|
intensities = [] |
|
|
|
|
|
for y in range(32): |
|
row_intensities = [] |
|
for x in range(32): |
|
left = x * grid_width |
|
upper = y * grid_height |
|
right = left + grid_width |
|
lower = upper + grid_height |
|
|
|
|
|
grid = image.crop((left, upper, right, lower)) |
|
intensity = calculate_intensity(grid) |
|
row_intensities.append(intensity) |
|
intensities.append(row_intensities) |
|
|
|
intensities = np.array(intensities) |
|
|
|
|
|
min_intensity = intensities.min() |
|
max_intensity = intensities.max() |
|
normalized_intensities = (intensities - min_intensity) / (max_intensity - min_intensity) |
|
|
|
return normalized_intensities |
|
|
|
|
|
def intensity_to_oxygen_level(normalized_intensities): |
|
""" |
|
Map normalized intensity values to oxygen levels using interpolation. |
|
""" |
|
|
|
ksv_values = [0, 0.009303080328, 0.19176, 0.5196879311, 0.6252571452, 0.7115134738, |
|
0.8476158622, 0.9531850764, 1.039441405, 1.145010619, 1.175543793, |
|
1.231266948, 1.281113007, 1.47293855, 1.609040939] |
|
oxygen_levels = [0, 0.68, 1, 2, 2.5, 3, 4, 5, 6, 7.5, 8, 9, 10, 15, 20] |
|
|
|
|
|
min_ksv = min(ksv_values) |
|
max_ksv = max(ksv_values) |
|
normalized_intensities = (normalized_intensities * (max_ksv - min_ksv)) + min_ksv |
|
|
|
|
|
interp_func = interp1d(ksv_values, oxygen_levels, kind='linear', fill_value='extrapolate') |
|
oxygen_levels_mapped = interp_func(normalized_intensities) |
|
|
|
return oxygen_levels_mapped |
|
|
|
|
|
def generate_heatmap(data, output_path): |
|
""" |
|
Generate a high-resolution heatmap from data and save it as an image. |
|
""" |
|
plt.figure(figsize=(10, 8)) |
|
plt.imshow(data, cmap='RdBu_r', interpolation='nearest', norm=mcolors.Normalize(vmin=0, vmax=20)) |
|
plt.colorbar(label='Oxygen Level (%)') |
|
|
|
|
|
plt.savefig(output_path, dpi=300) |
|
plt.close() |
|
|
|
|
|
def process_and_generate_heatmap(image): |
|
""" |
|
Process the image and generate a heatmap, saving the result to a file. |
|
""" |
|
|
|
unique_id = str(uuid.uuid4()) |
|
input_dir = 'Gradio_Images' |
|
input_image_path = os.path.join(input_dir, f'uploaded_image_{unique_id}.jpg') |
|
image.save(input_image_path) |
|
|
|
|
|
normalized_intensities = process_image(input_image_path) |
|
oxygen_levels = intensity_to_oxygen_level(normalized_intensities) |
|
|
|
|
|
output_dir = 'Heatmap_Images' |
|
|
|
output_image_path = os.path.join(output_dir, f'heatmap_{unique_id}.jpg') |
|
|
|
generate_heatmap(oxygen_levels, output_image_path) |
|
|
|
return output_image_path |
|
|
|
|
|
|