jkushwaha commited on
Commit
7f7bf76
·
verified ·
1 Parent(s): 0de21f6

Update cv.py

Browse files
Files changed (1) hide show
  1. cv.py +46 -36
cv.py CHANGED
@@ -1,50 +1,60 @@
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)
 
 
 
 
 
 
 
 
 
 
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)