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

Update cv.py

Browse files
Files changed (1) hide show
  1. cv.py +38 -39
cv.py CHANGED
@@ -1,46 +1,45 @@
1
  import cv2
2
  import numpy as np
 
3
 
4
- def find_black_roi(image_path, threshold_area=1000):
5
  # Read the image
6
  image = cv2.imread(image_path)
7
- if image is None:
8
- print("Error: Image not found.")
9
- return
10
-
11
- # Convert image to grayscale
12
- grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
13
-
14
- # Apply binary thresholding to separate black patches
15
- _, thresholded = cv2.threshold(grayscale, 30, 255, cv2.THRESH_BINARY)
16
-
17
- # Find contours
18
- contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
19
-
20
- # Filter contours based on area
21
- roi_list = []
22
- for contour in contours:
23
- area = cv2.contourArea(contour)
24
- if area > threshold_area:
25
- # Get bounding box coordinates
26
- x, y, w, h = cv2.boundingRect(contour)
27
- roi = image[y:y+h, x:x+w] # Extract the ROI
28
- roi_list.append(roi)
29
-
30
- return roi_list
 
 
 
 
 
 
 
 
 
31
 
32
  # Example usage
33
- image_path = "example_image.jpg"
34
- rois = find_black_roi(image_path)
35
-
36
-
37
-
38
- import matplotlib.pyplot as plt
39
- import cv2
40
-
41
- def display_image(image):
42
- plt.imshow(image)
43
- plt.axis('off') # Hide axis
44
- plt.show()
45
-
46
- display_image(gray)
 
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
+
9
+ # Convert image to grayscale if necessary
10
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
11
+
12
+ # Apply nearest neighbor algorithm
13
+ # Assuming patches are represented as feature vectors
14
+ patches = extract_patches(gray_image) # Extract patches from the image
15
+ nn = NearestNeighbors(n_neighbors=2, algorithm='auto')
16
+ nn.fit(patches)
17
+
18
+ # Find nearest neighbors
19
+ distances, indices = nn.kneighbors(patches)
20
+
21
+ # Filter patches based on threshold distance
22
+ filtered_patches_indices = np.where(distances[:,1] > threshold_distance)[0]
23
+
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)):
30
+ # Extract patches from the image
31
+ patches = []
32
+ height, width = image.shape[:2]
33
+ patch_height, patch_width = patch_size
34
+
35
+ for y in range(0, height - patch_height + 1, patch_height):
36
+ for x in range(0, width - patch_width + 1, patch_width):
37
+ patch = image[y:y+patch_height, x:x+patch_width]
38
+ patches.append(patch.flatten()) # Flatten patch to create feature vector
39
+
40
+ return np.array(patches)
41
 
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)