Update cv.py
Browse files
cv.py
CHANGED
|
@@ -1,50 +1,60 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
# Convert image to grayscale
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
_, binary = cv2.threshold(gray,
|
| 10 |
|
| 11 |
# Find contours
|
| 12 |
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 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
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
def find_contiguous_regions(image, area_threshold):
|
| 6 |
# Convert image to grayscale
|
| 7 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 8 |
|
| 9 |
+
# Threshold the image to get binary image
|
| 10 |
+
_, binary = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY_INV)
|
| 11 |
|
| 12 |
# Find contours
|
| 13 |
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 14 |
|
| 15 |
+
# Filter out small contours and get their bounding rectangles
|
| 16 |
+
bounding_rects = [cv2.boundingRect(contour) for contour in contours if cv2.contourArea(contour) > area_threshold]
|
| 17 |
|
| 18 |
+
return bounding_rects
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def is_black_ratio_satisfied(patch, threshold):
|
| 21 |
+
# Calculate black to white ratio in the patch
|
| 22 |
+
black_pixels = np.count_nonzero(patch == 0)
|
| 23 |
+
white_pixels = np.count_nonzero(patch == 255)
|
| 24 |
+
total_pixels = black_pixels + white_pixels
|
| 25 |
+
|
| 26 |
+
# Calculate the ratio
|
| 27 |
+
black_ratio = black_pixels / total_pixels
|
| 28 |
+
|
| 29 |
+
return black_ratio >= threshold
|
| 30 |
|
| 31 |
+
def save_patches(image, patches, output_dir, ratio_threshold):
|
| 32 |
+
for idx, patch in enumerate(patches):
|
| 33 |
+
x, y, w, h = patch
|
| 34 |
+
patch_image = image[y:y+h, x:x+w]
|
| 35 |
+
|
| 36 |
+
# Check if the black ratio is satisfied
|
| 37 |
+
if is_black_ratio_satisfied(patch_image, ratio_threshold):
|
| 38 |
+
cv2.imwrite(os.path.join(output_dir, f"patch_{idx}.png"), patch_image)
|
| 39 |
|
| 40 |
+
def main(image_path, output_dir, area_threshold, ratio_threshold):
|
| 41 |
+
# Read the image
|
| 42 |
+
image = cv2.imread(image_path)
|
| 43 |
+
|
| 44 |
+
# Find contiguous black patches
|
| 45 |
+
black_patches = find_contiguous_regions(image, area_threshold)
|
| 46 |
+
|
| 47 |
+
# Save patches as separate images
|
| 48 |
+
save_patches(image, black_patches, output_dir, ratio_threshold)
|
| 49 |
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
image_path = "fin.png" # Path to your document image
|
| 52 |
+
output_dir = "black_patches/" # Directory to save the patches
|
| 53 |
+
area_threshold = 1000 # Minimum area threshold for a patch to be considered
|
| 54 |
+
ratio_threshold = 0.80 # Maximum ratio of white to black pixels
|
| 55 |
+
|
| 56 |
+
# Create output directory if it doesn't exist
|
| 57 |
+
if not os.path.exists(output_dir):
|
| 58 |
+
os.makedirs(output_dir)
|
| 59 |
+
|
| 60 |
+
main(image_path, output_dir, area_threshold, ratio_threshold)
|