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

Update cv.py

Browse files
Files changed (1) hide show
  1. cv.py +13 -2
cv.py CHANGED
@@ -1,8 +1,9 @@
1
  import cv2
2
  import numpy as np
3
  from sklearn.neighbors import NearestNeighbors
 
4
 
5
- def detect_back_patches(image_path, threshold_distance):
6
  # Read the image
7
  image = cv2.imread(image_path)
8
 
@@ -24,6 +25,14 @@ def detect_back_patches(image_path, threshold_distance):
24
  # Get the ROIs corresponding to the filtered patches
25
  ROIs = [patches[idx] for idx in filtered_patches_indices]
26
 
 
 
 
 
 
 
 
 
27
  return ROIs
28
 
29
  def extract_patches(image, patch_size=(32, 32)):
@@ -42,4 +51,6 @@ def extract_patches(image, patch_size=(32, 32)):
42
  # Example usage
43
  image_path = 'document_image.jpg'
44
  threshold_distance = 100 # Adjust this threshold based on your requirements
45
- back_patches_ROIs = detect_back_patches(image_path, threshold_distance)
 
 
 
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
 
 
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)):
 
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
+