| import cv2 |
| import numpy as np |
| from sklearn.neighbors import NearestNeighbors |
| import os |
|
|
| def detect_back_patches(image_path, threshold_distance, save_path): |
| |
| image = cv2.imread(image_path) |
| |
| |
| gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
| |
| |
| |
| patches = extract_patches(gray_image) |
| nn = NearestNeighbors(n_neighbors=2, algorithm='auto') |
| nn.fit(patches) |
| |
| |
| distances, indices = nn.kneighbors(patches) |
| |
| |
| filtered_patches_indices = np.where(distances[:,1] > threshold_distance)[0] |
| |
| |
| ROIs = [patches[idx] for idx in filtered_patches_indices] |
| |
| |
| if not os.path.exists(save_path): |
| os.makedirs(save_path) |
| |
| for i, roi in enumerate(ROIs): |
| patch_image = roi.reshape(gray_image.shape[0] // 32, gray_image.shape[1] // 32) |
| cv2.imwrite(os.path.join(save_path, f"black_patch_{i}.jpg"), patch_image) |
| |
| return ROIs |
|
|
| def extract_patches(image, patch_size=(32, 32)): |
| |
| patches = [] |
| height, width = image.shape[:2] |
| patch_height, patch_width = patch_size |
| |
| for y in range(0, height - patch_height + 1, patch_height): |
| for x in range(0, width - patch_width + 1, patch_width): |
| patch = image[y:y+patch_height, x:x+patch_width] |
| patches.append(patch.flatten()) |
| |
| return np.array(patches) |
|
|
| |
| image_path = 'document_image.jpg' |
| threshold_distance = 100 |
| save_path = 'black_patches' |
| back_patches_ROIs = detect_back_patches(image_path, threshold_distance, save_path) |
|
|
|
|