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

Update cv.py

Browse files
Files changed (1) hide show
  1. cv.py +44 -50
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 detect_back_patches(image_path, threshold_distance, save_path):
7
- # Read the image
8
- image = cv2.imread(image_path)
9
-
10
- # Convert image to grayscale if necessary
11
- gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
12
-
13
- # Apply nearest neighbor algorithm
14
- # Assuming patches are represented as feature vectors
15
- patches = extract_patches(gray_image) # Extract patches from the image
16
- nn = NearestNeighbors(n_neighbors=2, algorithm='auto')
17
- nn.fit(patches)
18
-
19
- # Find nearest neighbors
20
- distances, indices = nn.kneighbors(patches)
21
-
22
- # Filter patches based on threshold distance
23
- filtered_patches_indices = np.where(distances[:,1] > threshold_distance)[0]
24
-
25
- # Get the ROIs corresponding to the filtered patches
26
- ROIs = [patches[idx] for idx in filtered_patches_indices]
27
-
28
- # Save the black patches as images
29
- if not os.path.exists(save_path):
30
- os.makedirs(save_path)
31
-
32
- for i, roi in enumerate(ROIs):
33
- patch_image = roi.reshape(gray_image.shape[0] // 32, gray_image.shape[1] // 32)
34
- cv2.imwrite(os.path.join(save_path, f"black_patch_{i}.jpg"), patch_image)
35
-
36
- return ROIs
 
37
 
38
- def extract_patches(image, patch_size=(32, 32)):
39
- # Extract patches from the image
40
- patches = []
41
- height, width = image.shape[:2]
42
- patch_height, patch_width = patch_size
43
-
44
- for y in range(0, height - patch_height + 1, patch_height):
45
- for x in range(0, width - patch_width + 1, patch_width):
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
- # Example usage
52
- image_path = 'document_image.jpg'
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)