Update cv.py
Browse files
cv.py
CHANGED
|
@@ -1,56 +1,50 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
-
from sklearn.neighbors import NearestNeighbors
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
patch = image[y:y+patch_height, x:x+patch_width]
|
| 47 |
-
patches.append(patch.flatten()) # Flatten patch to create feature vector
|
| 48 |
-
|
| 49 |
-
return np.array(patches)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
threshold_distance = 100 # Adjust this threshold based on your requirements
|
| 54 |
-
save_path = 'black_patches'
|
| 55 |
-
back_patches_ROIs = detect_back_patches(image_path, threshold_distance, save_path)
|
| 56 |
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def detect_background(gray, threshold=0.70):
|
| 5 |
+
# Convert image to grayscale
|
| 6 |
+
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 7 |
+
|
| 8 |
+
# Thresholding to binarize the image
|
| 9 |
+
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
|
| 10 |
+
|
| 11 |
+
# Find contours
|
| 12 |
+
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 13 |
+
|
| 14 |
+
# Initialize list to store identified background regions
|
| 15 |
+
background_regions = []
|
| 16 |
+
|
| 17 |
+
# Iterate through contours
|
| 18 |
+
for contour in contours:
|
| 19 |
+
# Calculate area of contour
|
| 20 |
+
area = cv2.contourArea(contour)
|
| 21 |
+
|
| 22 |
+
# Calculate bounding rectangle
|
| 23 |
+
x, y, w, h = cv2.boundingRect(contour)
|
| 24 |
+
|
| 25 |
+
# Calculate aspect ratio of bounding rectangle
|
| 26 |
+
aspect_ratio = w / h if h != 0 else 0
|
| 27 |
+
|
| 28 |
+
# Calculate ratio of area of contour to area of bounding rectangle
|
| 29 |
+
ratio = area / (w * h) if (w * h) != 0 else 0
|
| 30 |
+
|
| 31 |
+
# If aspect ratio is close to 1 (nearly square) and ratio is greater than threshold, it's likely background
|
| 32 |
+
if aspect_ratio > 0.9 and ratio > threshold:
|
| 33 |
+
background_regions.append((x, y, w, h))
|
| 34 |
+
|
| 35 |
+
return background_regions
|
| 36 |
|
| 37 |
+
def save_background_regions(image, regions, output_prefix="background_region"):
|
| 38 |
+
for i, region in enumerate(regions):
|
| 39 |
+
x, y, w, h = region
|
| 40 |
+
region_image = image[y:y+h, x:x+w]
|
| 41 |
+
cv2.imwrite(f"black_patches/{output_prefix}_{i}.png", region_image)
|
| 42 |
+
|
| 43 |
+
# Load document image
|
| 44 |
+
gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Detect continuous black or white background regions
|
| 47 |
+
background_regions = detect_background(gray)
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Save identified background regions as images
|
| 50 |
+
save_background_regions(gray, background_regions)
|