code / cv.py
jkushwaha's picture
Update cv.py
f29c684 verified
raw
history blame
2 kB
import cv2
import numpy as np
from sklearn.neighbors import NearestNeighbors
import os
def detect_back_patches(image_path, threshold_distance, save_path):
# Read the image
image = cv2.imread(image_path)
# Convert image to grayscale if necessary
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply nearest neighbor algorithm
# Assuming patches are represented as feature vectors
patches = extract_patches(gray_image) # Extract patches from the image
nn = NearestNeighbors(n_neighbors=2, algorithm='auto')
nn.fit(patches)
# Find nearest neighbors
distances, indices = nn.kneighbors(patches)
# Filter patches based on threshold distance
filtered_patches_indices = np.where(distances[:,1] > threshold_distance)[0]
# Get the ROIs corresponding to the filtered patches
ROIs = [patches[idx] for idx in filtered_patches_indices]
# Save the black patches as images
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)):
# Extract patches from the image
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()) # Flatten patch to create feature vector
return np.array(patches)
# Example usage
image_path = 'document_image.jpg'
threshold_distance = 100 # Adjust this threshold based on your requirements
save_path = 'black_patches'
back_patches_ROIs = detect_back_patches(image_path, threshold_distance, save_path)