import numpy as np import cv2 from keras.models import loadmodel from…
- User
import numpy as np import cv2 from keras.models import load_model from keras_preprocessing.image import img_to_array, load_img import tkinter as tk from tkinter import filedialog from tkinter import messagebox import matplotlib.pyplot as plt
Load U-Net and CNN-LSTM models
unet_model = load_model(r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\lstm_model.h5") cnn_lstm_model = load_model(r'C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\cnn_lstm_model.h5')
Function to split image into patches
def image_to_patches(image, patch_size, overlap=0): patches = [] positions = [] img_height, img_width = image.shape[:2] patch_height, patch_width = patch_size step_y = patch_height - overlap step_x = patch_width - overlap for y in range(0, img_height - overlap + 1, step_y): for x in range(0, img_width - overlap + 1, step_x): patch = image[y:y + patch_height, x:x + patch_width] if patch.shape[0] != patch_height or patch.shape[1] != patch_width: pad_height = patch_height - patch.shape[0] pad_width = patch_width - patch.shape[1] patch = np.pad(patch, ((0, pad_height), (0, pad_width), (0, 0)), mode='constant') patches.append(patch) positions.append((y, x)) return np.array(patches), positions
Function to combine patches into a single image
def patches_to_image(patches, positions, image_shape, overlap=0): reconstructed_image = np.zeros(image_shape, dtype=np.float32) weight_mask = np.zeros(image_shape, dtype=np.float32) patch_height, patch_width = patches.shape[1:3]
codefor patch, (y, x) in zip(patches, positions): y_end = min(y + patch_height, image_shape[0]) x_end = min(x + patch_width, image_shape[1]) h = y_end - y w = x_end - x patch_crop = patch[:h, :w, 0] # Shape: (h, w) patch_crop = patch_crop[..., np.newaxis] # Shape: (h, w, 1) reconstructed_image[y:y_end, x:x_end, 0] += patch_crop[:, :, 0] weight_mask[y:y_end, x:x_end, 0] += 1 weight_mask[weight_mask == 0] = 1 reconstructed_image /= weight_mask return reconstructed_imageFile selection dialog functions
def select_input_file(): file_path = filedialog.askopenfilename(filetypes=[("Image Files", ".png;.jpg")]) input_entry.delete(0, tk.END) input_entry.insert(0, file_path)
def select_output_folder(): folder_path = filedialog.askdirectory() output_entry.delete(0, tk.END) output_entry.insert(0, folder_path)
Image processing and saving function
- Assistant