in combine check loss of information
- User
in combine check loss of information
Assistant
Looking at your code, I can see you want to replace the resizing approach with a padding-only approach to reach a common size. Here are the minimal changes needed: pythonimport cv2 import os import glob import argparse import numpy as np
parser = argparse.ArgumentParser(description="Split or combine character images.") parser.add_argument('--mode', choices=['split', 'combine'], default='split', help='Mode to run: split or combine') parser.add_argument('--split_input', type=str, default='split', help='Input folder for split mode') parser.add_argument('--split_output', type=str, default='combined', help='Output folder for split mode') parser.add_argument('--combine_input', type=str, default='split', help='Input folder for combine mode') parser.add_argument('--combine_output', type=str, default='combined', help='Output folder for combine mode') parser.add_argument('--original_input', type=str, default='test', help='Folder containing original images for combine mode') parser.add_argument('--padding', type=int, default=10, help='Padding to add around each character') parser.add_argument('--char_size', type=int, default=64, help='Uniform size for character images (width and height)') parser.add_argument('--debug', action='store_true', help='Save debug images showing detected character boundaries') args = parser.parse_args()
if args.mode == "split": INPUT_FOLDER = args.split_input OUTPUT_FOLDER = args.split_output PADDING = args.padding CHAR_SIZE = args.char_size os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# Clear metadata file at start of split mode meta_path = os.path.join(OUTPUT_FOLDER, "metadata.txt") if os.path.exists(meta_path): os.remove(meta_path) # Remove existing metadata file # CHANGE: First pass to find maximum dimensions after padding max_width = 0 max_height = 0 all_char_info = [] # Store info for second pass # First pass: collect all character dimensions for filepath in glob.glob(os.path.join(INPUT_FOLDER, "*.png")) + glob.glob(os.path.join(INPUT_FOLDER, "*.jpg")): filename = os.path.basename(filepath) parts = filename.split("_") if len(parts) < 2: continue label = parts[1] img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) if img is None: continue thresh = 255 - img _, thresh_bin = cv2.threshold(thresh, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) contours, _ = cv2.findContours(thresh_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) valid_contours = [] for ctr in contours: x, y, w, h = cv2.boundingRect(ctr) if w > 2 and h > 2: valid_contours.append(ctr) # Store info for second pass all_char_info.append({ 'filepath': filepath, 'filename': filename, 'label': label, 'img': img, 'contours': valid_contours }) # Find max dimensions after padding for ctr in valid_contours: x, y, w, h = cv2.boundingRect(ctr) padded_w = w + 2 * PADDING padded_h = h + 2 * PADDING max_width = max(max_width, padded_w) max_height = max(max_height, padded_h) print(f"Found common size after padding: {max_width}x{max_height}") # Second pass: process all characters with common size for char_info in all_char_info: filepath = char_info['filepath'] filename = char_info['filename'] label = char_info['label'] img = char_info['img'] valid_contours = char_info['contours'] print(f"Processing {filename}...") print(f" -> Found {len(valid_contours)} valid contours for '{label}' (expected {len(label)})") if len(valid_contours) != len(label): if len(valid_contours) < len(label): print(f" -> WARNING: Missing {len(label) - len(valid_contours)} characters in {filename}") print(f" Some characters may not be detected properly") else: print(f" -> WARNING: Found {len(valid_contours) - len(label)} extra contours") print(f" Extra contours will be ignored") metadata = [] h_orig, w_orig = img.shape[:2] metadata.append(f"ORIGINAL_SIZE {w_orig} {h_orig}") for i, ctr in enumerate(valid_contours): x, y, w, h = cv2.boundingRect(ctr) if i >= len(label): print(f"Skipping extra contour {i} in {filename} (no matching label)") break char_img = img[y:y+h, x:x+w].copy() orig_char_h, orig_char_w = char_img.shape[:2] char_img_padded = cv2.copyMakeBorder( char_img, PADDING, PADDING, PADDING, PADDING, cv2.BORDER_CONSTANT, value=255 ) # CHANGE: Instead of resizing, pad to common size current_h, current_w = char_img_padded.shape[:2] pad_w = max_width - current_w pad_h = max_height - current_h # Add additional padding to reach common size char_img_uniform = cv2.copyMakeBorder( char_img_padded, pad_h//2, pad_h - pad_h//2, # top, bottom padding pad_w//2, pad_w - pad_w//2, # left, right padding cv2.BORDER_CONSTANT, value=255 ) char_label = label[i] print(f" -> Saving char {i} ({char_label}) from {filename} - original: {orig_char_w}x{orig_char_h}, common: {max_width}x{max_height}") out_path = os.path.join( OUTPUT_FOLDER, f"{filename[:-4]}_{i}_{char_label}.png" ) cv2.imwrite(out_path, char_img_uniform) # CHANGE: Update metadata to store common size instead of CHAR_SIZE metadata.append(f"CHAR {i} {x} {y} {w} {h} {PADDING} {max_width} {max_height}") if args.debug: debug_img = img.copy() if len(debug_img.shape) == 2: debug_img = cv2.cvtColor(debug_img, cv2.COLOR_GRAY2BGR) for i, ctr in enumerate(valid_contours): if i < len(label): x, y, w, h = cv2.boundingRect(ctr) cv2.rectangle(debug_img, (x, y), (x+w, y+h), (0, 255, 0), 1) cv2.putText(debug_img, label[i], (x, y-2), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1) debug_path = os.path.join(OUTPUT_FOLDER, f"debug_{filename}") cv2.imwrite(debug_path, debug_img) meta_path = os.path.join(OUTPUT_FOLDER, "metadata.txt") with open(meta_path, "a") as f: f.write(f"FILE {filename[:-4]}\n") for line in metadata[1:]: f.write(line + "\n") f.write(metadata[0] + "\n") f.write("END_FILE\n") print("✅ Done! All word images split into common-sized characters with padding only. Metadata saved to metadata.txt")elif args.mode == "combine": INPUT_FOLDER = args.combine_input OUTPUT_FOLDER = args.combine_output os.makedirs(OUTPUT_FOLDER, exist_ok=True)
meta_file = os.path.join(INPUT_FOLDER, "metadata.txt") if not os.path.exists(meta_file): print("Error: metadata.txt not found in combine input folder") exit(1) print("Reading metadata from metadata.txt...") all_files_metadata = {} current_file = None with open(meta_file, "r") as f: for line in f: parts = line.strip().split() if len(parts) >= 2 and parts[0] == "FILE": current_file = parts[1] all_files_metadata[current_file] = {'original_size': None, 'chars': []} elif len(parts) >= 3 and parts[0] == "ORIGINAL_SIZE" and current_file: all_files_metadata[current_file]['original_size'] = (int(parts[1]), int(parts[2])) elif len(parts) == 9 and parts[0] == "CHAR" and current_file: # CHANGE: Now expects 9 parts instead of 8 idx, x, y, w, h, padding, common_w, common_h = parts[1:] all_files_metadata[current_file]['chars'].append((int(idx), int(x), int(y), int(w), int(h), int(padding), int(common_w), int(common_h))) elif parts == ["END_FILE"]: current_file = None print(f"Found metadata for {len(all_files_metadata)} files for combining.") for base_name, file_metadata in all_files_metadata.items(): print(f"Processing {base_name}...") original_size = file_metadata['original_size'] char_metadata = file_metadata['chars'] if original_size is None: print(f"Original size not found in metadata for {base_name}, skipping.") continue w_orig, h_orig = original_size combined_img = np.full((h_orig, w_orig), 255, dtype=np.uint8) # CHANGE: Updated to work with common size instead of uniform size for idx, x, y, w, h, padding, common_w, common_h in char_metadata: pattern = os.path.join(INPUT_FOLDER, f"{base_name}_{idx}_*.png") char_files = glob.glob(pattern) if not char_files: print(f"Character image for {base_name} index {idx} not found, skipping.") continue char_img_common = cv2.imread(char_files[0], cv2.IMREAD_GRAYSCALE) if char_img_common is None: print(f"Failed to read character image {char_files[0]}, skipping.") continue # CHANGE: Remove padding from common size to get back to original padded size padded_w = w + 2 * padding padded_h = h + 2 * padding # Calculate how much additional padding was added to reach common size extra_pad_w = common_w - padded_w extra_pad_h = common_h - padded_h # Remove extra padding first if extra_pad_w > 0 or extra_pad_h > 0: start_y = extra_pad_h // 2 end_y = start_y + padded_h start_x = extra_pad_w // 2 end_x = start_x + padded_w char_img_padded = char_img_common[start_y:end_y, start_x:end_x] else: char_img_padded = char_img_common # Then remove original padding to get back original character size if padding > 0: char_img = char_img_padded[padding:-padding, padding:-padding] else: char_img = char_img_padded if char_img.shape != (h, w): print(f"Warning: Character {idx} size mismatch after processing. Expected ({h},{w}), got {char_img.shape}") char_img = cv2.resize(char_img, (w, h)) combined_img[y:y+h, x:x+w] = char_img print(f" -> Placed character {idx} at ({x},{y}) size ({w},{h})") out_path = os.path.join(OUTPUT_FOLDER, f"{base_name}.png") cv2.imwrite(out_path, combined_img) print(f"✅ Saved combined image: {out_path}") original_img_path = None for ext in [".png", ".jpg", ".jpeg"]: candidate = os.path.join(args.original_input, base_name + ext) if os.path.exists(candidate): original_img_path = candidate break if original_img_path: original_img = cv2.imread(original_img_path, cv2.IMREAD_GRAYSCALE) if original_img is not None: if np.array_equal(combined_img, original_img): print(f"✅ Perfect reconstruction verified for {base_name}") else: diff = np.sum(combined_img != original_img) total_pixels = combined_img.size print(f"⚠️ Reconstruction differs by {diff}/{total_pixels} pixels ({100*diff/total_pixels:.2f}%)") print("✅ Done! All images combined back to original form.")Usage examples:
Split mode with debug visualization:
python3 script.py --mode split --split_input images --split_output characters --padding 10 --char_size 64 --debug
Split mode without debug:
python3 script.py --mode split --split_input images --split_output characters --padding 10 --char_size 64
Combine mode:
python3 script.py --mode combine --combine_input characters --combine_output reconstructed --original_input images
Key Changes Made:
Added two-pass processing in split mode: First pass finds the maximum dimensions after initial padding, second pass processes all characters with that common size.
Replaced resizing with additional padding: Instead of cv2.resize() to CHAR_SIZE, characters are padded to reach the common maximum dimensions found in the first pass.
Updated metadata format: Now stores both common width and height instead of just CHAR_SIZE.
Modified combine mode parsing: Updated to handle the new metadata format with 9 fields instead of 8.
Updated padding removal logic: Now removes both the extra padding (to reach common size) and the original padding to reconstruct the original character.
The --char_size parameter is now ignored since the common size is automatically determined from the actual character dimensions plus padding.